[pypy-commit] pypy fix-gen-dfa: refactored output function to be tested more easily

plan_rich pypy.commits at gmail.com
Sun May 22 12:28:11 EDT 2016


Author: Richard Plangger <planrichi at gmail.com>
Branch: fix-gen-dfa
Changeset: r84574:fa9847af949c
Date: 2016-05-22 18:27 +0200
http://bitbucket.org/pypy/pypy/changeset/fa9847af949c/

Log:	refactored output function to be tested more easily added not yet
	complete test for the same function

diff --git a/pypy/interpreter/pyparser/genpytokenize.py b/pypy/interpreter/pyparser/genpytokenize.py
--- a/pypy/interpreter/pyparser/genpytokenize.py
+++ b/pypy/interpreter/pyparser/genpytokenize.py
@@ -265,20 +265,25 @@
 
 def output(name, dfa_class, dfa):
     import textwrap
+    lines = []
     i = 0
     for line in textwrap.wrap(repr(dfa.accepts), width = 50):
         if i == 0:
-            print "accepts =", line
+            lines.append("accepts = ")
         else:
-            print "          ", line
+            lines.append("          ")
+        lines.append(line)
+        lines.append("\n")
         i += 1
     import StringIO
-    print "states = ["
+    lines.append("states = [\n")
     for numstate, state in enumerate(dfa.states):
-        print "    #", numstate
+        lines.append("    #")
+        lines.append(str(numstate))
+        lines.append('\n')
         s = StringIO.StringIO()
         i = 0
-        for k, v in sorted(state.items()):
+        for k, v in enumerate(state):
             i += 1
             if k == '\x00default':
                 k = "automata.DEFAULT"
@@ -298,22 +303,24 @@
         for line in text:
             line = line.replace('::', ': ')
             if i == 0:
-                print '    {' + line
+                lines.append('    {')
             else:
-                print '     ' + line
+                lines.append('     ')
+            lines.append(line)
+            lines.append('\n')
             i += 1
-    print "    ]"
-    print "%s = automata.%s(states, accepts)" % (name, dfa_class)
-    print
+    lines.append("    ]\n")
+    lines.append("%s = automata.%s(states, accepts)\n\n" % (name, dfa_class))
+    return ''.join(lines)
 
 def main ():
     pseudoDFA = makePyPseudoDFA()
-    output("pseudoDFA", "DFA", pseudoDFA)
+    print output("pseudoDFA", "DFA", pseudoDFA)
     endDFAMap = makePyEndDFAMap()
-    output("double3DFA", "NonGreedyDFA", endDFAMap['"""'])
-    output("single3DFA", "NonGreedyDFA", endDFAMap["'''"])
-    output("singleDFA", "DFA", endDFAMap["'"])
-    output("doubleDFA", "DFA", endDFAMap['"'])
+    print output("double3DFA", "NonGreedyDFA", endDFAMap['"""'])
+    print output("single3DFA", "NonGreedyDFA", endDFAMap["'''"])
+    print output("singleDFA", "DFA", endDFAMap["'"])
+    print output("doubleDFA", "DFA", endDFAMap['"'])
 
 # ______________________________________________________________________
 
diff --git a/pypy/interpreter/pyparser/test/test_gendfa.py b/pypy/interpreter/pyparser/test/test_gendfa.py
new file mode 100644
--- /dev/null
+++ b/pypy/interpreter/pyparser/test/test_gendfa.py
@@ -0,0 +1,12 @@
+from pypy.interpreter.pyparser.automata import DFA, DEFAULT
+from pypy.interpreter.pyparser.genpytokenize import output
+
+def test_states():
+    d = DFA([{"\x00": 1}, {"\x01": 0}], [False, True])
+    assert output('test', DFA, d) == """\
+accepts = [False, True]
+states = [
+    ]
+test = automata.pypy.interpreter.pyparser.automata.DFA(states, accepts)
+
+"""


More information about the pypy-commit mailing list