mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Hk cyclomatic complexity (#92)
* 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
This commit is contained in:
parent
c4016bd28c
commit
e850bff60f
4 changed files with 149 additions and 0 deletions
39
spec/ameba/ast/visitors/counting_visitor_spec.cr
Normal file
39
spec/ameba/ast/visitors/counting_visitor_spec.cr
Normal file
|
@ -0,0 +1,39 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::AST
|
||||
describe CountingVisitor do
|
||||
describe "#visit" do
|
||||
it "allow to visit ASTNode" do
|
||||
node = Crystal::Parser.new("").parse
|
||||
visitor = CountingVisitor.new node
|
||||
node.accept visitor
|
||||
end
|
||||
end
|
||||
|
||||
describe "#count" do
|
||||
it "is 1 for an empty method" do
|
||||
node = Crystal::Parser.new("def hello; end").parse
|
||||
visitor = CountingVisitor.new node
|
||||
|
||||
visitor.count.should eq 1
|
||||
end
|
||||
|
||||
{% for pair in [
|
||||
{code: "if true; end", description: "conditional"},
|
||||
{code: "while true; end", description: "while loop"},
|
||||
{code: "until 1 < 2; end", description: "until loop"},
|
||||
{code: "begin; rescue; end", description: "rescue"},
|
||||
{code: "case 1 when 1; end", description: "when"},
|
||||
{code: "true || false", description: "or"},
|
||||
{code: "true && false", description: "and"},
|
||||
] %}
|
||||
it "increases count for every {{ pair[:description].id }}" do
|
||||
node = Crystal::Parser.new("def hello; {{ pair[:code].id }} end").parse
|
||||
visitor = CountingVisitor.new node
|
||||
|
||||
visitor.count.should eq 2
|
||||
end
|
||||
{% end %}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue