From 2fdbb9f3aeb6515fc9c01f0f5a4213e77b20b489 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 28 Apr 2014 16:26:19 +0300 Subject: [PATCH] imroved and fixed node test (parse and transform) --- test/test_nodes.rb | 117 +++++++++++++++++++++++++++++++---------- test/test_transform.rb | 3 -- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/test/test_nodes.rb b/test/test_nodes.rb index e1cc45bb..fe43a422 100644 --- a/test/test_nodes.rb +++ b/test/test_nodes.rb @@ -1,50 +1,113 @@ 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 +require_relative 'helper' + class TestNodes < MiniTest::Test def setup @parser = Parser::Composed.new @transform = Parser::Transform.new end - - def parse string - syntax = @parser.parse(string) + + def check + syntax = @parser.parse(@input) tree = @transform.apply(syntax) - tree + assert tree + assert_equal @expected , tree end def test_number - tree = parse "42" - assert_kind_of Vm::IntegerExpression , tree - assert_equal 42 , tree.value + @input = '42 ' + @expected = Vm::IntegerExpression.new(42) + @parser = @parser.integer + check end - def test_args - tree = parse "( 42 )" - assert_kind_of Hash , tree - assert_kind_of Vm::IntegerExpression , tree[:argument_list] - assert_equal 42 , tree[:argument_list].value + + def test_name + @input = 'foo ' + @expected = Vm::NameExpression.new('foo') + @parser = @parser.name + check end - def test_arg_list + + def test_one_argument + @input = '(42)' + @expected = { :argument_list => Vm::IntegerExpression.new(42) } @parser = @parser.argument_list - tree = parse "(42, foo)" - assert_instance_of Array , tree - assert_equal 42 , tree.first.value - assert_equal "foo" , tree.last.name + check end - def test_definition - input = <[ Vm::IntegerExpression.new(4), Vm::IntegerExpression.new(5)]} + + @parser = @parser.expressions_else + check + end + + def test_expression_end + @input = < [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 = < { :integer => "0"}, :if_true => { :expressions => [ { :integer => "42" } ] } , :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), [Vm::IntegerExpression.new(42)], [Vm::IntegerExpression.new(667)])