172 lines
4.8 KiB
Ruby
172 lines
4.8 KiB
Ruby
require_relative 'helper'
|
|
|
|
# Some sanity is emerging in the testing of parsers
|
|
# (Parsers are fiddly in respect to space and order, small changes may and do have unexpected effects)
|
|
|
|
# For any functionality that we want to work (ie test), there are actually three tests, with the _same_ name
|
|
# One in each of the parser/transform/ast classes
|
|
# Parser test that the parser parses and thet the output is correct. Rules are named and and boil down to
|
|
# hashes and arrays with lots of symbols for the names the rules (actually the reults) were given
|
|
# Transform test really just test the tranformation. They basically take the output of the parse
|
|
# and check that correct Ast classes are produced
|
|
# Ast tests both steps in one. Ie string input to ast classes output
|
|
|
|
# All threee classes are layed out quite similarly in that they use a check method and
|
|
# each test assigns @input and @parse_output which the check methods then checks
|
|
# The check methods have a pust in it (to be left) which is very handy for checking
|
|
# also the output of parser.check can actually be used as the input of transform
|
|
|
|
# Repeat: For every test in parser, there should beone in transform and ast
|
|
# The test in transform should use the output of parser as input
|
|
# The test in ast should expect the same result as transform
|
|
|
|
class ParserTest < MiniTest::Test
|
|
|
|
def setup
|
|
@parser = Parser::Composed.new
|
|
end
|
|
|
|
def check
|
|
is = @parser.parse(@input)
|
|
#puts is.inspect
|
|
assert_equal @parse_output , is
|
|
end
|
|
def test_number
|
|
@input = '42 '
|
|
@parse_output = {:integer => '42'}
|
|
@parser = @parser.integer
|
|
check
|
|
end
|
|
|
|
def test_name
|
|
@input = 'foo '
|
|
@parse_output = {:name => 'foo'}
|
|
@parser = @parser.name
|
|
check
|
|
end
|
|
|
|
def test_string
|
|
@input = <<HERE
|
|
"hello"
|
|
HERE
|
|
@parse_output = {:string=>"hello"}
|
|
@parser = @parser.string
|
|
check
|
|
end
|
|
|
|
def test_one_argument
|
|
@input = '(42)'
|
|
@parse_output = {:argument_list => {:argument => {:integer => '42'}} }
|
|
@parser = @parser.argument_list
|
|
check
|
|
end
|
|
|
|
def test_argument_list
|
|
@input = '(42, foo)'
|
|
@parse_output = {:argument_list => [{:argument => {:integer => '42'}},
|
|
{:argument => {:name => 'foo'}}]}
|
|
@parser = @parser.argument_list
|
|
check
|
|
end
|
|
|
|
def test_function_call
|
|
@input = 'baz(42, foo)'
|
|
@parse_output = {:function_call => {:name => 'baz' },
|
|
:argument_list => [{:argument => {:integer => '42'}},
|
|
{:argument => {:name => 'foo'}}]}
|
|
|
|
@parser = @parser.function_call
|
|
check
|
|
end
|
|
|
|
def test_function_call_string
|
|
@input = <<HERE
|
|
puts( "hello")
|
|
HERE
|
|
@parse_output = {:function_call => {:name => 'baz' },
|
|
:argument_list => [{:argument => {:integer => '42'}},
|
|
{:argument => {:name => 'foo'}}]}
|
|
|
|
@parser = @parser.function_call
|
|
check
|
|
end
|
|
|
|
def test_expression_else
|
|
@input = <<HERE
|
|
4
|
|
5
|
|
else
|
|
HERE
|
|
@parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]}
|
|
|
|
@parser = @parser.expressions_else
|
|
check
|
|
end
|
|
|
|
def test_expression_end
|
|
@input = <<HERE
|
|
5
|
|
name
|
|
call(4,6)
|
|
end
|
|
HERE
|
|
@parse_output = {:expressions => [ { :integer => "5" },
|
|
{ :name => "name" },
|
|
{ :function_call => { :name => "call" } ,
|
|
:argument_list => [ {:argument => { :integer => "4" } } ,
|
|
{:argument => { :integer => "6" } } ] } ]}
|
|
@parser = @parser.expressions_end
|
|
check
|
|
end
|
|
|
|
def test_conditional
|
|
@input = <<HERE
|
|
if (0)
|
|
42
|
|
else
|
|
667
|
|
end
|
|
HERE
|
|
@parse_output = { :conditional => { :integer => "0"},
|
|
:if_true => { :expressions => [ { :integer => "42" } ] } ,
|
|
:if_false => { :expressions => [ { :integer => "667" } ] } }
|
|
@parser = @parser.conditional
|
|
check
|
|
end
|
|
|
|
def test_function_definition
|
|
@input = <<HERE
|
|
def foo(x)
|
|
5
|
|
end
|
|
HERE
|
|
@parse_output = {:function_definition => {:name => 'foo'},
|
|
:parmeter_list => {:parmeter => {:name => 'x'}},
|
|
:expressions => [{:integer => '5'}]}
|
|
@parser = @parser.function_definition
|
|
check
|
|
end
|
|
|
|
def test_function_assignment
|
|
@input = <<HERE
|
|
def foo(x)
|
|
abba = 5
|
|
end
|
|
HERE
|
|
@parse_output = { :function_definition => { :name => "foo" } ,
|
|
:parmeter_list => { :parmeter => { :name => "x" } },
|
|
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
|
}
|
|
@parser = @parser.function_definition
|
|
check
|
|
end
|
|
|
|
def test_assignment
|
|
@input = "a = 5"
|
|
@parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } }
|
|
@parser = @parser.assignment
|
|
check
|
|
end
|
|
|
|
end
|