Skip to content

Commit

Permalink
Merge pull request #561 from crystal-ameba/refactor-rules-specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored Feb 4, 2025
2 parents e8d0424 + 5defa09 commit 17cdb61
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 174 deletions.
49 changes: 24 additions & 25 deletions spec/ameba/rule/lint/require_parentheses_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,45 @@ module Ameba::Rule::Lint

it "passes if logical operator in call args has parentheses" do
expect_no_issues subject, <<-CRYSTAL
if foo.includes?("bar") || foo.includes?("batz")
puts "this code is bug-free"
end
foo.includes?("bar") || foo.includes?("baz")
foo.includes?("bar" || foo.includes? "baz")
CRYSTAL
end

if foo.includes?("bar" || foo.includes? "batz")
puts "this code is bug-free"
end
it "passes if logical operator in call doesn't involve another method call" do
expect_no_issues subject, <<-CRYSTAL
foo.includes? "bar" || "baz"
CRYSTAL
end

form.add("query", "val_1" || "val_2")
form.add "query", "val_1" || "val_2"
form.add "query", ("val_1" || "val_2")
it "passes if logical operator in call involves another method call with no arguments" do
expect_no_issues subject, <<-CRYSTAL
foo.includes? "bar" || foo.not_nil!
CRYSTAL
end

it "passes if logical operator in assignment call" do
it "passes if logical operator is used in an assignment call" do
expect_no_issues subject, <<-CRYSTAL
hello.there = "world" || method.call
hello.there ||= "world" || method.call
foo.bar = "baz" || bat.call :foo
foo.bar ||= "baz" || bat.call :foo
foo[bar] = "baz" || bat.call :foo
CRYSTAL
end

it "passes if logical operator in square bracket call" do
it "passes if logical operator is used in a square bracket call" do
expect_no_issues subject, <<-CRYSTAL
hello["world" || :thing]
hello["world" || :thing]?
this.is[1 || method.call]
foo["bar" || baz.call :bat]
foo["bar" || baz.call :bat]?
CRYSTAL
end

it "fails if logical operator in call args doesn't have parentheses" do
expect_issue subject, <<-CRYSTAL
if foo.includes? "bar" || foo.includes? "batz"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use parentheses in the method call to avoid confusion about precedence
puts "this code is not bug-free"
end
if foo.in? "bar", "baz" || foo.ends_with? "qux"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use parentheses in the method call to avoid confusion about precedence
puts "this code is not bug-free"
end
foo.includes? "bar" || foo.includes? "baz"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use parentheses in the method call to avoid confusion about precedence
foo.in? "bar", "baz" || foo.ends_with? "bat"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use parentheses in the method call to avoid confusion about precedence
CRYSTAL
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/shadowed_argument_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Ameba::Rule::Lint

