rubyx/test/test_parser.rb

109 lines
2.4 KiB
Ruby
Raw Normal View History

2014-04-24 14:53:48 +02:00
require_relative 'helper'
2014-04-24 14:43:20 +02:00
2014-04-27 14:56:22 +02:00
class ParserTest < MiniTest::Test
2014-04-24 14:43:20 +02:00
2014-04-27 14:56:22 +02:00
def setup
2014-04-27 15:30:32 +02:00
@parser = Parser::Composed.new
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def check
is = @parser.parse(@input)
2014-04-27 14:56:22 +02:00
assert is
assert_equal @expected , is
end
def test_number
@input = '42 '
@expected = {:integer => '42'}
@parser = @parser.integer
check
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def test_name
@input = 'foo '
@expected = {:name => 'foo'}
@parser = @parser.name
check
2014-04-24 14:43:20 +02:00
end
def test_one_argument
@input = '(42)'
@expected = {:argument_list => {:argument => {:integer => '42'}} }
@parser = @parser.argument_list
check
end
2014-04-27 14:56:22 +02:00
def test_argument_list
@input = '(42, foo)'
@expected = {:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]}
@parser = @parser.argument_list
check
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def test_function_call
@input = 'baz(42, foo)'
@expected = {:function_call => {:name => 'baz' },
:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]}
2014-04-24 14:43:20 +02:00
@parser = @parser.function_call
check
2014-04-24 14:43:20 +02:00
end
2014-04-28 14:46:57 +02:00
def test_expression_else
@input = <<HERE
4
5
else
HERE
@expected = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]}
@parser = @parser.expressions_else
check
end
def test_expression_end
@input = <<HERE
5
name
call(4,6)
end
HERE
@expected = {:expressions => [ { :integer => "5" },
{ :name => "name" },
{ :function_call => { :name => "call" } ,
:argument_list => [ {:argument => { :integer => "4" } } ,
{:argument => { :integer => "6" } } ] } ]}
@parser = @parser.expressions_end
check
end
2014-04-27 14:56:22 +02:00
def test_conditional
@input = <<HERE
2014-04-28 14:46:57 +02:00
if (0)
2014-04-24 14:43:20 +02:00
42
2014-04-28 14:46:57 +02:00
else
2014-04-24 14:43:20 +02:00
667
2014-04-28 14:46:57 +02:00
end
2014-04-24 14:43:20 +02:00
HERE
2014-04-28 14:46:57 +02:00
@expected = { :conditional => { :integer => "0"},
:if_true => { :expressions => [ { :integer => "42" } ] } ,
:if_false => { :expressions => [ { :integer => "667" } ] } }
@parser = @parser.conditional
check
2014-04-24 14:43:20 +02:00
end
2014-04-28 14:46:57 +02:00
2014-04-27 14:56:22 +02:00
def test_function_definition
@input = <<HERE
2014-04-28 14:46:57 +02:00
def foo(x)
2014-04-24 14:43:20 +02:00
5
2014-04-28 14:46:57 +02:00
end
2014-04-24 14:43:20 +02:00
HERE
@expected = {:function_definition => {:name => 'foo'},
:parmeter_list => {:parmeter => {:name => 'x'}},
2014-04-28 14:46:57 +02:00
:expressions => [{:integer => '5'}]}
@parser = @parser.function_definition
check
2014-04-24 14:43:20 +02:00
end
end