2018-11-05 18:56:54 +00:00
|
|
|
require "../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::AST
|
|
|
|
describe FlowExpression do
|
|
|
|
describe "#initialize" do
|
|
|
|
it "creates a new flow expression" do
|
|
|
|
node = as_node("return 22")
|
2018-11-22 08:38:32 +00:00
|
|
|
flow_expression = FlowExpression.new node, false
|
2018-11-05 18:56:54 +00:00
|
|
|
flow_expression.node.should_not be_nil
|
2018-11-22 08:38:32 +00:00
|
|
|
flow_expression.in_loop?.should eq false
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#delegation" do
|
|
|
|
it "delegates to_s to @node" do
|
|
|
|
node = as_node("return 22")
|
2018-11-22 08:38:32 +00:00
|
|
|
flow_expression = FlowExpression.new node, false
|
2018-11-05 18:56:54 +00:00
|
|
|
flow_expression.to_s.should eq node.to_s
|
|
|
|
end
|
|
|
|
|
2018-11-24 17:38:13 +00:00
|
|
|
it "delegates locations to @node" do
|
2018-11-05 18:56:54 +00:00
|
|
|
node = as_node %(break if true)
|
2018-11-22 08:38:32 +00:00
|
|
|
flow_expression = FlowExpression.new node, false
|
2018-11-05 18:56:54 +00:00
|
|
|
flow_expression.location.should eq node.location
|
2018-11-24 17:38:13 +00:00
|
|
|
flow_expression.end_location.should eq node.end_location
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 08:38:32 +00:00
|
|
|
describe "#unreachable_nodes" do
|
|
|
|
it "returns unreachable nodes" do
|
2018-11-05 18:56:54 +00:00
|
|
|
nodes = as_nodes %(
|
|
|
|
def foobar
|
|
|
|
return
|
|
|
|
a = 1
|
2018-11-22 08:38:32 +00:00
|
|
|
a = 2
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
)
|
2018-11-22 08:38:32 +00:00
|
|
|
node = nodes.expressions_nodes.first
|
|
|
|
flow_expression = FlowExpression.new node, false
|
|
|
|
flow_expression.unreachable_nodes.should eq nodes.assign_nodes
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
|
2021-11-10 07:11:45 +00:00
|
|
|
it "returns nil if there is no unreachable node after loop" do
|
|
|
|
nodes = as_nodes %(
|
|
|
|
def run
|
|
|
|
idx = items.size - 1
|
|
|
|
while 0 <= idx
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "foo"
|
|
|
|
end
|
|
|
|
)
|
|
|
|
node = nodes.expressions_nodes.first
|
|
|
|
flow_expression = FlowExpression.new node, false
|
|
|
|
flow_expression.unreachable_nodes.empty?.should eq true
|
|
|
|
end
|
|
|
|
|
2018-11-05 18:56:54 +00:00
|
|
|
it "returns nil if there is no unreachable node" do
|
|
|
|
nodes = as_nodes %(
|
|
|
|
def foobar
|
|
|
|
a = 1
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
)
|
2018-11-22 08:38:32 +00:00
|
|
|
node = nodes.expressions_nodes.first
|
|
|
|
flow_expression = FlowExpression.new node, false
|
|
|
|
flow_expression.unreachable_nodes.empty?.should eq true
|
2018-11-05 18:56:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|