[pypy-commit] lang-scheme default: New test: Run and assert tests at scheme level

boemmels noreply at buildbot.pypy.org
Sun Nov 27 00:55:27 CET 2011


Author: Juergen Boemmels <boemmels at web.de>
Branch: 
Changeset: r13:6ecdb8380916
Date: 2011-11-24 23:35 +0100
http://bitbucket.org/pypy/lang-scheme/changeset/6ecdb8380916/

Log:	New test: Run and assert tests at scheme level

	This test creates an Scheme level assert macro, which passes the
	execption to python level.

diff --git a/scheme/test/test_scheme_level.py b/scheme/test/test_scheme_level.py
new file mode 100644
--- /dev/null
+++ b/scheme/test/test_scheme_level.py
@@ -0,0 +1,71 @@
+import py
+from scheme.ssparser import parse
+from scheme.execution import ExecutionContext
+from scheme.object import *
+
+# A scheme level macro, which raises an AssertionError at python
+# level. This python level Errors are then reported by pytest.py
+
+class Assert(W_Macro):
+    def call(self, ctx, lst):
+        if not isinstance(lst, W_Pair):
+            raise SchemeSyntaxError
+        w_test = lst.car
+        if lst.cdr is w_nil:
+            comment = w_test.to_string()
+        else:
+            w_rest = lst.get_cdr_as_pair()
+            if w_rest.cdr is not w_nil:
+                raise WrongArgsNumber
+            comment = w_rest.car.to_string()
+
+        w_test_result = w_test.eval(ctx)
+        assert w_test_result.to_boolean(), comment
+
+        return w_undefined
+
+w_assert = Assert("assert")
+
+def run_with_assert(text):
+    ctx = ExecutionContext()
+    ctx.set("assert", w_assert)
+    
+    for stmt in parse(text):
+        w_result = stmt.eval(ctx)
+
+    return w_result
+
+def test_assert():
+    run_with_assert(r'(assert #t "No fail for passed assert")')
+    run_with_assert(r'(assert (or #f #t))')
+    py.test.raises(AssertionError, run_with_assert,
+        r'(assert #f "Failed assert raises")')
+    py.test.raises(AssertionError, run_with_assert,
+        r'(define foo #f) (+ 1 1) (assert foo "more complex test")')
+
+def test_fac():
+    run_with_assert(r"""
+(define (fac n)
+  (if (eqv? n 0)
+      1
+      (* n (fac (- n 1)))
+  )
+)
+(assert (eqv? (fac 1) 1))
+(assert (eqv? (fac 5) 120))
+""")
+
+def test_fac_with_acc():
+    run_with_assert(r"""
+(define (fac n)
+  (define (fac-acc n acc)
+    (if (eqv? n 0)
+        acc
+        (fac-acc (- n 1) (* n acc))
+    )
+  )
+  (fac-acc n 1)
+)
+(assert (eqv? (fac 1) 1))
+(assert (eqv? (fac 5) 120))
+""")


More information about the pypy-commit mailing list