mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
e850bff60f
* Proof of concept for cyclomatic complexity * Enable configurability of rule * Use the same nodes to increment the complexity as rubocop * Fix typo in test description * Properly indent code and simplify macro * Move metric into metrics * Cover a violation supressed by increased threshold * Extract visitor into its own file * Document cyclomatic complexity rule and visitor * Refactor specs to use a macro * Indent code inside macro * Replace array with tuple for string formatting. `Tuple` is stack based, whereas `Array` is allocated on the heap increasing GC pressure. * Fix formatting * Enable cyclomatic complexity rule by default
47 lines
1.2 KiB
Crystal
47 lines
1.2 KiB
Crystal
require "../../../spec_helper"
|
|
|
|
module Ameba::Rule::Metrics
|
|
subject = CyclomaticComplexity.new
|
|
complex_method = <<-CODE
|
|
def hello(a, b, c)
|
|
if a && b && c
|
|
begin
|
|
while true
|
|
return if false && b
|
|
end
|
|
""
|
|
rescue
|
|
""
|
|
end
|
|
end
|
|
end
|
|
CODE
|
|
|
|
describe CyclomaticComplexity do
|
|
it "passes for empty methods" do
|
|
source = Source.new %(
|
|
def hello
|
|
end
|
|
)
|
|
subject.catch(source).should be_valid
|
|
end
|
|
|
|
it "reports one issue for a complex method" do
|
|
subject.max_complexity = 5
|
|
source = Source.new(complex_method, "source.cr")
|
|
subject.catch(source).should_not be_valid
|
|
|
|
issue = source.issues.first
|
|
issue.rule.should eq subject
|
|
issue.location.to_s.should eq "source.cr:1:1"
|
|
issue.end_location.to_s.should eq "source.cr:12:3"
|
|
issue.message.should eq "Cyclomatic complexity too high [8/5]"
|
|
end
|
|
|
|
it "doesn't report an issue for an increased threshold" do
|
|
subject.max_complexity = 100
|
|
source = Source.new(complex_method, "source.cr")
|
|
subject.catch(source).should be_valid
|
|
end
|
|
end
|
|
end
|