Use issue expectation helpers in Lint::AmbiguousAssignment rule spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 21:32:46 +02:00
parent caeb1609c9
commit 390b26d7f1

View file

@ -6,163 +6,115 @@ module Ameba::Rule::Lint
context "when using `-`" do context "when using `-`" do
it "registers an offense with `x`" do it "registers an offense with `x`" do
source = Source.new("x =- y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid x =- y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "registers an offense with `@x`" do it "registers an offense with `@x`" do
source = Source.new("@x =- y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @x =- y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
end end
it "registers an offense with `@@x`" do it "registers an offense with `@@x`" do
source = Source.new("@@x =- y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @@x =- y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
end end
it "registers an offense with `X`" do it "registers an offense with `X`" do
source = Source.new("X =- y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid X =- y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "does not register an offense when no mistype assignments" do it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid expect_no_issues subject, <<-CRYSTAL
x = 1 x = 1
x -= y x -= y
x = -y x = -y
CRYSTAL CRYSTAL
end end
end end
context "when using `+`" do context "when using `+`" do
it "registers an offense with `x`" do it "registers an offense with `x`" do
source = Source.new("x =+ y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid x =+ y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "registers an offense with `@x`" do it "registers an offense with `@x`" do
source = Source.new("@x =+ y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @x =+ y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
end end
it "registers an offense with `@@x`" do it "registers an offense with `@@x`" do
source = Source.new("@@x =+ y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @@x =+ y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
end end
it "registers an offense with `X`" do it "registers an offense with `X`" do
source = Source.new("X =+ y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid X =+ y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "does not register an offense when no mistype assignments" do it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid expect_no_issues subject, <<-CRYSTAL
x = 1 x = 1
x += y x += y
x = +y x = +y
CRYSTAL CRYSTAL
end end
end end
context "when using `!`" do context "when using `!`" do
it "registers an offense with `x`" do it "registers an offense with `x`" do
source = Source.new("x =! y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid x =! y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "registers an offense with `@x`" do it "registers an offense with `@x`" do
source = Source.new("@x =! y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @x =! y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
end end
it "registers an offense with `@@x`" do it "registers an offense with `@@x`" do
source = Source.new("@@x =! y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid @@x =! y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
end end
it "registers an offense with `X`" do it "registers an offense with `X`" do
source = Source.new("X =! y", "source.cr") expect_issue subject, <<-CRYSTAL
subject.catch(source).should_not be_valid X =! y
source.issues.size.should eq 1 # ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
end end
it "does not register an offense when no mistype assignments" do it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid expect_no_issues subject, <<-CRYSTAL
x = false x = false
x != y x != y
x = !y x = !y
CRYSTAL CRYSTAL
end end
end end
end end