2014-04-24 16:38:06 +02:00
|
|
|
require_relative "helper"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
|
2014-04-28 15:26:19 +02:00
|
|
|
# testing that parsing strings that we know to be correct returns the nodes we expect
|
2014-04-24 18:45:22 +02:00
|
|
|
# in a way the combination of test_parser and test_transform
|
2014-04-24 16:17:17 +02:00
|
|
|
|
2014-04-28 15:26:19 +02:00
|
|
|
require_relative 'helper'
|
|
|
|
|
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-28 15:26:19 +02:00
|
|
|
|
|
|
|
def check
|
|
|
|
syntax = @parser.parse(@input)
|
2014-04-24 18:45:22 +02:00
|
|
|
tree = @transform.apply(syntax)
|
2014-04-28 15:26:19 +02:00
|
|
|
assert tree
|
|
|
|
assert_equal @expected , tree
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-24 18:45:22 +02:00
|
|
|
|
|
|
|
def test_number
|
2014-04-28 15:26:19 +02:00
|
|
|
@input = '42 '
|
|
|
|
@expected = Vm::IntegerExpression.new(42)
|
|
|
|
@parser = @parser.integer
|
|
|
|
check
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-28 15:26:19 +02:00
|
|
|
|
|
|
|
def test_name
|
|
|
|
@input = 'foo '
|
|
|
|
@expected = Vm::NameExpression.new('foo')
|
|
|
|
@parser = @parser.name
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_one_argument
|
|
|
|
@input = '(42)'
|
|
|
|
@expected = { :argument_list => Vm::IntegerExpression.new(42) }
|
|
|
|
@parser = @parser.argument_list
|
|
|
|
check
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|
2014-04-28 15:26:19 +02:00
|
|
|
|
|
|
|
def test_argument_list
|
|
|
|
@input = '(42, foo)'
|
|
|
|
@expected = [Vm::IntegerExpression.new(42),
|
|
|
|
Vm::NameExpression.new('foo')]
|
2014-04-27 20:12:42 +02:00
|
|
|
@parser = @parser.argument_list
|
2014-04-28 15:26:19 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_call
|
|
|
|
@input = 'baz(42, foo)'
|
|
|
|
@expected = Vm::FuncallExpression.new 'baz', [Vm::IntegerExpression.new(42),
|
|
|
|
Vm::NameExpression.new('foo')]
|
|
|
|
|
|
|
|
@parser = @parser.function_call
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expression_else
|
|
|
|
@input = <<HERE
|
|
|
|
4
|
|
|
|
5
|
|
|
|
else
|
|
|
|
HERE
|
|
|
|
@expected = {:expressions=>[ Vm::IntegerExpression.new(4), Vm::IntegerExpression.new(5)]}
|
|
|
|
|
|
|
|
@parser = @parser.expressions_else
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expression_end
|
|
|
|
@input = <<HERE
|
|
|
|
5
|
|
|
|
name
|
|
|
|
call(4,6)
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expected = {:expressions=> [Vm::IntegerExpression.new(5), Vm::NameExpression.new("name"),
|
|
|
|
Vm::FuncallExpression.new("call", [Vm::IntegerExpression.new(4), Vm::IntegerExpression.new(6) ]) ] }
|
|
|
|
@parser = @parser.expressions_end
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_conditional
|
|
|
|
@input = <<HERE
|
|
|
|
if (0)
|
|
|
|
42
|
|
|
|
else
|
|
|
|
667
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expected = Vm::ConditionalExpression.new( Vm::IntegerExpression.new(0),
|
|
|
|
[Vm::IntegerExpression.new(42)],
|
|
|
|
[Vm::IntegerExpression.new(667)])
|
|
|
|
@parser = @parser.conditional
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_definition
|
|
|
|
@input = <<HERE
|
|
|
|
def foo(x)
|
2014-04-27 20:12:42 +02:00
|
|
|
5
|
2014-04-28 15:26:19 +02:00
|
|
|
end
|
2014-04-27 20:12:42 +02:00
|
|
|
HERE
|
2014-04-28 15:26:19 +02:00
|
|
|
@expected = Vm::FunctionExpression.new('foo',
|
|
|
|
[Vm::NameExpression.new('x')],
|
|
|
|
[Vm::IntegerExpression.new(5)])
|
2014-04-27 20:12:42 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 15:26:19 +02:00
|
|
|
check
|
2014-04-27 20:12:42 +02:00
|
|
|
end
|
2014-04-24 16:17:17 +02:00
|
|
|
end
|