2018-11-05 18:56:54 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::AST
|
|
|
|
source = Source.new ""
|
|
|
|
|
|
|
|
describe FlowExpressionVisitor do
|
|
|
|
it "creates an expression for return" do
|
|
|
|
rule = FlowExpressionRule.new
|
2022-12-19 17:03:11 +00:00
|
|
|
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
def foo
|
|
|
|
return :bar
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
rule.expressions.size.should eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can create multiple expressions" do
|
|
|
|
rule = FlowExpressionRule.new
|
2022-12-19 17:03:11 +00:00
|
|
|
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
def foo
|
|
|
|
if bar
|
|
|
|
return :baz
|
|
|
|
else
|
|
|
|
return :foobar
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-11-22 08:38:32 +00:00
|
|
|
rule.expressions.size.should eq 3
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "properly creates nested flow expressions" do
|
|
|
|
rule = FlowExpressionRule.new
|
2022-12-19 17:03:11 +00:00
|
|
|
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
def foo
|
2022-12-19 17:03:11 +00:00
|
|
|
return (
|
2018-11-05 18:56:54 +00:00
|
|
|
loop do
|
|
|
|
break if a > 1
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-11-22 08:38:32 +00:00
|
|
|
rule.expressions.size.should eq 4
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an expression for break" do
|
|
|
|
rule = FlowExpressionRule.new
|
2022-12-19 17:03:11 +00:00
|
|
|
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
while true
|
|
|
|
break
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
rule.expressions.size.should eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an expression for next" do
|
|
|
|
rule = FlowExpressionRule.new
|
2022-12-19 17:03:11 +00:00
|
|
|
FlowExpressionVisitor.new rule, Source.new <<-CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
while true
|
|
|
|
next if something
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-11-05 18:56:54 +00:00
|
|
|
rule.expressions.size.should eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|