From 390b26d7f15dad345707ded997c909f2757015d8 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 4 Apr 2022 21:32:46 +0200 Subject: [PATCH] Use issue expectation helpers in `Lint::AmbiguousAssignment` rule spec --- .../rule/lint/ambiguous_assignment_spec.cr | 156 ++++++------------ 1 file changed, 54 insertions(+), 102 deletions(-) diff --git a/spec/ameba/rule/lint/ambiguous_assignment_spec.cr b/spec/ameba/rule/lint/ambiguous_assignment_spec.cr index 153f1796..a808df40 100644 --- a/spec/ameba/rule/lint/ambiguous_assignment_spec.cr +++ b/spec/ameba/rule/lint/ambiguous_assignment_spec.cr @@ -6,163 +6,115 @@ module Ameba::Rule::Lint context "when using `-`" do it "registers an offense with `x`" do - source = Source.new("x =- y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + x =- y + # ^^ error: Suspicious assignment detected. Did you mean `-=`? + CRYSTAL end it "registers an offense with `@x`" do - source = Source.new("@x =- y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @x =- y + # ^^ error: Suspicious assignment detected. Did you mean `-=`? + CRYSTAL end it "registers an offense with `@@x`" do - source = Source.new("@@x =- y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @@x =- y + # ^^ error: Suspicious assignment detected. Did you mean `-=`? + CRYSTAL end it "registers an offense with `X`" do - source = Source.new("X =- y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + X =- y + # ^^ error: Suspicious assignment detected. Did you mean `-=`? + CRYSTAL end 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 -= y x = -y - CRYSTAL + CRYSTAL end end context "when using `+`" do it "registers an offense with `x`" do - source = Source.new("x =+ y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + x =+ y + # ^^ error: Suspicious assignment detected. Did you mean `+=`? + CRYSTAL end it "registers an offense with `@x`" do - source = Source.new("@x =+ y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @x =+ y + # ^^ error: Suspicious assignment detected. Did you mean `+=`? + CRYSTAL end it "registers an offense with `@@x`" do - source = Source.new("@@x =+ y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @@x =+ y + # ^^ error: Suspicious assignment detected. Did you mean `+=`? + CRYSTAL end it "registers an offense with `X`" do - source = Source.new("X =+ y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + X =+ y + # ^^ error: Suspicious assignment detected. Did you mean `+=`? + CRYSTAL end 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 += y x = +y - CRYSTAL + CRYSTAL end end context "when using `!`" do it "registers an offense with `x`" do - source = Source.new("x =! y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + x =! y + # ^^ error: Suspicious assignment detected. Did you mean `!=`? + CRYSTAL end it "registers an offense with `@x`" do - source = Source.new("@x =! y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @x =! y + # ^^ error: Suspicious assignment detected. Did you mean `!=`? + CRYSTAL end it "registers an offense with `@@x`" do - source = Source.new("@@x =! y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + @@x =! y + # ^^ error: Suspicious assignment detected. Did you mean `!=`? + CRYSTAL end it "registers an offense with `X`" do - source = Source.new("X =! y", "source.cr") - subject.catch(source).should_not be_valid - source.issues.size.should eq 1 - - 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" + expect_issue subject, <<-CRYSTAL + X =! y + # ^^ error: Suspicious assignment detected. Did you mean `!=`? + CRYSTAL end 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 != y x = !y - CRYSTAL + CRYSTAL end end end