mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Specs cleanup
This commit is contained in:
parent
e6ebca7a5b
commit
8112dddc8f
24 changed files with 267 additions and 251 deletions
|
@ -19,33 +19,33 @@ module Ameba::AST
|
|||
end
|
||||
|
||||
it "is 1 if there is Macro::For" do
|
||||
code = %(
|
||||
def initialize()
|
||||
code = <<-CRYSTAL
|
||||
def initialize
|
||||
{% for c in ALL_NODES %}
|
||||
true || false
|
||||
{% end %}
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
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
|
||||
code = %(
|
||||
def initialize()
|
||||
code = <<-CRYSTAL
|
||||
def initialize
|
||||
{% if foo.bar? %}
|
||||
true || false
|
||||
{% end %}
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
node = Crystal::Parser.new(code).parse
|
||||
visitor = CountingVisitor.new node
|
||||
visitor.count.should eq 1
|
||||
end
|
||||
|
||||
it "increases count for every exhaustive case" do
|
||||
code = %(
|
||||
code = <<-CRYSTAL
|
||||
def hello(a : Int32 | Int64 | Float32 | Float64)
|
||||
case a
|
||||
in Int32 then "int32"
|
||||
|
@ -54,7 +54,7 @@ module Ameba::AST
|
|||
in Float64 then "float64"
|
||||
end
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
node = Crystal::Parser.new(code).parse
|
||||
visitor = CountingVisitor.new node
|
||||
visitor.count.should eq 2
|
||||
|
|
|
@ -6,17 +6,17 @@ module Ameba::AST
|
|||
describe FlowExpressionVisitor do
|
||||
it "creates an expression for return" do
|
||||
rule = FlowExpressionRule.new
|
||||
FlowExpressionVisitor.new rule, Source.new %(
|
||||
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
||||
def foo
|
||||
return :bar
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.expressions.size.should eq 1
|
||||
end
|
||||
|
||||
it "can create multiple expressions" do
|
||||
rule = FlowExpressionRule.new
|
||||
FlowExpressionVisitor.new rule, Source.new %(
|
||||
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
||||
def foo
|
||||
if bar
|
||||
return :baz
|
||||
|
@ -24,42 +24,42 @@ module Ameba::AST
|
|||
return :foobar
|
||||
end
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.expressions.size.should eq 3
|
||||
end
|
||||
|
||||
it "properly creates nested flow expressions" do
|
||||
rule = FlowExpressionRule.new
|
||||
FlowExpressionVisitor.new rule, Source.new %(
|
||||
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
||||
def foo
|
||||
return(
|
||||
return (
|
||||
loop do
|
||||
break if a > 1
|
||||
return a
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.expressions.size.should eq 4
|
||||
end
|
||||
|
||||
it "creates an expression for break" do
|
||||
rule = FlowExpressionRule.new
|
||||
FlowExpressionVisitor.new rule, Source.new %(
|
||||
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
||||
while true
|
||||
break
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.expressions.size.should eq 1
|
||||
end
|
||||
|
||||
it "creates an expression for next" do
|
||||
rule = FlowExpressionRule.new
|
||||
FlowExpressionVisitor.new rule, Source.new %(
|
||||
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
||||
while true
|
||||
next if something
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.expressions.size.should eq 1
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,11 +5,11 @@ module Ameba::AST
|
|||
rule = RedundantControlExpressionRule.new
|
||||
|
||||
describe RedundantControlExpressionVisitor do
|
||||
node = as_node %(
|
||||
node = as_node <<-CRYSTAL
|
||||
a = 1
|
||||
b = 2
|
||||
return a + b
|
||||
)
|
||||
CRYSTAL
|
||||
subject = RedundantControlExpressionVisitor.new(rule, source, node)
|
||||
|
||||
it "assigns valid attributes" do
|
||||
|
|
|
@ -4,37 +4,37 @@ module Ameba::AST
|
|||
describe ScopeVisitor do
|
||||
it "creates a scope for the def" do
|
||||
rule = ScopeRule.new
|
||||
ScopeVisitor.new rule, Source.new %(
|
||||
ScopeVisitor.new rule, Source.new <<-CRYSTAL
|
||||
def method
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.scopes.size.should eq 1
|
||||
end
|
||||
|
||||
it "creates a scope for the proc" do
|
||||
rule = ScopeRule.new
|
||||
ScopeVisitor.new rule, Source.new %(
|
||||
ScopeVisitor.new rule, Source.new <<-CRYSTAL
|
||||
-> {}
|
||||
)
|
||||
CRYSTAL
|
||||
rule.scopes.size.should eq 1
|
||||
end
|
||||
|
||||
it "creates a scope for the block" do
|
||||
rule = ScopeRule.new
|
||||
ScopeVisitor.new rule, Source.new %(
|
||||
ScopeVisitor.new rule, Source.new <<-CRYSTAL
|
||||
3.times {}
|
||||
)
|
||||
CRYSTAL
|
||||
rule.scopes.size.should eq 2
|
||||
end
|
||||
|
||||
context "inner scopes" do
|
||||
it "creates scope for block inside def" do
|
||||
rule = ScopeRule.new
|
||||
ScopeVisitor.new rule, Source.new %(
|
||||
ScopeVisitor.new rule, Source.new <<-CRYSTAL
|
||||
def method
|
||||
3.times {}
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.scopes.size.should eq 2
|
||||
rule.scopes.last.outer_scope.should_not be_nil
|
||||
rule.scopes.first.outer_scope.should eq rule.scopes.last
|
||||
|
@ -42,11 +42,11 @@ module Ameba::AST
|
|||
|
||||
it "creates scope for block inside block" do
|
||||
rule = ScopeRule.new
|
||||
ScopeVisitor.new rule, Source.new %(
|
||||
ScopeVisitor.new rule, Source.new <<-CRYSTAL
|
||||
3.times do
|
||||
2.times {}
|
||||
end
|
||||
)
|
||||
CRYSTAL
|
||||
rule.scopes.size.should eq 3
|
||||
inner_block = rule.scopes.first
|
||||
outer_block = rule.scopes.last
|
||||
|
|
|
@ -4,10 +4,12 @@ module Ameba::AST
|
|||
describe TopLevelNodesVisitor do
|
||||
describe "#require_nodes" do
|
||||
it "returns require node" do
|
||||
source = Source.new %(
|
||||
source = Source.new <<-CRYSTAL
|
||||
require "foo"
|
||||
def bar; end
|
||||
)
|
||||
|
||||
def bar
|
||||
end
|
||||
CRYSTAL
|
||||
visitor = TopLevelNodesVisitor.new(source.ast)
|
||||
visitor.require_nodes.size.should eq 1
|
||||
visitor.require_nodes.first.to_s.should eq %q(require "foo")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue