Skip to content

Commit

Permalink
Use square brackets for %w and %i array literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Nov 17, 2023
1 parent 06dc201 commit 7ea2c67
Show file tree
Hide file tree
Showing 27 changed files with 97 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Generate new file by running `ameba --gen-config`.
**List of sources to run Ameba on can be configured globally via:**

- `Globs` section - an array of wildcards (or paths) to include to the
inspection. Defaults to `%w(**/*.cr !lib)`, meaning it includes all project
inspection. Defaults to `%w[**/*.cr !lib]`, meaning it includes all project
files with `*.cr` extension except those which exist in `lib` folder.
- `Excluded` section - an array of wildcards (or paths) to exclude from the
source list defined by `Globs`. Defaults to an empty array.
Expand Down
12 changes: 6 additions & 6 deletions spec/ameba/base_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Ameba::Rule
end

it "contains rules across all the available groups" do
Rule.rules.map(&.group_name).uniq!.reject!(&.empty?).sort.should eq %w(
Rule.rules.map(&.group_name).uniq!.reject!(&.empty?).sort.should eq %w[
Ameba
Documentation
Layout
Expand All @@ -19,7 +19,7 @@ module Ameba::Rule
Naming
Performance
Style
)
]
end
end

Expand Down Expand Up @@ -50,25 +50,25 @@ module Ameba::Rule

it "returns false if source is not excluded from this rule" do
rule = DummyRule.new
rule.excluded = %w(some_source.cr)
rule.excluded = %w[some_source.cr]
rule.excluded?(Source.new "", "another_source.cr").should_not be_true
end

it "returns true if source is excluded from this rule" do
rule = DummyRule.new
rule.excluded = %w(source.cr)
rule.excluded = %w[source.cr]
rule.excluded?(Source.new "", "source.cr").should be_true
end

it "returns true if source matches the wildcard" do
rule = DummyRule.new
rule.excluded = %w(**/*.cr)
rule.excluded = %w[**/*.cr]
rule.excluded?(Source.new "", __FILE__).should be_true
end

it "returns false if source does not match the wildcard" do
rule = DummyRule.new
rule.excluded = %w(*_spec.cr)
rule.excluded = %w[*_spec.cr]
rule.excluded?(Source.new "", "source.cr").should be_false
end
end
Expand Down
56 changes: 28 additions & 28 deletions spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ module Ameba::Cli
describe "Cmd" do
describe ".run" do
it "runs ameba" do
r = Cli.run %w(-f silent -c spec/fixtures/config.yml spec/fixtures/source.cr)
r = Cli.run %w[-f silent -c spec/fixtures/config.yml spec/fixtures/source.cr]
r.should be_nil
end
end

describe ".parse_args" do
%w(-s --silent).each do |flag|
%w[-s --silent].each do |flag|
it "accepts #{flag} flag" do
c = Cli.parse_args [flag]
c.formatter.should eq :silent
end
end

%w(-c --config).each do |flag|
%w[-c --config].each do |flag|
it "accepts #{flag} flag" do
c = Cli.parse_args [flag, "config.yml"]
c.config.should eq Path["config.yml"]
end
end

%w(-f --format).each do |flag|
%w[-f --format].each do |flag|
it "accepts #{flag} flag" do
c = Cli.parse_args [flag, "my-formatter"]
c.formatter.should eq "my-formatter"
Expand All @@ -34,68 +34,68 @@ module Ameba::Cli

it "accepts --only flag" do
c = Cli.parse_args ["--only", "RULE1,RULE2"]
c.only.should eq %w(RULE1 RULE2)
c.only.should eq %w[RULE1 RULE2]
end

it "accepts --except flag" do
c = Cli.parse_args ["--except", "RULE1,RULE2"]
c.except.should eq %w(RULE1 RULE2)
c.except.should eq %w[RULE1 RULE2]
end

it "defaults rules? flag to false" do
c = Cli.parse_args %w(spec/fixtures/source.cr)
c = Cli.parse_args %w[spec/fixtures/source.cr]
c.rules?.should be_false
end

it "defaults skip_reading_config? flag to false" do
c = Cli.parse_args %w(spec/fixtures/source.cr)
c = Cli.parse_args %w[spec/fixtures/source.cr]
c.skip_reading_config?.should be_false
end

