rubyx/test/test_parser.rb

68 lines
1.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)
assert is
assert_equal @expected , is
end
def test_number
@input = '42 '
@expected = {:number => '42'}
@parser = @parser.number
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
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def test_argument_list
@input = '(42, foo)'
@expected = {:args => [{:arg => {:number => '42'}},
2014-04-24 14:43:20 +02:00
{:arg => {:name => 'foo'}}]}
2014-04-27 14:56:22 +02:00
@parser = @parser.args
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 = {:funcall => {:name => 'baz' },
2014-04-24 14:43:20 +02:00
:args => [{:arg => {:number => '42'}},
{:arg => {:name => 'foo'}}]}
2014-04-27 14:56:22 +02:00
@parser = @parser.funcall
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def test_conditional
@input = <<HERE
2014-04-24 14:43:20 +02:00
if (0) {
42
} else {
667
}
HERE
2014-04-27 14:56:22 +02:00
@expected = {:cond => {:number => '0'},
2014-04-24 14:43:20 +02:00
:if_true => {:body => {:number => '42'}},
:if_false => {:body => {:number => '667'}}}
2014-04-27 14:56:22 +02:00
@parser = @parser.cond
2014-04-24 14:43:20 +02:00
end
2014-04-27 14:56:22 +02:00
def test_function_definition
@input = <<HERE
2014-04-24 14:43:20 +02:00
function foo(x) {
5
}
HERE
2014-04-27 14:56:22 +02:00
@expected = {:func => {:name => 'foo'},
2014-04-24 14:43:20 +02:00
:params => {:param => {:name => 'x'}},
:body => {:number => '5'}}
2014-04-27 14:56:22 +02:00
@parser = @parser.func
2014-04-24 14:43:20 +02:00
end
end