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