it "accepts --rules flag" do
c = Cli.parse_args %w(--rules)
c = Cli.parse_args %w[--rules]
c.rules?.should eq true
end

it "defaults all? flag to false" do
c = Cli.parse_args %w(spec/fixtures/source.cr)
c = Cli.parse_args %w[spec/fixtures/source.cr]
c.all?.should be_false
end

it "accepts --all flag" do
c = Cli.parse_args %w(--all)
c = Cli.parse_args %w[--all]
c.all?.should eq true
end

it "accepts --gen-config flag" do
c = Cli.parse_args %w(--gen-config)
c = Cli.parse_args %w[--gen-config]
c.formatter.should eq :todo
end

it "accepts --no-color flag" do
c = Cli.parse_args %w(--no-color)
c = Cli.parse_args %w[--no-color]
c.colors?.should be_false
end

it "accepts --without-affected-code flag" do
c = Cli.parse_args %w(--without-affected-code)
c = Cli.parse_args %w[--without-affected-code]
c.without_affected_code?.should be_true
end

it "doesn't disable colors by default" do
c = Cli.parse_args %w(--all)
c = Cli.parse_args %w[--all]
c.colors?.should be_true
end

it "ignores --config if --gen-config flag passed" do
c = Cli.parse_args %w(--gen-config --config my_config.yml)
c = Cli.parse_args %w[--gen-config --config my_config.yml]
c.formatter.should eq :todo
c.skip_reading_config?.should be_true
end

describe "-e/--explain" do
it "configures file/line/column" do
c = Cli.parse_args %w(--explain spec/fixtures/source.cr:3:5)
c = Cli.parse_args %w[--explain spec/fixtures/source.cr:3:5]

location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "spec/fixtures/source.cr"
Expand All @@ -105,59 +105,59 @@ module Ameba::Cli

it "raises an error if location is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain spec/fixtures/source.cr:3)
Cli.parse_args %w[--explain spec/fixtures/source.cr:3]
end
end

it "raises an error if line number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain spec/fixtures/source.cr:a:3)
Cli.parse_args %w[--explain spec/fixtures/source.cr:a:3]
end
end

it "raises an error if column number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain spec/fixtures/source.cr:3:&)
Cli.parse_args %w[--explain spec/fixtures/source.cr:3:&]
end
end

it "raises an error if line/column are missing" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain spec/fixtures/source.cr)
Cli.parse_args %w[--explain spec/fixtures/source.cr]
end
end
end

context "--fail-level" do
it "configures fail level Convention" do
c = Cli.parse_args %w(--fail-level convention)
c = Cli.parse_args %w[--fail-level convention]
c.fail_level.should eq Severity::Convention
end

it "configures fail level Warning" do
c = Cli.parse_args %w(--fail-level Warning)
c = Cli.parse_args %w[--fail-level Warning]
c.fail_level.should eq Severity::Warning
end

it "configures fail level Error" do
c = Cli.parse_args %w(--fail-level error)
c = Cli.parse_args %w[--fail-level error]
c.fail_level.should eq Severity::Error
end

it "raises if fail level is incorrect" do
expect_raises(Exception, "Incorrect severity name JohnDoe") do
Cli.parse_args %w(--fail-level JohnDoe)
Cli.parse_args %w[--fail-level JohnDoe]
end
end
end

it "accepts unknown args as globs" do
c = Cli.parse_args %w(source1.cr source2.cr)
c.globs.should eq %w(source1.cr source2.cr)
c = Cli.parse_args %w[source1.cr source2.cr]
c.globs.should eq %w[source1.cr source2.cr]
end

it "accepts one unknown arg as explain location if it has correct format" do
c = Cli.parse_args %w(source.cr:3:22)
c = Cli.parse_args %w[source.cr:3:22]

