start to split tests according to feature

This commit is contained in:
Torsten Ruger 2014-04-29 11:37:43 +03:00
parent 7dfde45373
commit 2507251f75
5 changed files with 103 additions and 42 deletions

View File

@ -0,0 +1,33 @@
Parsing
-------
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)
Parsing is a two step process with parslet:
- parse takes an input and outputs hashes/arrays with basic types
- tramsform takes the output of parse and generates an ast (as specified by the transformation)
A test tests both phases seperately and again together.
Each test must thus specify (as instance variables):
- the string input
- the parse output
- the transform output
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 @expected 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 be one 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

29
test/parser/helper.rb Normal file
View File

@ -0,0 +1,29 @@
require_relative "../helper"
module ParserTest
def setup
@parser = Parser::Composed.new
@transform = Parser::Transform.new
end
def check_parse
is = @parser.parse(@input)
#puts is.inspect
assert_equal @expected , is
end
def check_transform
is = @transform.apply @input
#puts is.transform
assert_equal @expected , is
end
def check_ast
syntax = @parser.parse(@input)
tree = @transform.apply(syntax)
# puts tree.inspect
assert_equal @expected , tree
end
end

View File

@ -15,19 +15,19 @@ class TestAst < MiniTest::Test
syntax = @parser.parse(@input) syntax = @parser.parse(@input)
tree = @transform.apply(syntax) tree = @transform.apply(syntax)
# puts tree.inspect # puts tree.inspect
assert_equal @expected , tree assert_equal @transform_output , tree
end end
def test_number def test_number
@input = '42 ' @input = '42 '
@expected = Parser::IntegerExpression.new(42) @transform_output = Parser::IntegerExpression.new(42)
@parser = @parser.integer @parser = @parser.integer
check check
end end
def test_name def test_name
@input = 'foo ' @input = 'foo '
@expected = Parser::NameExpression.new('foo') @transform_output = Parser::NameExpression.new('foo')
@parser = @parser.name @parser = @parser.name
check check
end end
@ -36,21 +36,21 @@ class TestAst < MiniTest::Test
@input = <<HERE @input = <<HERE
"hello" "hello"
HERE HERE
@expected = Parser::StringExpression.new('hello') @transform_output = Parser::StringExpression.new('hello')
@parser = @parser.string @parser = @parser.string
check check
end end
def test_one_argument def test_one_argument
@input = '(42)' @input = '(42)'
@expected = { :argument_list => Parser::IntegerExpression.new(42) } @transform_output = { :argument_list => Parser::IntegerExpression.new(42) }
@parser = @parser.argument_list @parser = @parser.argument_list
check check
end end
def test_argument_list def test_argument_list
@input = '(42, foo)' @input = '(42, foo)'
@expected = [Parser::IntegerExpression.new(42), @transform_output = [Parser::IntegerExpression.new(42),
Parser::NameExpression.new('foo')] Parser::NameExpression.new('foo')]
@parser = @parser.argument_list @parser = @parser.argument_list
check check
@ -58,7 +58,7 @@ HERE
def test_function_call def test_function_call
@input = 'baz(42, foo)' @input = 'baz(42, foo)'
@expected = Parser::FuncallExpression.new 'baz', [Parser::IntegerExpression.new(42), @transform_output = Parser::FuncallExpression.new 'baz', [Parser::IntegerExpression.new(42),
Parser::NameExpression.new('foo')] Parser::NameExpression.new('foo')]
@parser = @parser.function_call @parser = @parser.function_call
@ -71,7 +71,7 @@ HERE
5 5
else else
HERE HERE
@expected = {:expressions=>[ Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(5)]} @transform_output = {:expressions=>[ Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(5)]}
@parser = @parser.expressions_else @parser = @parser.expressions_else
check check
@ -84,7 +84,7 @@ name
call(4,6) call(4,6)
end end
HERE HERE
@expected = {:expressions=> [Parser::IntegerExpression.new(5), Parser::NameExpression.new("name"), @transform_output = {:expressions=> [Parser::IntegerExpression.new(5), Parser::NameExpression.new("name"),
Parser::FuncallExpression.new("call", [Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(6) ]) ] } Parser::FuncallExpression.new("call", [Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(6) ]) ] }
@parser = @parser.expressions_end @parser = @parser.expressions_end
check check
@ -98,7 +98,7 @@ else
667 667
end end
HERE HERE
@expected = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0), @transform_output = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0),
[Parser::IntegerExpression.new(42)], [Parser::IntegerExpression.new(42)],
[Parser::IntegerExpression.new(667)]) [Parser::IntegerExpression.new(667)])
@parser = @parser.conditional @parser = @parser.conditional
@ -111,7 +111,7 @@ def foo(x)
5 5
end end
HERE HERE
@expected = 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)])
@parser = @parser.function_definition @parser = @parser.function_definition
@ -123,7 +123,7 @@ def foo(x)
abba = 5 abba = 5
end end
HERE HERE
@expected = 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 check
end end

