2018-05-03 15:57:47 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::AST
|
|
|
|
describe Assignment do
|
|
|
|
node = Crystal::NilLiteral.new
|
|
|
|
scope = Scope.new as_node "foo = 1"
|
|
|
|
variable = Variable.new(Crystal::Var.new("foo"), scope)
|
|
|
|
|
|
|
|
describe "#initialize" do
|
|
|
|
it "creates a new assignment with node and var" do
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(node, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.node.should_not be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#reference=" do
|
|
|
|
it "creates a new reference" do
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(node, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.referenced = true
|
|
|
|
assignment.referenced?.should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delegation" do
|
2018-11-24 17:38:13 +00:00
|
|
|
it "delegates locations" do
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(node, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.location.should eq node.location
|
2018-11-24 17:38:13 +00:00
|
|
|
assignment.end_location.should eq node.end_location
|
2018-05-03 15:57:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates to_s" do
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(node, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.to_s.should eq node.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates scope" do
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(node, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.scope.should eq variable.scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#branch" do
|
|
|
|
it "returns the branch of the assignment" do
|
2022-12-19 17:03:11 +00:00
|
|
|
nodes = as_nodes <<-CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
def method(a)
|
|
|
|
if a
|
|
|
|
a = 3 # --> Crystal::Expressions
|
|
|
|
puts a
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
scope = Scope.new nodes.def_nodes.first
|
|
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
|
2022-10-28 22:03:39 +00:00
|
|
|
branch = assignment.branch.should_not be_nil
|
|
|
|
branch.node.class.should eq Crystal::Expressions
|
2018-05-03 15:57:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns inner branch" do
|
2022-12-19 17:03:11 +00:00
|
|
|
nodes = as_nodes <<-CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
def method(a, b)
|
|
|
|
if a
|
|
|
|
if b
|
|
|
|
a = 3 # --> Crystal::Assign
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
scope = Scope.new nodes.def_nodes.first
|
|
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
|
2022-10-28 22:03:39 +00:00
|
|
|
branch = assignment.branch.should_not be_nil
|
|
|
|
branch.node.class.should eq Crystal::Assign
|
2018-05-03 15:57:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil if assignment does not have a branch" do
|
2022-12-19 17:03:11 +00:00
|
|
|
nodes = as_nodes <<-CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
def method(a)
|
|
|
|
a = 2
|
|
|
|
end
|
2022-12-19 17:03:11 +00:00
|
|
|
CRYSTAL
|
2018-05-03 15:57:47 +00:00
|
|
|
scope = Scope.new nodes.def_nodes.first
|
|
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
2020-03-24 18:10:56 +00:00
|
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
|
2018-05-03 15:57:47 +00:00
|
|
|
assignment.branch.should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|