shard-ameba/spec/ameba/inline_comments_spec.cr

162 lines
5.4 KiB
Crystal
Raw Normal View History

require "../spec_helper"
module Ameba
describe InlineComments do
2020-03-25 16:14:15 +00:00
describe InlineComments::COMMENT_DIRECTIVE_REGEX do
subject = InlineComments::COMMENT_DIRECTIVE_REGEX
it "allows to parse action and rule name" do
result = subject.match("# ameba:enable Group/RuleName")
result = result.should_not be_nil
result["action"].should eq "enable"
result["rules"].should eq "Group/RuleName"
2020-03-25 16:14:15 +00:00
end
it "parses multiple rules" do
result = subject.match("# ameba:enable Group/RuleName, OtherRule, Foo/Bar")
result = result.should_not be_nil
result["action"].should eq "enable"
result["rules"].should eq "Group/RuleName, OtherRule, Foo/Bar"
end
it "fails to parse directives with spaces" do
2020-03-25 16:14:15 +00:00
result = subject.match("# ameba : enable Group/RuleName")
result.should be_nil
2020-03-25 16:14:15 +00:00
end
end
it "disables a rule with a comment directive" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
source.should be_valid
end
it "disables a rule with a line that ends with a comment directive" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
Time.epoch(1483859302) # ameba:disable #{NamedRule.name}
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
source.should be_valid
end
it "does not disable a rule of a different name" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable WrongName
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "Error!")
source.should_not be_valid
end
it "disables a rule if multiple rule names provided" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable SomeRule LargeNumbers #{NamedRule.name} SomeOtherRule
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "")
source.should be_valid
end
it "disables a rule if multiple rule names are separated by comma" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable SomeRule, LargeNumbers, #{NamedRule.name}, SomeOtherRule
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "")
source.should be_valid
end
it "does not disable if multiple rule names used without required one" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable SomeRule, SomeOtherRule LargeNumbers
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "")
source.should_not be_valid
end
it "does not disable if comment directive has wrong place" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
# ameba:disable #{NamedRule.name}
#
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {3, 12}, message: "")
source.should_not be_valid
end
it "does not disable if comment directive added to the wrong line" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
if use_epoch? # ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
end
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {3, 12}, message: "")
source.should_not be_valid
end
it "does not disable if that is not a comment directive" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
"ameba:disable #{NamedRule.name}"
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {3, 12}, message: "")
source.should_not be_valid
end
2018-01-31 17:46:44 +00:00
it "does not disable if that is a commented out directive" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
2018-01-31 17:46:44 +00:00
# # ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {3, 12}, message: "")
source.should_not be_valid
2018-01-31 17:46:44 +00:00
end
it "does not disable if that is an inline commented out directive" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
2018-01-31 17:46:44 +00:00
a = 1 # Disable it: # ameba:disable #{NamedRule.name}
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "")
source.should_not be_valid
2018-01-31 17:46:44 +00:00
end
context "with group name" do
it "disables one rule with a group" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
a = 1 # ameba:disable #{DummyRule.rule_name}
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(DummyRule.new, location: {1, 12}, message: "")
source.should be_valid
end
it "doesn't disable others rules" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
a = 1 # ameba:disable #{DummyRule.rule_name}
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(NamedRule.new, location: {2, 12}, message: "")
source.should_not be_valid
end
it "disables a hole group of rules" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
a = 1 # ameba:disable #{DummyRule.group_name}
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(DummyRule.new, location: {1, 12}, message: "")
source.should be_valid
end
it "does not disable rules which do not belong to the group" do
2022-12-19 17:03:11 +00:00
source = Source.new <<-CRYSTAL
a = 1 # ameba:disable Lint
2022-12-19 17:03:11 +00:00
CRYSTAL
source.add_issue(DummyRule.new, location: {2, 12}, message: "")
source.should_not be_valid
end
end
end
end