imroved and fixed node test (parse and transform)
This commit is contained in:
parent
cfcb9ded2f
commit
2fdbb9f3ae
@ -1,9 +1,11 @@
|
|||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
|
||||||
|
|
||||||
#testing that parsing strings that we know to be correct returns the nodes we expect
|
# testing that parsing strings that we know to be correct returns the nodes we expect
|
||||||
# in a way the combination of test_parser and test_transform
|
# in a way the combination of test_parser and test_transform
|
||||||
|
|
||||||
|
require_relative 'helper'
|
||||||
|
|
||||||
class TestNodes < MiniTest::Test
|
class TestNodes < MiniTest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -11,40 +13,101 @@ class TestNodes < MiniTest::Test
|
|||||||
@transform = Parser::Transform.new
|
@transform = Parser::Transform.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse string
|
def check
|
||||||
syntax = @parser.parse(string)
|
syntax = @parser.parse(@input)
|
||||||
tree = @transform.apply(syntax)
|
tree = @transform.apply(syntax)
|
||||||
tree
|
assert tree
|
||||||
|
assert_equal @expected , tree
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_number
|
def test_number
|
||||||
tree = parse "42"
|
@input = '42 '
|
||||||
assert_kind_of Vm::IntegerExpression , tree
|
@expected = Vm::IntegerExpression.new(42)
|
||||||
assert_equal 42 , tree.value
|
@parser = @parser.integer
|
||||||
|
check
|
||||||
end
|
end
|
||||||
def test_args
|
|
||||||
tree = parse "( 42 )"
|
def test_name
|
||||||
assert_kind_of Hash , tree
|
@input = 'foo '
|
||||||
assert_kind_of Vm::IntegerExpression , tree[:argument_list]
|
@expected = Vm::NameExpression.new('foo')
|
||||||
assert_equal 42 , tree[:argument_list].value
|
@parser = @parser.name
|
||||||
|
check
|
||||||
end
|
end
|
||||||
def test_arg_list
|
|
||||||
|
def test_one_argument
|
||||||
|
@input = '(42)'
|
||||||
|
@expected = { :argument_list => Vm::IntegerExpression.new(42) }
|
||||||
@parser = @parser.argument_list
|
@parser = @parser.argument_list
|
||||||
tree = parse "(42, foo)"
|
check
|
||||||
assert_instance_of Array , tree
|
|
||||||
assert_equal 42 , tree.first.value
|
|
||||||
assert_equal "foo" , tree.last.name
|
|
||||||
end
|
end
|
||||||
def test_definition
|
|
||||||
input = <<HERE
|
def test_argument_list
|
||||||
def foo(x) {
|
@input = '(42, foo)'
|
||||||
5
|
@expected = [Vm::IntegerExpression.new(42),
|
||||||
}
|
Vm::NameExpression.new('foo')]
|
||||||
|
@parser = @parser.argument_list
|
||||||
|
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
|
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)
|
||||||
|
5
|
||||||
|
end
|
||||||
|
HERE
|
||||||
|
@expected = Vm::FunctionExpression.new('foo',
|
||||||
|
[Vm::NameExpression.new('x')],
|
||||||
|
[Vm::IntegerExpression.new(5)])
|
||||||
@parser = @parser.function_definition
|
@parser = @parser.function_definition
|
||||||
tree = parse(input)
|
check
|
||||||
assert_kind_of Vm::FunctionExpression , tree
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,9 +55,6 @@ class TransformTest < MiniTest::Test
|
|||||||
@input = { :conditional => { :integer => "0"},
|
@input = { :conditional => { :integer => "0"},
|
||||||
:if_true => { :expressions => [ { :integer => "42" } ] } ,
|
:if_true => { :expressions => [ { :integer => "42" } ] } ,
|
||||||
:if_false => { :expressions => [ { :integer => "667" } ] } }
|
:if_false => { :expressions => [ { :integer => "667" } ] } }
|
||||||
# = {:conditional => {:integer => '0'},
|
|
||||||
# :if_true => {:block => {:integer => '42'}},
|
|
||||||
# :if_false => {:block => {:integer => '667'}}}
|
|
||||||
@expected = Vm::ConditionalExpression.new( Vm::IntegerExpression.new(0),
|
@expected = Vm::ConditionalExpression.new( Vm::IntegerExpression.new(0),
|
||||||
[Vm::IntegerExpression.new(42)],
|
[Vm::IntegerExpression.new(42)],
|
||||||
[Vm::IntegerExpression.new(667)])
|
[Vm::IntegerExpression.new(667)])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user