moe the last (function) tests to new test model

This commit is contained in:
Torsten Ruger 2014-04-29 16:57:36 +03:00
parent 1593d330ae
commit 8151ed6b9d
3 changed files with 21 additions and 127 deletions

View File

@ -1,37 +1,36 @@
require_relative 'helper' require_relative "helper"
# Please read note in test_parser class TestFunctionDefinition < MiniTest::Test
# include the magic (setup and parse -> test method translation), see there
include ParserHelper
class TransformTest < MiniTest::Test def test_simplest_function
@string_input = <<HERE
def setup def foo(x)
@transform = Parser::Transform.new 5
end end
HERE
def check @parse_output = {:function_definition => {:name => 'foo'},
is = @transform.apply @input :parmeter_list => [{:parmeter => {:name => 'x'}}],
#puts is.transform
assert_equal @transform_output , is
end
def test_function_definition
@input = {:function_definition => {:name => 'foo'},
:parmeter_list => {:parmeter => {:name => 'x'}},
:expressions => [{:integer => '5'}]} :expressions => [{:integer => '5'}]}
@transform_output = Parser::FunctionExpression.new('foo', @transform_output = Parser::FunctionExpression.new('foo',
[Parser::NameExpression.new('x')], [Parser::NameExpression.new('x')],
[Parser::IntegerExpression.new(5)]) [Parser::IntegerExpression.new(5)])
check
end end
def test_function_assignment def test_function_assignment
@input = { :function_definition => { :name => "foo" } , @string_input = <<HERE
:parmeter_list => { :parmeter => { :name => "x" } }, 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" } } ] :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
} }
@transform_output = Parser::FunctionExpression.new( "foo", [Parser::NameExpression.new("x")], @transform_output = Parser::FunctionExpression.new( "foo", [Parser::NameExpression.new("x")],
[Parser::AssignmentExpression.new( "abba", Parser::IntegerExpression.new(5) ) ]) [Parser::AssignmentExpression.new( "abba", Parser::IntegerExpression.new(5) ) ])
check
end end
end end

View File

@ -1,43 +0,0 @@
require_relative "helper"
# Please read note in test_parser
require_relative 'helper'
class TestAst < MiniTest::Test
def setup
@parser = Parser::Composed.new
@transform = Parser::Transform.new
end
def check
syntax = @parser.parse(@string_input)
tree = @transform.apply(syntax)
# puts tree.inspect
assert_equal @transform_output , tree
end
def test_function_definition
@string_input = <<HERE
def foo(x)
5
end
HERE
@transform_output = Parser::FunctionExpression.new('foo',
[Parser::NameExpression.new('x')],
[Parser::IntegerExpression.new(5)])
@parser = @parser.function_definition
check
end
def test_function_assignment
@string_input = <<HERE
def foo(x)
abba = 5
end
HERE
@transform_output = Parser::FunctionExpression.new( "foo", [Parser::NameExpression.new("x")],
[Parser::AssignmentExpression.new( "abba", Parser::IntegerExpression.new(5) ) ])
check
end
end

View File

@ -1,62 +0,0 @@
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 @string_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(@string_input)
#puts is.inspect
assert_equal @parse_output , is
end
def test_function_definition
@string_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
@string_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
end