2014-04-24 15:53:48 +03:00
|
|
|
require_relative 'helper'
|
2014-04-24 15:43:20 +03:00
|
|
|
require 'minitest/spec'
|
2014-04-24 15:53:48 +03:00
|
|
|
require 'vm/transform'
|
2014-04-24 15:43:20 +03:00
|
|
|
|
2014-04-24 15:53:48 +03:00
|
|
|
include Vm
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
describe Transform do
|
|
|
|
before do
|
2014-04-24 15:53:48 +03:00
|
|
|
@transform = Vm::Transform.new
|
2014-04-24 15:43:20 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a number' do
|
|
|
|
input = {:number => '42'}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::NumberExpression.new(42)
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a name' do
|
|
|
|
input = {:name => 'foo'}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::NameExpression.new('foo')
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms an argument list' do
|
|
|
|
input = {:args => [{:arg => {:number => '42'}},
|
|
|
|
{:arg => {:name => 'foo'}}]}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = [Vm::NumberExpression.new(42),
|
|
|
|
Vm::NameExpression.new('foo')]
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a single-argument function call' do
|
|
|
|
input = {:funcall => {:name => 'foo'},
|
|
|
|
:args => [{:arg => {:number => '42'}}]}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::FuncallExpression.new 'foo', [Vm::NumberExpression.new(42)]
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a multi-argument function call' do
|
|
|
|
input = {:funcall => {:name => 'baz'},
|
|
|
|
:args => [{:arg => {:number => '42'}},
|
|
|
|
{:arg => {:name => 'foo'}}]}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
|
|
|
|
Vm::NameExpression.new('foo')]
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a conditional' do
|
|
|
|
input = {:cond => {:number => '0'},
|
|
|
|
:if_true => {:body => {:number => '42'}},
|
|
|
|
:if_false => {:body => {:number => '667'}}}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::ConditionalExpression.new \
|
|
|
|
Vm::NumberExpression.new(0),
|
|
|
|
Vm::NumberExpression.new(42),
|
|
|
|
Vm::NumberExpression.new(667)
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a function definition' do
|
|
|
|
input = {:func => {:name => 'foo'},
|
|
|
|
:params => {:param => {:name => 'x'}},
|
|
|
|
:body => {:number => '5'}}
|
2014-04-24 17:38:06 +03:00
|
|
|
expected = Vm::FunctionExpression.new \
|
2014-04-24 15:43:20 +03:00
|
|
|
'foo',
|
2014-04-24 17:38:06 +03:00
|
|
|
[Vm::NameExpression.new('x')],
|
|
|
|
Vm::NumberExpression.new(5)
|
2014-04-24 15:43:20 +03:00
|
|
|
|
|
|
|
@transform.apply(input).must_equal expected
|
|
|
|
end
|
|
|
|
end
|