328999c5f368 — Gerald Klix (delle) 9 years ago
SUM: Got indenting right.
FIX: Completely messed up indenting behaviour.
ADD: A doctest based unit-test for the aforementioned problem.
CHG: No "%" expansion takes please if no arguments are passed.
NTE: This is an incompatible change to `Writer`.
3 files changed, 164 insertions(+), 6 deletions(-)

M gf/go.py
A => tests/textest.py
A => tests/writer_simple.tst
M gf/go.py +15 -6
@@ 166,7 166,10 @@ def __call__(writer):
 @variadic_method(Writer, basestring)
 def __call__(writer, text, *arguments):
     """Write text % arguments on the file-like object."""
-    writer.file.write(text % arguments)
+    if arguments:  # FIXME: Restore backward compatibility
+        writer.file.write(text % arguments)
+    else:
+        writer.file.write(text)
 
 @method(Writer)
 def push(writer):

          
@@ 209,18 212,24 @@ def __call__(writer):
 
 @variadic_method(IndentingWriter, basestring)
 def __call__(writer, text, *arguments):
-    writer.only_newline = False
     if arguments:
         text = text % arguments
     text_list = text.split("\n")
     base_call =  __call__.super(Writer, basestring)
+    sub_text = text_list[0]
     if writer.only_newline:
         base_call(writer, writer.indent)
-    base_call(writer, text_list[0])
+    if sub_text:
+        base_call(writer, sub_text)
+        writer.only_newline = False
+    else:
+        writer.only_newline = True
     for sub_text in text_list[1:]:
-        __call__.super(Writer)(writer)
-        base_call(writer, writer.indent)
-        base_call(writer, sub_text)
+        writer()
+        if sub_text:
+            base_call(writer, writer.indent)
+            base_call(writer, sub_text)
+            writer.only_newline = False
 
 @method(IndentingWriter, int)
 def push(writer, steps):

          
A => tests/textest.py +54 -0
@@ 0,0 1,54 @@ 
+"""Test scanner and parser of macro talk."""
+
+from unittest import TestCase as BaseTestCase
+from doctest import (DocFileSuite, set_unittest_reportflags,
+        REPORT_NDIFF, REPORT_ONLY_FIRST_FAILURE)
+from glob import glob
+
+import os, sys
+sys.path.insert(0,
+        os.path.abspath(
+            os.path.normpath(
+                os.path.join(
+                    os.path.basename(__file__), os.pardir))))
+
+from gf import IndentingWriter, push, pop
+from gf.go import get_text
+
+test_directory = os.path.abspath(
+        os.path.normpath(os.path.dirname(__file__ )))
+
+
+iw = IndentingWriter()
+
+def show_text():
+    global iw
+    print get_text(iw)
+    iw = IndentingWriter()
+
+
+test_functions = dict(
+        w=lambda *arguments: iw(*arguments),
+        push=lambda *arguments: push(iw, *arguments),
+        pop=lambda *arguments: pop(iw, *arguments),
+        st=show_text)
+
+suite = DocFileSuite(*glob(os.path.join(test_directory, "*.tst")),
+        **dict(module_relative = False, globs=test_functions))
+
+
+if __name__ == "__main__":
+    from unittest import TextTestRunner
+    argv = []
+    for arg in sys.argv[1:]:
+        if arg == "-d":
+            set_unittest_reportflags(REPORT_NDIFF)
+        elif arg == "-f":
+            set_unittest_reportflags(REPORT_ONLY_FIRST_FAILURE)
+        else:
+            argv.append(arg)
+    if argv:
+        suite = DocFileSuite(*argv,
+                **dict(module_relative=False,
+                    globs=test_functions))
+    TextTestRunner().run(suite)

          
A => tests/writer_simple.tst +95 -0
@@ 0,0 1,95 @@ 
+>>> w("sepp")
+>>> st()
+sepp
+
+>>> w()
+>>> st()
+<BLANKLINE>
+<BLANKLINE>
+
+>>> w("1")
+>>> st()
+1
+
+>>> w("%r %03d", "sepp", 10)
+>>> st()
+'sepp' 010
+
+>>> w("1")
+>>> w("2")
+>>> w("3")
+>>> st()
+123
+
+>>> w("1")
+>>> push()
+>>> w()
+>>> w("2")
+>>> w()
+>>> pop()
+>>> w("3")
+>>> st()
+1
+    2
+3
+
+>>> w("f(%s)", "arg")
+>>> w(); w("{")
+>>> push()
+>>> w()
+>>> w(r'printf("%%i\n", %d);', 42)
+>>> w()
+>>> w("return 0;")
+>>> pop()
+>>> w()
+>>> w("}")
+>>> st()
+f(arg)
+{
+    printf("%i\n", 42);
+    return 0;
+}
+
+>>> w("0"); push(); w(); w("1"); push(); w(); w("2")
+>>> pop(); pop()
+>>> w(); w("3")
+>>> st()
+0
+    1
+        2
+3
+
+>>> w("0"); push(); w(); w("1"); push(); w(); w("2")
+>>> pop(2)
+>>> w(); w("3")
+>>> st()
+0
+    1
+        2
+3
+
+>>> w("0"); push(2); w(); w("1"); push(2); w(); w("2")
+>>> pop(2)
+>>> w(); w("3")
+>>> st()
+0
+        1
+                2
+3
+
+>>> w("0"); push(); w(); w("1\n"); pop(); w("2")
+>>> st()
+0
+    1
+2
+
+>>> push(); w("0\n"); pop(); w("1")
+>>> st()
+0
+1
+
+>>> push(); w(); w("0\n"); pop(); w("1")
+>>> st()
+<BLANKLINE>
+    0
+1