2019-02-16 19:03:44 +00:00
|
|
|
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
|
|
|
|
|
2019-03-31 17:27:20 +00:00
|
|
|
it "is 1 if there is Macro::For" do
|
2022-12-19 17:03:11 +00:00
|
|
|
code = <<-CRYSTAL
|
|
|
|
def initialize
|
2019-03-31 17:27:20 +00:00
|
|
|
{% for c in ALL_NODES %}
|
|
|
|
true || false
|
|
|
|
{% end %}
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2019-03-31 17:27:20 +00:00
|
|
|
node = Crystal::Parser.new(code).parse
|
|
|
|
visitor = CountingVisitor.new node
|
|
|
|
visitor.count.should eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is 1 if there is Macro::If" do
|
2022-12-19 17:03:11 +00:00
|
|
|
code = <<-CRYSTAL
|
|
|
|
def initialize
|
2019-03-31 17:27:20 +00:00
|
|
|
{% if foo.bar? %}
|
|
|
|
true || false
|
|
|
|
{% end %}
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2019-03-31 17:27:20 +00:00
|
|
|
node = Crystal::Parser.new(code).parse
|
|
|
|
visitor = CountingVisitor.new node
|
|
|
|
visitor.count.should eq 1
|
|
|
|
end
|
|
|
|
|
2021-06-05 11:29:57 +00:00
|
|
|
it "increases count for every exhaustive case" do
|
2022-12-19 17:03:11 +00:00
|
|
|
code = <<-CRYSTAL
|
2021-06-05 11:29:57 +00:00
|
|
|
def hello(a : Int32 | Int64 | Float32 | Float64)
|
|
|
|
case a
|
2021-06-05 18:49:00 +00:00
|
|
|
in Int32 then "int32"
|
|
|
|
in Int64 then "int64"
|
2021-06-05 11:29:57 +00:00
|
|
|
in Float32 then "float32"
|
|
|
|
in Float64 then "float64"
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2021-06-05 11:29:57 +00:00
|
|
|
node = Crystal::Parser.new(code).parse
|
|
|
|
visitor = CountingVisitor.new node
|
|
|
|
visitor.count.should eq 2
|
|
|
|
end
|
|
|
|
|
2019-02-16 19:03:44 +00:00
|
|
|
{% 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: "true || false", description: "or"},
|
|
|
|
{code: "true && false", description: "and"},
|
2021-06-05 11:29:57 +00:00
|
|
|
{
|
|
|
|
code: "a : String | Int32 = 1; case a when true; end",
|
|
|
|
description: "inexhaustive when",
|
|
|
|
},
|
2019-02-16 19:03:44 +00:00
|
|
|
] %}
|
|
|
|
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
|