2014-04-24 16:38:06 +02:00
|
|
|
require_relative "helper"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
|
2014-04-24 18:45:22 +02:00
|
|
|
#testing that parsing strings that we know to be correct returns the nodes we expect
|
|
|
|
# in a way the combination of test_parser and test_transform
|
2014-04-24 16:17:17 +02:00
|
|
|
|
2014-04-27 14:56:22 +02:00
|
|
|
class TestNodes < MiniTest::Test
|
2014-04-24 16:38:06 +02:00
|
|
|
|
2014-04-24 18:45:22 +02:00
|
|
|
def setup
|
2014-04-27 15:30:32 +02:00
|
|
|
@parser = Parser::Composed.new
|
2014-04-27 14:44:34 +02:00
|
|
|
@transform = Parser::Transform.new
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-24 18:45:22 +02:00
|
|
|
|
|
|
|
def parse string
|
|
|
|
syntax = @parser.parse(string)
|
|
|
|
tree = @transform.apply(syntax)
|
|
|
|
tree
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-24 18:45:22 +02:00
|
|
|
|
|
|
|
def test_number
|
|
|
|
tree = parse "42"
|
2014-04-27 17:13:34 +02:00
|
|
|
assert_kind_of Vm::IntegerExpression , tree
|
2014-04-24 18:45:22 +02:00
|
|
|
assert_equal 42 , tree.value
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-24 18:45:22 +02:00
|
|
|
def test_args
|
|
|
|
tree = parse "( 42 )"
|
2014-04-27 15:09:22 +02:00
|
|
|
assert_kind_of Hash , tree
|
2014-04-27 20:12:42 +02:00
|
|
|
assert_kind_of Vm::IntegerExpression , tree[:argument_list]
|
|
|
|
assert_equal 42 , tree[:argument_list].value
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-25 10:56:53 +02:00
|
|
|
def test_arg_list
|
2014-04-27 20:12:42 +02:00
|
|
|
@parser = @parser.argument_list
|
2014-04-25 10:56:53 +02:00
|
|
|
tree = parse "(42, foo)"
|
2014-04-27 15:09:22 +02:00
|
|
|
assert_instance_of Array , tree
|
2014-04-25 10:56:53 +02:00
|
|
|
assert_equal 42 , tree.first.value
|
|
|
|
assert_equal "foo" , tree.last.name
|
|
|
|
end
|
2014-04-27 20:12:42 +02:00
|
|
|
def test_definition
|
|
|
|
input = <<HERE
|
|
|
|
def foo(x) {
|
|
|
|
5
|
|
|
|
}
|
|
|
|
HERE
|
|
|
|
@parser = @parser.function_definition
|
|
|
|
tree = parse(input)
|
2014-04-27 20:41:38 +02:00
|
|
|
assert_kind_of Vm::FunctionExpression , tree
|
2014-04-27 20:12:42 +02:00
|
|
|
end
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 14:43:20 +02:00
|
|
|
|