it "reports if there is a shadowed proc argument" do
s = Source.new %(
->(x : Int32) {
-> (x : Int32) {
x = 20
x
}
Expand Down
6 changes: 3 additions & 3 deletions spec/ameba/rule/lint/shadowing_outer_local_var_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Ameba::Rule::Lint
bar
end
-> (baz : Int32) {}
-> (bar : String) {}
-> (baz : Int32) { }
-> (bar : String) { }
end
CRYSTAL
end
Expand Down Expand Up @@ -67,7 +67,7 @@ module Ameba::Rule::Lint
def some_method
foo = 1
-> (foo : Int32) {}
-> (foo : Int32) { }
# ^^^^^^^^^^^ error: Shadowing outer local variable `foo`
end
CRYSTAL
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/shared_var_in_fiber_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module Ameba::Rule::Lint
expect_no_issues subject, <<-CRYSTAL
i = 0
while i < 10
proc = ->(x : Int32) do
proc = -> (x : Int32) do
spawn do
puts(x)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/ameba/rule/lint/trailing_rescue_exception_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module Ameba::Rule::Lint

it "passes for trailing rescue with literal values" do
expect_no_issues subject, <<-CRYSTAL
puts "hello" rescue "world"
puts :meow rescue 1234
puts "foo" rescue "bar"
puts :foo rescue 42
CRYSTAL
end

it "passes for trailing rescue with class initialization" do
expect_no_issues subject, <<-CRYSTAL
puts "hello" rescue MyClass.new
puts "foo" rescue MyClass.new
CRYSTAL
end

Expand Down
8 changes: 4 additions & 4 deletions spec/ameba/rule/lint/unused_argument_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Ameba::Rule::Lint
i + 1
end
->(i : Int32) { i + 1 }
-> (i : Int32) { i + 1 }
CRYSTAL
end

Expand Down Expand Up @@ -296,7 +296,7 @@ module Ameba::Rule::Lint
rule.ignore_procs = true

expect_no_issues rule, <<-CRYSTAL
->(a : Int32) {}
-> (a : Int32) { }
CRYSTAL
end

Expand All @@ -305,8 +305,8 @@ module Ameba::Rule::Lint
rule.ignore_procs = false

expect_issue rule, <<-CRYSTAL
->(a : Int32) {}
# ^^^^^^^^^ error: Unused argument `a`. If it's necessary, use `_a` as an argument name to indicate that it won't be used.
-> (a : Int32) { }
# ^^^^^^^^^ error: Unused argument `a`. If it's necessary, use `_a` as an argument name to indicate that it won't be used.
CRYSTAL
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/ameba/rule/lint/useless_assign_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Ameba::Rule::Lint

it "reports a useless assignment in a proc" do
expect_issue subject, <<-CRYSTAL
->() {
-> () {
a = 2
# ^ error: Useless assignment to variable `a`
}
Expand All @@ -46,7 +46,7 @@ module Ameba::Rule::Lint
it "reports a useless assignment in a proc inside def" do
expect_issue subject, <<-CRYSTAL
def method
->() {
-> () {
a = 2
# ^ error: Useless assignment to variable `a`
}
Expand All @@ -65,7 +65,7 @@ module Ameba::Rule::Lint
expect_issue subject, <<-CRYSTAL
def method
3.times do
->() {
-> () {
a = 2
# ^ error: Useless assignment to variable `a`
}
Expand Down Expand Up @@ -239,7 +239,7 @@ module Ameba::Rule::Lint
expect_no_issues subject, <<-CRYSTAL
def method
called = false
->() { called = true }
-> () { called = true }
called
end
CRYSTAL
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/style/redundant_self_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module Ameba::Rule::Style
end
def bar
->(foo : Int32) { self.foo + foo }
-> (foo : Int32) { self.foo + foo }
end
end
CRYSTAL
Expand Down
53 changes: 28 additions & 25 deletions spec/ameba/rule/typing/macro_call_argument_type_restriction_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,42 @@ module Ameba::Rule::Typing

it "passes if macro call args have type restrictions" do
expect_no_issues subject, <<-CRYSTAL
class Greeter
getter name : String?
class_getter age : Int32 = 0
setter tasks : Array(String) = [] of String
class_setter queue : Array(Int32)?
property task_mutex : Mutex = Mutex.new
class_property asdf : String
class Foo
getter foo : Int32?
setter bar : Array(Int32)?
property baz : Bool?
end
record Task,
cmd : String,
args : Array(String) = %w[]
args : Array(String)
CRYSTAL
end

it "fails if a macro call arg doesn't have a type restriction" do
expect_issue subject, <<-CRYSTAL
class Greeter
getter name
# ^^^^ error: Argument should have a type restriction
getter :age
# ^^^^ error: Argument should have a type restriction
getter "height"
# ^^^^^^^^ error: Argument should have a type restriction
it "passes if macro call args have default values" do
expect_no_issues subject, <<-CRYSTAL
class Foo
getter foo = 0
setter bar = [] of Int32
property baz = true
end
record Task,
cmd = "",
args = %w[]
CRYSTAL
end

it "passes if a record call arg with a default value doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
record Task,
cmd : String,
args = %[]
it "fails if a macro call arg doesn't have a type restriction" do
expect_issue subject, <<-CRYSTAL
class Foo
getter foo
# ^^^ error: Argument should have a type restriction
getter :bar
# ^^^^ error: Argument should have a type restriction
getter "baz"
# ^^^^^ error: Argument should have a type restriction
end
CRYSTAL
end

Expand All @@ -49,9 +52,9 @@ module Ameba::Rule::Typing

it "fails if a macro call arg with a default value doesn't have a type restriction" do
expect_issue rule, <<-CRYSTAL
class Greeter
getter name = "Kenobi"
# ^^^^ error: Argument should have a type restriction
class Foo
getter foo = "bar"
# ^^^ error: Argument should have a type restriction
end
CRYSTAL
end
Expand Down
Loading

0 comments on commit 17cdb61

Please sign in to comment.