2021-10-19 09:44:41 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Lint
|
|
|
|
describe AmbiguousAssignment do
|
|
|
|
subject = AmbiguousAssignment.new
|
|
|
|
|
|
|
|
context "when using `-`" do
|
|
|
|
it "registers an offense with `x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
x =- y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@x =- y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@@x =- y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `X`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
X =- y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not register an offense when no mistype assignments" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
x = 1
|
|
|
|
x -= y
|
|
|
|
x = -y
|
2022-04-04 19:32:46 +00:00
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when using `+`" do
|
|
|
|
it "registers an offense with `x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
x =+ y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@x =+ y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@@x =+ y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `X`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
X =+ y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not register an offense when no mistype assignments" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
x = 1
|
|
|
|
x += y
|
|
|
|
x = +y
|
2022-04-04 19:32:46 +00:00
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when using `!`" do
|
|
|
|
it "registers an offense with `x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
x =! y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@x =! y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `@@x`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
@@x =! y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers an offense with `X`" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
X =! y
|
|
|
|
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not register an offense when no mistype assignments" do
|
2022-04-04 19:32:46 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
x = false
|
|
|
|
x != y
|
|
|
|
x = !y
|
2022-04-04 19:32:46 +00:00
|
|
|
CRYSTAL
|
2021-10-19 09:44:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|