# HG changeset patch # User Gerald Klix (delle) # Date 1406050273 -7200 # Tue Jul 22 19:31:13 2014 +0200 # Branch Rel0.1.5 # Node ID 328999c5f36802854cbcadfd14582bf78a533d49 # Parent 614c58dca6aa2149c68d1faf0fd36d62e0f7a979 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`. diff --git a/gf/go.py b/gf/go.py --- a/gf/go.py +++ b/gf/go.py @@ -166,7 +166,10 @@ @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 @@ @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): diff --git a/tests/textest.py b/tests/textest.py new file mode 100644 --- /dev/null +++ b/tests/textest.py @@ -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) diff --git a/tests/writer_simple.tst b/tests/writer_simple.tst new file mode 100644 --- /dev/null +++ b/tests/writer_simple.tst @@ -0,0 +1,95 @@ +>>> w("sepp") +>>> st() +sepp + +>>> w() +>>> st() + + + +>>> 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() + + 0 +1