location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "source.cr"
Expand Down
18 changes: 9 additions & 9 deletions spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Ameba
Globs: src/*.cr
CONFIG
config = Config.new(yml)
config.globs.should eq %w(src/*.cr)
config.globs.should eq %w[src/*.cr]
end

it "initializes globs as array" do
Expand All @@ -43,7 +43,7 @@ module Ameba
- "!spec"
CONFIG
config = Config.new(yml)
config.globs.should eq %w(src/*.cr !spec)
config.globs.should eq %w[src/*.cr !spec]
end

it "raises if Globs has a wrong type" do
Expand All @@ -62,7 +62,7 @@ module Ameba
Excluded: spec
CONFIG
config = Config.new(yml)
config.excluded.should eq %w(spec)
config.excluded.should eq %w[spec]
end

it "initializes excluded as array" do
Expand All @@ -73,7 +73,7 @@ module Ameba
- lib/*.cr
CONFIG
config = Config.new(yml)
config.excluded.should eq %w(spec lib/*.cr)
config.excluded.should eq %w[spec lib/*.cr]
end

it "raises if Excluded has a wrong type" do
Expand Down Expand Up @@ -145,12 +145,12 @@ module Ameba
end

it "returns a list of sources matching globs" do
config.globs = %w(**/config_spec.cr)
config.globs = %w[**/config_spec.cr]
config.sources.size.should eq(1)
end

it "returns a list of sources excluding 'Excluded'" do
config.excluded = %w(**/config_spec.cr)
config.excluded = %w[**/config_spec.cr]
config.sources.any?(&.fullpath.==(__FILE__)).should be_false
end
end
Expand Down Expand Up @@ -192,7 +192,7 @@ module Ameba

it "updates excluded property" do
name = DummyRule.rule_name
excluded = %w(spec/source.cr)
excluded = %w[spec/source.cr]
config.update_rule name, excluded: excluded
rule = config.rules.find!(&.name.== name)
rule.excluded.should eq excluded
Expand All @@ -211,7 +211,7 @@ module Ameba

it "updates multiple rules by excluded property" do
name = DummyRule.rule_name
excluded = %w(spec/source.cr)
excluded = %w[spec/source.cr]
config.update_rules [name], excluded: excluded
rule = config.rules.find!(&.name.== name)
rule.excluded.should eq excluded
Expand All @@ -226,7 +226,7 @@ module Ameba

it "updates a group by excluded property" do
name = DummyRule.group_name
excluded = %w(spec/source.cr)
excluded = %w[spec/source.cr]
config.update_rules [name], excluded: excluded
rule = config.rules.find!(&.name.== DummyRule.rule_name)
rule.excluded.should eq excluded
Expand Down
28 changes: 14 additions & 14 deletions spec/ameba/rule/lint/percent_arrays_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ module Ameba::Rule::Lint

it "passes if percent arrays are written correctly" do
s = Source.new %q(
%i(one two three)
%w(one two three)
%i[one two three]
%w[one two three]
%i(1 2 3)
%w(1 2 3)
%i[1 2 3]
%w[1 2 3]
%i()
%w()
%i[]
%w[]
)
subject.catch(s).should be_valid
end

it "fails if string percent array has commas" do
s = Source.new %( %w(one, two) )
s = Source.new %( %w[one, two] )
subject.catch(s).should_not be_valid
end

it "fails if string percent array has quotes" do
s = Source.new %( %w("one" "two") )
s = Source.new %( %w["one" "two"] )
subject.catch(s).should_not be_valid
end

it "fails if symbols percent array has commas" do
s = Source.new %( %i(one, two) )
s = Source.new %( %i[one, two] )
subject.catch(s).should_not be_valid
end

it "fails if symbols percent array has a colon" do
s = Source.new %( %i(:one :two) )
s = Source.new %( %i[:one :two] )
subject.catch(s).should_not be_valid
end

it "reports rule, location and message for %i" do
s = Source.new %(
%i(:one)
%i[:one]
), "source.cr"

subject.catch(s).should_not be_valid
Expand All @@ -54,7 +54,7 @@ module Ameba::Rule::Lint

it "reports rule, location and message for %w" do
s = Source.new %(
%w("one")
%w["one"]
), "source.cr"

subject.catch(s).should_not be_valid
Expand All @@ -71,14 +71,14 @@ module Ameba::Rule::Lint
it "#string_array_unwanted_symbols" do
rule = PercentArrays.new
rule.string_array_unwanted_symbols = ","
s = Source.new %( %w("one") )
s = Source.new %( %w["one"] )
rule.catch(s).should be_valid
end

it "#symbol_array_unwanted_symbols" do
rule = PercentArrays.new
rule.symbol_array_unwanted_symbols = ","
s = Source.new %( %i(:one) )
s = Source.new %( %i[:one] )
rule.catch(s).should be_valid
end
end
Expand Down
Loading

0 comments on commit 7ea2c67

Please sign in to comment.