shard-ameba/spec/ameba/rule/lint/shadowed_argument_spec.cr

167 lines
3.6 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2018-05-28 16:42:01 +00:00
module Ameba::Rule::Lint
2018-05-28 16:42:01 +00:00
describe ShadowedArgument do
subject = ShadowedArgument.new
it "doesn't report if there is not a shadowed argument" do
s = Source.new %(
def foo(bar)
baz = 1
end
3.times do |i|
a = 1
end
proc = -> (a : Int32) {
b = 2
}
)
subject.catch(s).should be_valid
end
it "reports if there is a shadowed method argument" do
s = Source.new %(
def foo(bar)
bar = 1
bar
end
)
subject.catch(s).should_not be_valid
end
it "reports if there is a shadowed block argument" do
s = Source.new %(
3.times do |i|
i = 2
end
)
subject.catch(s).should_not be_valid
end
it "reports if there is a shadowed proc argument" do
s = Source.new %(
->(x : Int32) {
x = 20
x
}
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is referenced before the assignment" do
s = Source.new %(
def foo(bar)
bar
bar = 1
end
)
subject.catch(s).should be_valid
end
it "doesn't report if the argument is conditionally reassigned" do
s = Source.new %(
def foo(bar = nil)
bar ||= true
bar
end
)
subject.catch(s).should be_valid
end
it "doesn't report if the op assign is followed by another assignment" do
s = Source.new %(
def foo(bar)
bar ||= 3
bar = 43
bar
end
)
subject.catch(s).should be_valid
end
it "reports if the shadowing assignment is followed by op assign" do
s = Source.new %(
def foo(bar)
bar = 42
bar ||= 43
bar
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is unused" do
s = Source.new %(
def foo(bar)
end
)
subject.catch(s).should be_valid
end
it "reports if the argument is shadowed before super" do
s = Source.new %(
def foo(bar)
bar = 1
super
end
)
subject.catch(s).should_not be_valid
end
context "branch" do
it "doesn't report if the argument is not shadowed in a condition" do
s = Source.new %(
def foo(bar, baz)
bar = 1 if baz
bar
end
)
subject.catch(s).should be_valid
end
it "reports if the argument is shadowed after the condition" do
s = Source.new %(
def foo(foo)
if something
foo = 42
end
foo = 43
foo
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is conditionally assigned in a branch" do
s = Source.new %(
def foo(bar)
if something
bar ||= 22
end
bar
end
)
subject.catch(s).should be_valid
end
end
it "reports rule, location and message" do
s = Source.new %(
def foo(bar)
bar = 22
bar
end
), "source.cr"
subject.catch(s).should_not be_valid
2018-06-10 21:15:12 +00:00
issue = s.issues.first
issue.rule.should_not be_nil
2018-09-07 12:07:03 +00:00
issue.location.to_s.should eq "source.cr:2:3"
2018-11-24 17:38:13 +00:00
issue.end_location.to_s.should eq "source.cr:2:10"
2018-06-10 21:15:12 +00:00
issue.message.should eq "Argument `bar` is assigned before it is used"
2018-05-28 16:42:01 +00:00
end
end
end