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
|
2014-04-27 17:13:34 +02:00
|
|
|
@input = {:integer => '42'}
|
|
|
|
@expected = Vm::IntegerExpression.new(42)
|
2014-04-24 20:03:11 +02:00
|
|
|
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
|
2014-04-27 20:12:42 +02:00
|
|
|
@input = {:argument_list => [{:argument => {:integer => '42'}},
|
|
|
|
{:argument => {:name => 'foo'}}]}
|
2014-04-27 17:13:34 +02:00
|
|
|
@expected = [Vm::IntegerExpression.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
|
2014-04-27 20:12:42 +02:00
|
|
|
@input = {:function_call => {:name => 'foo'},
|
2014-04-27 20:41:38 +02:00
|
|
|
:argument_list => {:argument => {:integer => '42'} } }
|
2014-04-27 17:13:34 +02:00
|
|
|
@expected = Vm::FuncallExpression.new 'foo', [Vm::IntegerExpression.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
|
2014-04-27 20:12:42 +02:00
|
|
|
@input = {:function_call => {:name => 'baz'},
|
|
|
|
:argument_list => [{:argument => {:integer => '42'}},
|
|
|
|
{:argument => {:name => 'foo'}}]}
|
2014-04-27 17:13:34 +02:00
|
|
|
@expected = Vm::FuncallExpression.new 'baz', [Vm::IntegerExpression.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
|
2014-04-27 20:41:38 +02:00
|
|
|
@input = {:conditional => {:integer => '0'},
|
2014-04-27 20:12:42 +02:00
|
|
|
:if_true => {:block => {:integer => '42'}},
|
|
|
|
:if_false => {:block => {:integer => '667'}}}
|
2014-04-24 20:03:11 +02:00
|
|
|
@expected = Vm::ConditionalExpression.new \
|
2014-04-27 17:13:34 +02:00
|
|
|
Vm::IntegerExpression.new(0),
|
|
|
|
Vm::IntegerExpression.new(42),
|
|
|
|
Vm::IntegerExpression.new(667)
|
2014-04-24 20:03:11 +02:00
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2014-04-27 20:41:38 +02:00
|
|
|
def test_parmeter
|
|
|
|
@input = {:parmeter => { :name => "foo"}}
|
2014-04-27 20:12:42 +02:00
|
|
|
@expected = Vm::NameExpression.new('foo')
|
|
|
|
check_equals
|
|
|
|
end
|
2014-04-27 20:41:38 +02:00
|
|
|
def test_parmeter_list
|
|
|
|
@input = {:parmeter_list => [{:parmeter => { :name => "foo"}}]}
|
2014-04-27 20:12:42 +02:00
|
|
|
@expected = [Vm::NameExpression.new('foo')]
|
|
|
|
check_equals
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_definition
|
|
|
|
@input = {:function_definition => { :name => "foo"},
|
2014-04-27 20:41:38 +02:00
|
|
|
:parmeter_list => { :parmeter => { :name => "x" }} ,
|
2014-04-27 20:12:42 +02:00
|
|
|
:block => { :integer => "5" }}
|
|
|
|
@expected = Vm::FunctionExpression.new('foo', [Vm::NameExpression.new('x')], Vm::IntegerExpression.new(5))
|
|
|
|
check_equals
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|