Use issue expectation helpers in ShadowingOuterLocalVar spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 01:30:27 +02:00
parent 353d09df29
commit ad5d56b313

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Lint
subject = ShadowingOuterLocalVar.new subject = ShadowingOuterLocalVar.new
it "doesn't report if there is no shadowing" do it "doesn't report if there is no shadowing" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
def some_method def some_method
foo = 1 foo = 1
@ -14,126 +14,117 @@ module Ameba::Rule::Lint
end end
-> (baz : Int32) {} -> (baz : Int32) {}
-> (bar : String) {} -> (bar : String) {}
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "reports if there is a shadowing in a block" do it "reports if there is a shadowing in a block" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
def some_method def some_method
foo = 1 foo = 1
3.times do |foo| 3.times do |foo|
# ^ error: Shadowing outer local variable `foo`
end end
end end
) CRYSTAL
subject.catch(source).should_not be_valid
end end
it "does not report outer vars declared below shadowed block" do it "does not report outer vars declared below shadowed block" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
methods = klass.methods.select { |m| m.annotation(MyAnn) } methods = klass.methods.select { |m| m.annotation(MyAnn) }
m = methods.last m = methods.last
) CRYSTAL
subject.catch(source).should be_valid
end end
it "reports if there is a shadowing in a proc" do it "reports if there is a shadowing in a proc" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
def some_method def some_method
foo = 1 foo = 1
-> (foo : Int32) {} -> (foo : Int32) {}
# ^ error: Shadowing outer local variable `foo`
end end
) CRYSTAL
subject.catch(source).should_not be_valid
end end
it "reports if there is a shadowing in an inner scope" do it "reports if there is a shadowing in an inner scope" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
def foo def foo
foo = 1 foo = 1
3.times do |i| 3.times do |i|
3.times { |foo| foo } 3.times { |foo| foo }
# ^ error: Shadowing outer local variable `foo`
end end
end end
) CRYSTAL
subject.catch(source).should_not be_valid
end end
it "reports if variable is shadowed twice" do it "reports if variable is shadowed twice" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
foo = 1 foo = 1
3.times do |foo| 3.times do |foo|
# ^ error: Shadowing outer local variable `foo`
-> (foo : Int32) { foo + 1 } -> (foo : Int32) { foo + 1 }
# ^ error: Shadowing outer local variable `foo`
end end
) CRYSTAL
subject.catch(source).should_not be_valid
source.issues.size.should eq 2
end end
it "reports if a splat block argument shadows local var" do it "reports if a splat block argument shadows local var" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
foo = 1 foo = 1
3.times do |*foo| 3.times do |*foo|
# ^ error: Shadowing outer local variable `foo`
end end
) CRYSTAL
subject.catch(source).should_not be_valid
end end
it "reports if a &block argument is shadowed" do it "reports if a &block argument is shadowed" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
def method_with_block(a, &block) def method_with_block(a, &block)
3.times do |block| 3.times do |block|
# ^ error: Shadowing outer local variable `block`
end end
end end
) CRYSTAL
subject.catch(source).should_not be_valid
source.issues.first.message.should eq "Shadowing outer local variable `block`"
end end
it "reports if there are multiple args and one shadows local var" do it "reports if there are multiple args and one shadows local var" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
foo = 1 foo = 1
[1, 2, 3].each_with_index do |i, foo| [1, 2, 3].each_with_index do |i, foo|
# ^ error: Shadowing outer local variable `foo`
i + foo i + foo
end end
) CRYSTAL
subject.catch(source).should_not be_valid
source.issues.first.message.should eq "Shadowing outer local variable `foo`"
end end
it "doesn't report if an outer var is reassigned in a block" do it "doesn't report if an outer var is reassigned in a block" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
def foo def foo
foo = 1 foo = 1
3.times do |i| 3.times do |i|
foo = 2 foo = 2
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "doesn't report if an argument is a black hole '_'" do it "doesn't report if an argument is a black hole '_'" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
_ = 1 _ = 1
3.times do |_| 3.times do |_|
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "doesn't report if it shadows record type declaration" do it "doesn't report if it shadows record type declaration" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
class FooBar class FooBar
record Foo, index : String record Foo, index : String
@ -142,12 +133,11 @@ module Ameba::Rule::Lint
end end
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "doesn't report if it shadows throwaway arguments" do it "doesn't report if it shadows throwaway arguments" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
data = [{1, "a"}, {2, "b"}, {3, "c"}] data = [{1, "a"}, {2, "b"}, {3, "c"}]
data.each do |_, string| data.each do |_, string|
@ -155,18 +145,16 @@ module Ameba::Rule::Lint
puts string, number puts string, number
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "does not report if argument shadows an ivar assignment" do it "does not report if argument shadows an ivar assignment" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
def bar(@foo) def bar(@foo)
@foo.try do |foo| @foo.try do |foo|
end end
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "reports rule, location and message" do it "reports rule, location and message" do
@ -185,7 +173,7 @@ module Ameba::Rule::Lint
context "macro" do context "macro" do
it "does not report shadowed vars in outer scope" do it "does not report shadowed vars in outer scope" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
macro included macro included
def foo def foo
{% for ivar in instance_vars %} {% for ivar in instance_vars %}
@ -197,12 +185,11 @@ module Ameba::Rule::Lint
{% instance_vars.reject { |ivar| ivar } %} {% instance_vars.reject { |ivar| ivar } %}
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "does not report shadowed vars in macro within the same scope" do it "does not report shadowed vars in macro within the same scope" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
{% methods = klass.methods.select { |m| m.annotation(MyAnn) } %} {% methods = klass.methods.select { |m| m.annotation(MyAnn) } %}
{% for m, m_idx in methods %} {% for m, m_idx in methods %}
@ -210,12 +197,11 @@ module Ameba::Rule::Lint
{% d %} {% d %}
{% end %} {% end %}
{% end %} {% end %}
) CRYSTAL
subject.catch(source).should be_valid
end end
it "does not report shadowed vars within nested macro" do it "does not report shadowed vars within nested macro" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
module Foo module Foo
macro included macro included
def foo def foo
@ -233,12 +219,11 @@ module Ameba::Rule::Lint
end end
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
it "does not report scoped vars to MacroFor" do it "does not report scoped vars to MacroFor" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
struct Test struct Test
def test def test
{% for ivar in @type.instance_vars %} {% for ivar in @type.instance_vars %}
@ -248,8 +233,7 @@ module Ameba::Rule::Lint
{% ["a", "b"].map { |ivar| puts ivar } %} {% ["a", "b"].map { |ivar| puts ivar } %}
end end
end end
) CRYSTAL
subject.catch(source).should be_valid
end end
end end
end end