require 'test/unit' require 'rockit/dparser' class ATestListOperator < Test::Unit::TestCase ListTestGrammar = Rockit::DParser::Grammar.new do start :S, [list(:Number, ",")], value(0) term :Number, [/[0-9]+/] end Parser = ListTestGrammar.new_parser def test_01_two_value_list ary = Parser.parse("1, 2") assert_equal(["1", "2"], ary) end def test_02_multi_value_list ary = Parser.parse("1, 2, 34, 567,890") assert_equal(["1", "2", "34", "567", "890"], ary) end def test_03_single_value_list ary = Parser.parse("9731") assert_equal(["9731"], ary) end ListAndAstTestGrammar = Rockit::DParser::Grammar.new do start :S, [list(:Digit)], ast(:S, 0) term :Digit, [/[0-9]+/] end AstParser = ListAndAstTestGrammar.new_parser def test_04_multiple_occurences_and_ast ast = AstParser.parse("7,5,6,9") assert_equal("S", ast.name) assert_kind_of(AstParser.astclass_of_name(:S), ast) assert_equal(1, ast.num_children) assert_equal(["7", "5", "6", "9"], ast[0]) end def test_05_single_occurence_and_ast ast = AstParser.parse("745") assert_equal("S", ast.name) assert_kind_of(AstParser.astclass_of_name(:S), ast) assert_equal(1, ast.num_children) assert_equal(["745"], ast[0]) end def atest_06_no_occurence_and_ast ast = AstParser.parse("") assert_equal("S", ast.name) assert_kind_of(AstParser.astclass_of_name(:S), ast) assert_equal(1, ast.num_children) assert_equal([], ast[0]) end end