diff test/test_burm.py @ 321:8c569fbe60e4

Load yacc and burg dynamic
author Windel Bouwman
date Sun, 19 Jan 2014 18:48:45 +0100
parents python/test_burm.py@8d07a4254f04
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test_burm.py	Sun Jan 19 18:48:45 2014 +0100
@@ -0,0 +1,44 @@
+import unittest
+import io
+import argparse
+
+from tree import Tree
+import pyburg
+
+
+class testBURG(unittest.TestCase):
+    def testSample4(self):
+        """ Test sample4 burg system """
+        # Generate matcher from spec:
+        buf = io.StringIO()
+        args = argparse.Namespace(source=open('sample4.brg'), output=buf)
+        pyburg.main(args)
+
+        # Execute generated script into global scope:
+        exec(buf.getvalue(), globals())
+
+        # Sample tree:
+        t = Tree('ASGNI',
+             Tree('ADDRLP'),
+             Tree('ADDI',
+                  Tree('CVCI', Tree('INDIRC', Tree('ADDRLP'))),
+                  Tree('CNSTI')
+                 )
+            )
+
+        # Subclass generated matcher:
+        class MyMatcher(Matcher):
+            def __init__(self):
+                super().__init__()
+                self.trace = []
+
+            def tr(self, r):
+                self.trace.append(r)
+
+        # Match tree:
+        mm = MyMatcher()
+        mm.gen(t)
+        self.assertSequenceEqual([8,8,4,11,9,3,1], mm.trace)
+
+if __name__ == '__main__':
+    unittest.main()