Author: Juergen Boemmels <boemm...@web.de>
Branch: 
Changeset: r4:afd4189fb68b
Date: 2011-09-04 21:49 +0200
http://bitbucket.org/pypy/lang-scheme/changeset/afd4189fb68b/

Log:    Implement display & newline

        Mostly just uncommenting the implementation. Added tests for display
        and newline

diff --git a/scheme/procedure.py b/scheme/procedure.py
--- a/scheme/procedure.py
+++ b/scheme/procedure.py
@@ -357,18 +357,28 @@
 ##
 # Input/Output procedures
 ##
-#class Display(W_Procedure):
-#    _symbol_name = "display"
-#
-#    def procedure(self, ctx, lst):
-#        if len(lst) == 1:
-#            obj = lst[0]
-#        elif len(lst) == 2:
-#            (obj, port) = lst
-#            raise NotImplementedError
-#        else:
-#            raise WrongArgsNumber
-#
-#        print obj.to_string()
-#        return w_undefined
+class Display(W_Procedure):
+    _symbol_name = "display"
 
+    def procedure(self, ctx, lst):
+        if len(lst) == 1:
+            obj = lst[0]
+        elif len(lst) == 2:
+            (obj, port) = lst
+            raise NotImplementedError
+        else:
+            raise WrongArgsNumber
+
+        print obj.to_string(),
+        return w_undefined
+
+class Newline(W_Procedure):
+    _symbol_name = "newline"
+
+    def procedure(self, ctx, lst):
+        if len(lst) != 0:
+            raise WrongArgsNumber
+
+        print
+        return w_undefined
+
diff --git a/scheme/test/test_output.py b/scheme/test/test_output.py
new file mode 100644
--- /dev/null
+++ b/scheme/test/test_output.py
@@ -0,0 +1,35 @@
+
+import sys
+from StringIO import StringIO
+from scheme.ssparser import parse
+from scheme.execution import ExecutionContext
+
+
+def capture_output(func):
+    s = StringIO()
+    so = sys.stdout
+    sys.stdout = s
+    try:
+        func()
+    finally:
+        sys.stdout = so
+    res = s.getvalue()
+    s.close()
+    return res
+
+def eval_noctx(expr):
+    return parse(expr)[0].eval(ExecutionContext())
+
+def test_display():
+    tests = [("(display 'foobar)", "foobar"),
+             ("(display 42)", "42"),
+             ("(display \"Hello World!\")", "Hello World!"),
+             ("(display '(1 2 3))", "(1 2 3)"),
+            ]
+    for code, expected in tests:
+        out = capture_output(lambda: eval_noctx(code))
+        assert out == expected
+
+def test_newline():
+    out = capture_output(lambda: eval_noctx("(newline)"))
+    assert out == "\n"
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to