annotate python/testpyy.py @ 184:fe2b72381a83

Added testset for pyy
author Windel Bouwman
date Fri, 24 May 2013 16:13:23 +0200
parents
children 51a6440d6398
rev   line source
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
1 import unittest, pprint
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
2 from pyyacc import Grammar, Item, EOF
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
3
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
4
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
5 class testLR(unittest.TestCase):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
6 def setUp(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
7 pass
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
8
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
9 def testSimpleGrammar(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
10 # 1. define a simple grammar:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
11 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
12 g.add_production('input', ['expression'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
13 g.add_production('expression', ['term'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
14 g.add_production('expression', ['expression', '+', 'term'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
15 g.add_production('term', ['factor'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
16 g.add_production('term', ['term', '*', 'factor'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
17 g.add_production('factor', ['(', 'expression', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
18 g.add_production('factor', ['identifier'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
19 g.start_symbol = 'input'
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
20 # 2. define input:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
21 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF']
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
22 # 3. build parser:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
23 p = g.genParser()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
24 # 4. feed input:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
25 p.parse(tokens)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
26
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
27 class testPG(unittest.TestCase):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
28 """ Tests several parts of the parser generator """
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
29 def setUp(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
30 g = Grammar(['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
31 g.add_production('goal', ['list'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
32 g.add_production('list', ['list', 'pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
33 g.add_production('list', ['pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
34 g.add_production('pair', ['(', 'pair', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
35 g.add_production('pair', ['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
36 g.start_symbol = 'goal'
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
37 g.first = g.calcFirstSets()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
38 self.g = g
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
39
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
40 def testFirstSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
41 for a in ['(', ')', EOF, 'EPS']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
42 self.assertEqual(self.g.first[a], {a})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
43 for nt in ['list', 'pair', 'goal']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
44 self.assertEqual(self.g.first[nt], {'('})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
45
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
46 def testInitItemSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
47 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
48 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
49 self.assertEqual(len(s0), 9) # 9 with the goal rule included!
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
50 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
51 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
52 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
53 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
54 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
55 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
56 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
57 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
58 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
59
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
60 def testCanonical(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
61 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
62 s, gt = self.g.genCanonicalSet(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
63 pprint.pprint(s)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
64 # Must result in 12 sets:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
65 self.assertEqual(len(s), 12)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
66
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
67 def testClosure(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
68 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
69 s0 = set()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
70 for p in self.g.productionsForName(self.g.start_symbol):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
71 s0.add(Item(p, 0, EOF))
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
72 self.assertEqual(len(s0), 1) # 1 rule
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
73 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
74
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
75 # Invoke closure on set:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
76 s0 = self.g.closure(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
77 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
78 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
79 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
80 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
81 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
82 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
83 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
84 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
85 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
86
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
87 def testParser(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
88 tokens = ['(', '(', ')', '(', ')', ')', 'EOF']
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
89 # 3. build parser:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
90 p = self.g.genParser()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
91 # 4. feed input:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
92 p.parse(tokens)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
93
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
94 if __name__ == '__main__':
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
95 unittest.main()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
96
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
97