2014-04-24 14:53:48 +02:00
|
|
|
require_relative 'helper'
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 14:53:48 +02:00
|
|
|
include Vm
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
class TransformTest < MiniTest::Test
|
|
|
|
|
|
|
|
def setup
|
2014-04-27 14:44:34 +02:00
|
|
|
@transform = Parser::Transform.new
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def check_equals
|
|
|
|
is = @transform.apply @input
|
|
|
|
assert_equal @expected.class , is.class
|
|
|
|
end
|
|
|
|
def test_number
|
|
|
|
@input = {:number => '42'}
|
|
|
|
@expected = Vm::NumberExpression.new(42)
|
|
|
|
check_equals
|
|
|
|
assert_equal 42 , @expected.value
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test_name
|
|
|
|
@input = {:name => 'foo'}
|
|
|
|
@expected = Vm::NameExpression.new('foo')
|
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test_argument_list
|
|
|
|
@input = {:args => [{:arg => {:number => '42'}},
|
2014-04-24 14:43:20 +02:00
|
|
|
{:arg => {:name => 'foo'}}]}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = [Vm::NumberExpression.new(42),
|
2014-04-24 16:38:06 +02:00
|
|
|
Vm::NameExpression.new('foo')]
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test_single_argument
|
|
|
|
@input = {:funcall => {:name => 'foo'},
|
2014-04-24 14:43:20 +02:00
|
|
|
:args => [{:arg => {:number => '42'}}]}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = Vm::FuncallExpression.new 'foo', [Vm::NumberExpression.new(42)]
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test_multi_argument
|
|
|
|
@input = {:funcall => {:name => 'baz'},
|
2014-04-24 14:43:20 +02:00
|
|
|
:args => [{:arg => {:number => '42'}},
|
|
|
|
{:arg => {:name => 'foo'}}]}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
|
2014-04-24 16:38:06 +02:00
|
|
|
Vm::NameExpression.new('foo')]
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test_conditional
|
|
|
|
@input = {:cond => {:number => '0'},
|
2014-04-24 14:43:20 +02:00
|
|
|
:if_true => {:body => {:number => '42'}},
|
|
|
|
:if_false => {:body => {:number => '667'}}}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = Vm::ConditionalExpression.new \
|
2014-04-24 16:38:06 +02:00
|
|
|
Vm::NumberExpression.new(0),
|
|
|
|
Vm::NumberExpression.new(42),
|
|
|
|
Vm::NumberExpression.new(667)
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
def test__function_definition
|
|
|
|
@input = {:func => {:name => 'foo'},
|
2014-04-24 14:43:20 +02:00
|
|
|
:params => {:param => {:name => 'x'}},
|
|
|
|
:body => {:number => '5'}}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = Vm::FunctionExpression.new \
|
2014-04-24 14:43:20 +02:00
|
|
|
'foo',
|
2014-04-24 16:38:06 +02:00
|
|
|
[Vm::NameExpression.new('x')],
|
|
|
|
Vm::NumberExpression.new(5)
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|