2019-02-16 19:03:44 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Metrics
|
|
|
|
subject = CyclomaticComplexity.new
|
2022-12-08 12:57:46 +00:00
|
|
|
complex_method = <<-CRYSTAL
|
2019-02-16 19:03:44 +00:00
|
|
|
def hello(a, b, c)
|
|
|
|
if a && b && c
|
|
|
|
begin
|
|
|
|
while true
|
|
|
|
return if false && b
|
|
|
|
end
|
|
|
|
""
|
|
|
|
rescue
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-08 12:57:46 +00:00
|
|
|
CRYSTAL
|
2019-02-16 19:03:44 +00:00
|
|
|
|
|
|
|
describe CyclomaticComplexity do
|
|
|
|
it "passes for empty methods" do
|
2022-04-04 20:15:43 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2019-02-16 19:03:44 +00:00
|
|
|
def hello
|
|
|
|
end
|
2022-04-04 20:15:43 +00:00
|
|
|
CRYSTAL
|
2019-02-16 19:03:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports one issue for a complex method" do
|
2022-04-04 20:15:43 +00:00
|
|
|
rule = CyclomaticComplexity.new
|
|
|
|
rule.max_complexity = 5
|
|
|
|
|
2019-02-16 19:03:44 +00:00
|
|
|
source = Source.new(complex_method, "source.cr")
|
2022-04-04 20:15:43 +00:00
|
|
|
rule.catch(source).should_not be_valid
|
2019-02-16 19:03:44 +00:00
|
|
|
|
|
|
|
issue = source.issues.first
|
2022-04-04 20:15:43 +00:00
|
|
|
issue.rule.should eq rule
|
2019-07-12 20:33:08 +00:00
|
|
|
issue.location.to_s.should eq "source.cr:1:5"
|
2021-11-16 21:30:33 +00:00
|
|
|
issue.end_location.to_s.should eq "source.cr:1:9"
|
2019-02-16 19:03:44 +00:00
|
|
|
issue.message.should eq "Cyclomatic complexity too high [8/5]"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report an issue for an increased threshold" do
|
2022-04-04 20:15:43 +00:00
|
|
|
rule = CyclomaticComplexity.new
|
|
|
|
rule.max_complexity = 100
|
|
|
|
|
|
|
|
expect_no_issues rule, complex_method
|
2019-02-16 19:03:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|