###################################################################### # # Tests for the parse methods. # ###################################################################### require 'test_helper' class MatchTest < Minitest::Test def setup C.default_parser = C::Parser.new end def test_node_matches i = C::Int.new assert_same(true, i.match?(i)) assert_same(true, i.match?('int')) l = C::IntLiteral.new(10) assert_same(true, l.match?(l)) assert_same(true, l.match?(10)) i = C::Int.new assert_same(false, i.match?('unsigned int')) assert_same(false, i.match?('long int')) assert_same(false, i.match?('no int here')) ## shouldn't raise! l = C::IntLiteral.new(10) assert_same(false, i.match?(10.0)) t = C::CustomType.new('T') # assert_same(false, t.match?('T')) # parser = C::Parser.new parser.type_names << 'T' assert_same(true, t.match?('T', parser)) # assert_same(false, t.match?('T')) # C.default_parser.type_names << 'T' assert_same(true, t.match?('T')) end def test_nodelist_match list = C::NodeArray[] assert_same(true, list.match?(list)) assert_same(true, list.match?([])) list = C::NodeArray[C::Int.new, C::IntLiteral.new(10)] list2 = C::NodeChain[C::Int.new, C::IntLiteral.new(10)] assert_same(true, list.match?(list)) assert_same(true, list.match?(list2)) assert_same(true, list.match?(['int', 10])) list = C::NodeArray[C::NodeArray[C::Int.new], C::NodeChain[]] list2 = C::NodeChain[C::NodeChain[C::Int.new], C::NodeArray[]] assert_same(true, list.match?(list)) assert_same(true, list.match?(list2)) assert_same(true, list.match?([['int'], []])) assert_same(false, list.match?([[], ['int']])) assert_same(false, list.match?(['int'])) assert_same(false, list.match?([['int']])) t = C::NodeArray[C::CustomType.new('T')] # assert_same(false, t.match?(['T'])) # parser = C::Parser.new parser.type_names << 'T' assert_same(true, t.match?(['T'], parser)) # assert_same(false, t.match?(['T'])) # C.default_parser.type_names << 'T' assert_same(true, t.match?(['T'])) end end class ParseTests < Minitest::Test def check(klass, s) check_ast(s){|inp| klass.parse(inp)} end def test_translation_unit check C::TranslationUnit, <y ---- Arrow expr: Variable name: "x" member: Member name: "y" EOS assert_raises(C::ParseError){C::Arrow.parse('} void f() {')} assert_raises(C::ParseError){C::Arrow.parse(';')} assert_raises(C::ParseError){C::Arrow.parse('int i')} assert_raises(C::ParseError){C::Arrow.parse('int')} assert_raises(C::ParseError){C::Arrow.parse('if (0)')} assert_raises(C::ParseError){C::Arrow.parse('switch (0)')} assert_raises(C::ParseError){C::Arrow.parse('for (;;)')} assert_raises(C::ParseError){C::Arrow.parse('goto')} assert_raises(C::ParseError){C::Arrow.parse('return')} end def test_dot check C::Dot, < 10 ---- More expr1: IntLiteral val: 1 expr2: IntLiteral val: 10 EOS assert_raises(C::ParseError){C::More.parse('} void f() {')} assert_raises(C::ParseError){C::More.parse(';')} assert_raises(C::ParseError){C::More.parse('int i')} assert_raises(C::ParseError){C::More.parse('int')} assert_raises(C::ParseError){C::More.parse('if (0)')} assert_raises(C::ParseError){C::More.parse('switch (0)')} assert_raises(C::ParseError){C::More.parse('for (;;)')} assert_raises(C::ParseError){C::More.parse('goto')} assert_raises(C::ParseError){C::More.parse('return')} end def test_less_or_equal check C::LessOrEqual, <= 10 ---- MoreOrEqual expr1: IntLiteral val: 1 expr2: IntLiteral val: 10 EOS assert_raises(C::ParseError){C::MoreOrEqual.parse('} void f() {')} assert_raises(C::ParseError){C::MoreOrEqual.parse(';')} assert_raises(C::ParseError){C::MoreOrEqual.parse('int i')} assert_raises(C::ParseError){C::MoreOrEqual.parse('int')} assert_raises(C::ParseError){C::MoreOrEqual.parse('if (0)')} assert_raises(C::ParseError){C::MoreOrEqual.parse('switch (0)')} assert_raises(C::ParseError){C::MoreOrEqual.parse('for (;;)')} assert_raises(C::ParseError){C::MoreOrEqual.parse('goto')} assert_raises(C::ParseError){C::MoreOrEqual.parse('return')} end def test_bit_and check C::BitAnd, <> 10 ---- ShiftRight expr1: IntLiteral val: 1 expr2: IntLiteral val: 10 EOS assert_raises(C::ParseError){C::ShiftRight.parse('} void f() {')} assert_raises(C::ParseError){C::ShiftRight.parse(';')} assert_raises(C::ParseError){C::ShiftRight.parse('int i')} assert_raises(C::ParseError){C::ShiftRight.parse('int')} assert_raises(C::ParseError){C::ShiftRight.parse('if (0)')} assert_raises(C::ParseError){C::ShiftRight.parse('switch (0)')} assert_raises(C::ParseError){C::ShiftRight.parse('for (;;)')} assert_raises(C::ParseError){C::ShiftRight.parse('goto')} assert_raises(C::ParseError){C::ShiftRight.parse('return')} end def test_and check C::And, <>= 10 ---- ShiftRightAssign lval: Variable name: "x" rval: IntLiteral val: 10 EOS assert_raises(C::ParseError){C::ShiftRightAssign.parse('} void f() {')} assert_raises(C::ParseError){C::ShiftRightAssign.parse(';')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('int i')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('int')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('if (0)')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('switch (0)')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('for (;;)')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('goto')} assert_raises(C::ParseError){C::ShiftRightAssign.parse('return')} end def test_bit_and_assign check C::BitAndAssign, <