2014-04-24 14:53:48 +02:00
|
|
|
require_relative 'helper'
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-28 21:22:37 +02:00
|
|
|
# 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
|
2014-04-29 15:22:39 +02:00
|
|
|
# each test assigns @string_input and @parse_output which the check methods then checks
|
2014-04-28 21:22:37 +02:00
|
|
|
# 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
|
|
|
|
|
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
|
2014-04-29 15:22:39 +02:00
|
|
|
is = @parser.parse(@string_input)
|
2014-04-28 21:22:37 +02:00
|
|
|
#puts is.inspect
|
2014-04-29 10:37:43 +02:00
|
|
|
assert_equal @parse_output , is
|
2014-04-27 14:56:22 +02:00
|
|
|
end
|
2014-04-29 10:17:19 +02:00
|
|
|
|
2014-04-27 14:56:22 +02:00
|
|
|
def test_function_definition
|
2014-04-29 15:22:39 +02:00
|
|
|
@string_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
|
2014-04-29 10:37:43 +02:00
|
|
|
@parse_output = {:function_definition => {:name => 'foo'},
|
2014-04-27 20:41:38 +02:00
|
|
|
:parmeter_list => {:parmeter => {:name => 'x'}},
|
2014-04-28 14:46:57 +02:00
|
|
|
:expressions => [{:integer => '5'}]}
|
2014-04-27 20:12:42 +02:00
|
|
|
@parser = @parser.function_definition
|
|
|
|
check
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
2014-04-28 20:21:12 +02:00
|
|
|
|
2014-04-28 21:07:35 +02:00
|
|
|
def test_function_assignment
|
2014-04-29 15:22:39 +02:00
|
|
|
@string_input = <<HERE
|
2014-04-28 21:07:35 +02:00
|
|
|
def foo(x)
|
|
|
|
abba = 5
|
|
|
|
end
|
|
|
|
HERE
|
2014-04-29 10:37:43 +02:00
|
|
|
@parse_output = { :function_definition => { :name => "foo" } ,
|
2014-04-28 21:07:35 +02:00
|
|
|
:parmeter_list => { :parmeter => { :name => "x" } },
|
|
|
|
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
|
|
|
}
|
|
|
|
@parser = @parser.function_definition
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
2014-04-28 20:21:12 +02:00
|
|
|
def test_assignment
|
2014-04-29 15:22:39 +02:00
|
|
|
@string_input = "a = 5"
|
2014-04-29 10:37:43 +02:00
|
|
|
@parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } }
|
2014-04-28 20:21:12 +02:00
|
|
|
@parser = @parser.assignment
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|