rewrote as testcase (quicker than checking where spec errors came from)
This commit is contained in:
parent
3f316cda63
commit
93e4178017
@ -1,76 +1,78 @@
|
|||||||
require_relative 'helper'
|
require_relative 'helper'
|
||||||
require 'minitest/spec'
|
|
||||||
require 'vm/transform'
|
require 'vm/transform'
|
||||||
|
|
||||||
include Vm
|
include Vm
|
||||||
|
|
||||||
describe Transform do
|
class TransformTest < MiniTest::Test
|
||||||
before do
|
|
||||||
|
def setup
|
||||||
@transform = Vm::Transform.new
|
@transform = Vm::Transform.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a number' do
|
def check_equals
|
||||||
input = {:number => '42'}
|
is = @transform.apply @input
|
||||||
expected = Vm::NumberExpression.new(42)
|
assert_equal @expected.class , is.class
|
||||||
|
end
|
||||||
@transform.apply(input).must_equal expected
|
def test_number
|
||||||
|
@input = {:number => '42'}
|
||||||
|
@expected = Vm::NumberExpression.new(42)
|
||||||
|
check_equals
|
||||||
|
assert_equal 42 , @expected.value
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a name' do
|
def test_name
|
||||||
input = {:name => 'foo'}
|
@input = {:name => 'foo'}
|
||||||
expected = Vm::NameExpression.new('foo')
|
@expected = Vm::NameExpression.new('foo')
|
||||||
|
check_equals
|
||||||
@transform.apply(input).must_equal expected
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms an argument list' do
|
def test_argument_list
|
||||||
input = {:args => [{:arg => {:number => '42'}},
|
@input = {:args => [{:arg => {:number => '42'}},
|
||||||
{:arg => {:name => 'foo'}}]}
|
{:arg => {:name => 'foo'}}]}
|
||||||
expected = [Vm::NumberExpression.new(42),
|
@expected = [Vm::NumberExpression.new(42),
|
||||||
Vm::NameExpression.new('foo')]
|
Vm::NameExpression.new('foo')]
|
||||||
|
check_equals
|
||||||
@transform.apply(input).must_equal expected
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a single-argument function call' do
|
def test_single_argument
|
||||||
input = {:funcall => {:name => 'foo'},
|
@input = {:funcall => {:name => 'foo'},
|
||||||
:args => [{:arg => {:number => '42'}}]}
|
:args => [{:arg => {:number => '42'}}]}
|
||||||
expected = Vm::FuncallExpression.new 'foo', [Vm::NumberExpression.new(42)]
|
@expected = Vm::FuncallExpression.new 'foo', [Vm::NumberExpression.new(42)]
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
check_equals
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a multi-argument function call' do
|
def test_multi_argument
|
||||||
input = {:funcall => {:name => 'baz'},
|
@input = {:funcall => {:name => 'baz'},
|
||||||
:args => [{:arg => {:number => '42'}},
|
:args => [{:arg => {:number => '42'}},
|
||||||
{:arg => {:name => 'foo'}}]}
|
{:arg => {:name => 'foo'}}]}
|
||||||
expected = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
|
@expected = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
|
||||||
Vm::NameExpression.new('foo')]
|
Vm::NameExpression.new('foo')]
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
check_equals
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a conditional' do
|
def test_conditional
|
||||||
input = {:cond => {:number => '0'},
|
@input = {:cond => {:number => '0'},
|
||||||
:if_true => {:body => {:number => '42'}},
|
:if_true => {:body => {:number => '42'}},
|
||||||
:if_false => {:body => {:number => '667'}}}
|
:if_false => {:body => {:number => '667'}}}
|
||||||
expected = Vm::ConditionalExpression.new \
|
@expected = Vm::ConditionalExpression.new \
|
||||||
Vm::NumberExpression.new(0),
|
Vm::NumberExpression.new(0),
|
||||||
Vm::NumberExpression.new(42),
|
Vm::NumberExpression.new(42),
|
||||||
Vm::NumberExpression.new(667)
|
Vm::NumberExpression.new(667)
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
check_equals
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a function definition' do
|
def test__function_definition
|
||||||
input = {:func => {:name => 'foo'},
|
@input = {:func => {:name => 'foo'},
|
||||||
:params => {:param => {:name => 'x'}},
|
:params => {:param => {:name => 'x'}},
|
||||||
:body => {:number => '5'}}
|
:body => {:number => '5'}}
|
||||||
expected = Vm::FunctionExpression.new \
|
@expected = Vm::FunctionExpression.new \
|
||||||
'foo',
|
'foo',
|
||||||
[Vm::NameExpression.new('x')],
|
[Vm::NameExpression.new('x')],
|
||||||
Vm::NumberExpression.new(5)
|
Vm::NumberExpression.new(5)
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
check_equals
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user