View File

@ -12,7 +12,7 @@ require_relative 'helper'
# Ast tests both steps in one. Ie string input to ast classes output # 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 # All threee classes are layed out quite similarly in that they use a check method and
# each test assigns @input and @expected which the check methods then checks # 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 # 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 # also the output of parser.check can actually be used as the input of transform
@ -29,18 +29,18 @@ class ParserTest < MiniTest::Test
def check def check
is = @parser.parse(@input) is = @parser.parse(@input)
#puts is.inspect #puts is.inspect
assert_equal @expected , is assert_equal @parse_output , is
end end
def test_number def test_number
@input = '42 ' @input = '42 '
@expected = {:integer => '42'} @parse_output = {:integer => '42'}
@parser = @parser.integer @parser = @parser.integer
check check
end end
def test_name def test_name
@input = 'foo ' @input = 'foo '
@expected = {:name => 'foo'} @parse_output = {:name => 'foo'}
@parser = @parser.name @parser = @parser.name
check check
end end
@ -49,21 +49,21 @@ class ParserTest < MiniTest::Test
@input = <<HERE @input = <<HERE
"hello" "hello"
HERE HERE
@expected = {:string=>"hello"} @parse_output = {:string=>"hello"}
@parser = @parser.string @parser = @parser.string
check check
end end
def test_one_argument def test_one_argument
@input = '(42)' @input = '(42)'
@expected = {:argument_list => {:argument => {:integer => '42'}} } @parse_output = {:argument_list => {:argument => {:integer => '42'}} }
@parser = @parser.argument_list @parser = @parser.argument_list
check check
end end
def test_argument_list def test_argument_list
@input = '(42, foo)' @input = '(42, foo)'
@expected = {:argument_list => [{:argument => {:integer => '42'}}, @parse_output = {:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@parser = @parser.argument_list @parser = @parser.argument_list
check check
@ -71,7 +71,7 @@ HERE
def test_function_call def test_function_call
@input = 'baz(42, foo)' @input = 'baz(42, foo)'
@expected = {:function_call => {:name => 'baz' }, @parse_output = {:function_call => {:name => 'baz' },
:argument_list => [{:argument => {:integer => '42'}}, :argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@ -83,7 +83,7 @@ HERE
@input = <<HERE @input = <<HERE
puts( "hello") puts( "hello")
HERE HERE
@expected = {:function_call => {:name => 'baz' }, @parse_output = {:function_call => {:name => 'baz' },
:argument_list => [{:argument => {:integer => '42'}}, :argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@ -97,7 +97,7 @@ HERE
5 5
else else
HERE HERE
@expected = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]} @parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]}
@parser = @parser.expressions_else @parser = @parser.expressions_else
check check
@ -110,7 +110,7 @@ name
call(4,6) call(4,6)
end end
HERE HERE
@expected = {:expressions => [ { :integer => "5" }, @parse_output = {:expressions => [ { :integer => "5" },
{ :name => "name" }, { :name => "name" },
{ :function_call => { :name => "call" } , { :function_call => { :name => "call" } ,
:argument_list => [ {:argument => { :integer => "4" } } , :argument_list => [ {:argument => { :integer => "4" } } ,
@ -127,7 +127,7 @@ else
667 667
end end
HERE HERE
@expected = { :conditional => { :integer => "0"}, @parse_output = { :conditional => { :integer => "0"},
:if_true => { :expressions => [ { :integer => "42" } ] } , :if_true => { :expressions => [ { :integer => "42" } ] } ,
:if_false => { :expressions => [ { :integer => "667" } ] } } :if_false => { :expressions => [ { :integer => "667" } ] } }
@parser = @parser.conditional @parser = @parser.conditional
@ -140,7 +140,7 @@ def foo(x)
5 5
end end
HERE HERE
@expected = {:function_definition => {:name => 'foo'}, @parse_output = {:function_definition => {:name => 'foo'},
:parmeter_list => {:parmeter => {:name => 'x'}}, :parmeter_list => {:parmeter => {:name => 'x'}},
:expressions => [{:integer => '5'}]} :expressions => [{:integer => '5'}]}
@parser = @parser.function_definition @parser = @parser.function_definition
@ -153,7 +153,7 @@ def foo(x)
abba = 5 abba = 5
end end
HERE HERE
@expected = { :function_definition => { :name => "foo" } , @parse_output = { :function_definition => { :name => "foo" } ,
:parmeter_list => { :parmeter => { :name => "x" } }, :parmeter_list => { :parmeter => { :name => "x" } },
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
} }
@ -163,7 +163,7 @@ HERE
def test_assignment def test_assignment
@input = "a = 5" @input = "a = 5"
@expected = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } } @parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } }
@parser = @parser.assignment @parser = @parser.assignment
check check
end end

View File

@ -11,32 +11,31 @@ class TransformTest < MiniTest::Test
def check def check
is = @transform.apply @input is = @transform.apply @input
#puts is.transform #puts is.transform
assert_equal @expected , is assert_equal @transform_output , is
end end
def test_number def test_number
@input = {:integer => '42'} @input = {:integer => '42'}
@expected = Parser::IntegerExpression.new(42) @transform_output = Parser::IntegerExpression.new(42)
check check
assert_equal 42 , @expected.value assert_equal 42 , @transform_output.value
end end
def test_name def test_name
@input = {:name => 'foo'} @input = {:name => 'foo'}
@expected = Parser::NameExpression.new('foo') @transform_output = Parser::NameExpression.new('foo')
check check
end end
def test_string def test_string
@input = {:string=>"hello"} @input = {:string=>"hello"}
@expected = Parser::StringExpression.new('hello') @transform_output = Parser::StringExpression.new('hello')
@parser = @parser.string
check check
end end
def test_argument_list def test_argument_list
@input = {:argument_list => [{:argument => {:integer => '42'}}, @input = {:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@expected = [Parser::IntegerExpression.new(42), @transform_output = [Parser::IntegerExpression.new(42),
Parser::NameExpression.new('foo')] Parser::NameExpression.new('foo')]
check check
end end
@ -44,7 +43,7 @@ class TransformTest < MiniTest::Test
def test_single_argument def test_single_argument
@input = {:function_call => {:name => 'foo'}, @input = {:function_call => {:name => 'foo'},
:argument_list => {:argument => {:integer => '42'} } } :argument_list => {:argument => {:integer => '42'} } }
@expected = Parser::FuncallExpression.new 'foo', [Parser::IntegerExpression.new(42)] @transform_output = Parser::FuncallExpression.new 'foo', [Parser::IntegerExpression.new(42)]
check check
end end
@ -53,7 +52,7 @@ class TransformTest < MiniTest::Test
@input = {:function_call => {:name => 'baz'}, @input = {:function_call => {:name => 'baz'},
:argument_list => [{:argument => {:integer => '42'}}, :argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@expected = Parser::FuncallExpression.new 'baz', [Parser::IntegerExpression.new(42), @transform_output = Parser::FuncallExpression.new 'baz', [Parser::IntegerExpression.new(42),
Parser::NameExpression.new('foo')] Parser::NameExpression.new('foo')]
check check
@ -63,7 +62,7 @@ class TransformTest < MiniTest::Test
@input = { :conditional => { :integer => "0"}, @input = { :conditional => { :integer => "0"},
:if_true => { :expressions => [ { :integer => "42" } ] } , :if_true => { :expressions => [ { :integer => "42" } ] } ,
:if_false => { :expressions => [ { :integer => "667" } ] } } :if_false => { :expressions => [ { :integer => "667" } ] } }
@expected = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0), @transform_output = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0),
[Parser::IntegerExpression.new(42)], [Parser::IntegerExpression.new(42)],
[Parser::IntegerExpression.new(667)]) [Parser::IntegerExpression.new(667)])
check check
@ -71,12 +70,12 @@ class TransformTest < MiniTest::Test
def test_parmeter def test_parmeter
@input = {:parmeter => { :name => "foo"}} @input = {:parmeter => { :name => "foo"}}
@expected = Parser::NameExpression.new('foo') @transform_output = Parser::NameExpression.new('foo')
check check
end end
def test_parmeter_list def test_parmeter_list
@input = {:parmeter_list => [{:parmeter => { :name => "foo"}}]} @input = {:parmeter_list => [{:parmeter => { :name => "foo"}}]}
@expected = [Parser::NameExpression.new('foo')] @transform_output = [Parser::NameExpression.new('foo')]
check check
end end
@ -84,7 +83,7 @@ class TransformTest < MiniTest::Test
@input = {:function_definition => {:name => 'foo'}, @input = {:function_definition => {:name => 'foo'},
:parmeter_list => {:parmeter => {:name => 'x'}}, :parmeter_list => {:parmeter => {:name => 'x'}},
:expressions => [{:integer => '5'}]} :expressions => [{:integer => '5'}]}
@expected = 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 check
@ -95,14 +94,14 @@ class TransformTest < MiniTest::Test
:parmeter_list => { :parmeter => { :name => "x" } }, :parmeter_list => { :parmeter => { :name => "x" } },
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
} }
@expected = 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 check
end end
def test_assignment def test_assignment
@input = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } } @input = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } }
@expected = Parser::AssignmentExpression.new("a", Parser::IntegerExpression.new(5) ) @transform_output = Parser::AssignmentExpression.new("a", Parser::IntegerExpression.new(5) )
check check
end end
end end