2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2018-01-31 17:46:44 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2018-01-31 17:46:44 +00:00
|
|
|
describe UnneededDisableDirective do
|
|
|
|
subject = UnneededDisableDirective.new
|
|
|
|
|
|
|
|
it "passes if there are no comments" do
|
2022-04-04 19:05:33 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
a = 1
|
2022-04-04 19:05:33 +00:00
|
|
|
CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes if there is disable directive" do
|
2022-04-04 19:05:33 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
a = 1 # my super var
|
2022-04-04 19:05:33 +00:00
|
|
|
CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
|
2018-05-08 21:14:46 +00:00
|
|
|
it "doesn't report if there is disable directive and it is needed" do
|
|
|
|
s = Source.new %Q(
|
|
|
|
# ameba:disable #{NamedRule.name}
|
|
|
|
a = 1
|
|
|
|
)
|
2018-09-07 12:07:03 +00:00
|
|
|
s.add_issue NamedRule.new, location: {2, 1},
|
2018-06-10 21:15:12 +00:00
|
|
|
message: "Useless assignment", status: :disabled
|
2018-05-08 21:14:46 +00:00
|
|
|
subject.catch(s).should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes if there is inline disable directive and it is needed" do
|
2018-01-31 17:46:44 +00:00
|
|
|
s = Source.new %Q(
|
|
|
|
a = 1 # ameba:disable #{NamedRule.name}
|
|
|
|
)
|
2018-09-07 12:07:03 +00:00
|
|
|
s.add_issue NamedRule.new, location: {1, 1},
|
2018-06-10 21:15:12 +00:00
|
|
|
message: "Alarm!", status: :disabled
|
2018-01-31 17:46:44 +00:00
|
|
|
subject.catch(s).should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores commented out disable directive" do
|
|
|
|
s = Source.new %Q(
|
|
|
|
# # ameba:disable #{NamedRule.name}
|
|
|
|
a = 1
|
|
|
|
)
|
2018-06-10 21:15:12 +00:00
|
|
|
s.add_issue NamedRule.new, location: {3, 1},
|
|
|
|
message: "Alarm!", status: :disabled
|
2018-01-31 17:46:44 +00:00
|
|
|
subject.catch(s).should be_valid
|
|
|
|
end
|
|
|
|
|
2018-05-03 15:57:47 +00:00
|
|
|
it "fails if there is unneeded directive" do
|
2022-04-04 19:05:33 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
# ameba:disable #{NamedRule.name}
|
2022-04-04 19:05:33 +00:00
|
|
|
# ^{} error: Unnecessary disabling of #{NamedRule.name}
|
2018-01-31 17:46:44 +00:00
|
|
|
a = 1
|
2022-04-04 19:05:33 +00:00
|
|
|
CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is inline unneeded directive" do
|
2022-04-04 19:05:33 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
a = 1 # ameba:disable #{NamedRule.name}
|
|
|
|
# ^ error: Unnecessary disabling of #{NamedRule.name}
|
|
|
|
CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "detects mixed inline directives" do
|
2022-04-04 19:05:33 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
# ameba:disable Rule1, Rule2
|
2022-04-04 19:05:33 +00:00
|
|
|
# ^{} error: Unnecessary disabling of Rule1, Rule2
|
2018-01-31 17:46:44 +00:00
|
|
|
a = 1 # ameba:disable Rule3
|
2022-04-04 19:05:33 +00:00
|
|
|
# ^ error: Unnecessary disabling of Rule3
|
|
|
|
CRYSTAL
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
|
2018-02-02 08:52:21 +00:00
|
|
|
it "fails if there is disabled UnneededDisableDirective" do
|
|
|
|
s = Source.new %Q(
|
2018-02-02 20:11:18 +00:00
|
|
|
# ameba:disable #{UnneededDisableDirective.rule_name}
|
2018-02-02 08:52:21 +00:00
|
|
|
a = 1
|
|
|
|
), "source.cr"
|
2018-06-10 21:15:12 +00:00
|
|
|
s.add_issue UnneededDisableDirective.new, location: {3, 1},
|
|
|
|
message: "Alarm!", status: :disabled
|
2018-02-02 08:52:21 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
|
|
|
end
|
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
it "reports issue, location and message" do
|
2018-01-31 17:46:44 +00:00
|
|
|
s = Source.new %Q(
|
|
|
|
# ameba:disable Rule1, Rule2
|
|
|
|
a = 1
|
|
|
|
), "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:1:1"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq "Unnecessary disabling of Rule1, Rule2"
|
2018-01-31 17:46:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|