def test_init(self): "Choice.__init__()" p1 = Prod('p1', lambda t, v: t == 1) p2 = Prod('p2', lambda t, v: t == 2) t0 = (0, 0, 0, 0) t1 = (1, 0, 0, 0) t2 = (2, 0, 0, 0) ch = Choice(p1, p2) self.assertRaisesMsg(ParseError, 'No match for (0, 0, 0, 0) in Choice(p1, p2)', ch.nextProd, t0) self.assertEqual(p1, ch.nextProd(t1)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1) ch = Choice(p1, p2) self.assertEqual(p2, ch.nextProd(t2)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t2) ch = Choice(p2, p1) self.assertRaisesMsg(ParseError, 'No match for (0, 0, 0, 0) in Choice(p2, p1)', ch.nextProd, t0) self.assertEqual(p1, ch.nextProd(t1)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1) ch = Choice(p2, p1) self.assertEqual(p2, ch.nextProd(t2)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t2)
def test_reset(self): "Choice.reset()" p1 = Prod('p1', lambda t, v: t == 1) p2 = Prod('p2', lambda t, v: t == 2) t1 = (1, 0, 0, 0) t2 = (2, 0, 0, 0) ch = Choice(p1, p2) self.assertEqual(p1, ch.nextProd(t1)) self.assertRaises(Exhausted, ch.nextProd, t1) ch.reset() self.assertEqual(p2, ch.nextProd(t2))
def test_nested(self): "Choice with nested Sequence" p1 = Prod('p1', lambda t, v: t == 1) p2 = Prod('p2', lambda t, v: t == 2) s1 = Sequence(p1, p1) s2 = Sequence(p2, p2) t0 = (0, 0, 0, 0) t1 = (1, 0, 0, 0) t2 = (2, 0, 0, 0) ch = Choice(s1, s2) self.assertRaisesMsg( ParseError, 'No match for (0, 0, 0, 0) in Choice(Sequence(p1, p1), Sequence(p2, p2))', ch.nextProd, t0, ) self.assertEqual(s1, ch.nextProd(t1)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1) ch = Choice(s1, s2) self.assertEqual(s2, ch.nextProd(t2)) self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1)