[pypy-svn] r24285 - pypy/dist/pypy/translator/squeak/test

nik at codespeak.net nik at codespeak.net
Mon Mar 13 12:30:56 CET 2006


Author: nik
Date: Mon Mar 13 12:30:55 2006
New Revision: 24285

Added:
   pypy/dist/pypy/translator/squeak/test/runtest.py
Modified:
   pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
Log:
moved testing squeak gateway into its own module, for use by arbitrary
tests.


Added: pypy/dist/pypy/translator/squeak/test/runtest.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/squeak/test/runtest.py	Mon Mar 13 12:30:55 2006
@@ -0,0 +1,83 @@
+import os
+import py
+from pypy.tool.udir import udir
+from pypy.translator.squeak.gensqueak import GenSqueak, Selector
+from pypy.translator.translator import TranslationContext
+from pypy import conftest
+
+def compile_function(func, annotation=[]):
+    return SqueakFunction(func, annotation)
+
+
+# For now use pipes to communicate with squeak. This is very flaky
+# and only works for posix systems. At some later point we'll
+# probably need a socket based solution for this.
+startup_script = """
+| stdout src selector result arguments arg i |
+src := Smalltalk getSystemAttribute: 3.
+FileStream fileIn: src.
+selector := (Smalltalk getSystemAttribute: 4) asSymbol.
+arguments := OrderedCollection new.
+i := 4.
+[(arg := Smalltalk getSystemAttribute: (i := i + 1)) notNil]
+    whileTrue: [arguments add: arg asInteger].
+
+PyConstants setupConstants.
+result := (PyFunctions perform: selector withArguments: arguments asArray).
+stdout := StandardFileStream fileNamed: '/dev/stdout'.
+stdout nextPutAll: result asString.
+Smalltalk snapshot: false andQuit: true.
+"""
+
+class SqueakFunction:
+
+    def __init__(self, func, annotation):
+        self._func = func
+        self._gen = self._build(func, annotation)
+
+    def _build(self, func, annotation):
+        try: 
+            func = func.im_func
+        except AttributeError: 
+            pass
+        t = TranslationContext()
+        t.buildannotator().build_types(func, annotation)
+        t.buildrtyper(type_system="ootype").specialize()
+        if conftest.option.view:
+           t.viewcg()
+        return GenSqueak(udir, t)
+
+    def _write_startup(self):
+        startup_st = udir.join("startup.st")
+        try:
+            # Erm, py.path.local has no "exists" method?
+            startup_st.stat()
+        except py.error.ENOENT:
+            f = startup_st.open("w")
+            f.write(startup_script)
+            f.close()
+        return startup_st
+
+    def __call__(self, *args):
+        # NB: only integers arguments are supported currently
+        try:
+            import posix
+        except ImportError:
+            py.test.skip("Squeak tests only work on Unix right now.")
+        try:
+            py.path.local.sysfind("squeak")
+        except py.error.ENOENT:
+            py.test.skip("Squeak is not on your path.")
+        if os.getenv("SQUEAK_IMAGE") is None:
+            py.test.skip("Squeak tests expect the SQUEAK_IMAGE environment "
+                    "variable to point to an image.")
+        startup_st = self._write_startup()
+        cmd = 'squeak -headless -- %s %s "%s" %s' \
+                % (startup_st, udir.join(self._gen.filename),
+                   Selector(self._func.__name__, len(args)).symbol(),
+                   " ".join(['"%s"' % a for a in args]))
+        squeak_process = os.popen(cmd)
+        result = squeak_process.read()
+        assert squeak_process.close() is None # exit status was 0
+        return result
+

Modified: pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	(original)
+++ pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	Mon Mar 13 12:30:55 2006
@@ -1,10 +1,8 @@
 import os
 import py
-from pypy.tool.udir import udir
 from pypy.translator.test import snippet
-from pypy.translator.squeak.gensqueak import GenSqueak, Selector, camel_case
-from pypy.translator.translator import TranslationContext
-from pypy import conftest
+from pypy.translator.squeak.gensqueak import Selector, camel_case
+from pypy.translator.squeak.test.runtest import compile_function
 
 
 def looping(i, j):
@@ -13,89 +11,28 @@
         while j > 0:
             j -= 1
 
-def build_sqfunc(func, args=[]):
-    try: func = func.im_func
-    except AttributeError: pass
-    t = TranslationContext()
-    t.buildannotator().build_types(func, args)
-    if conftest.option.view:
-       t.viewcg()
-    t.buildrtyper(type_system="ootype").specialize()
-    if conftest.option.view:
-       t.viewcg()
-    return GenSqueak(udir, t)
-
 class TestSqueakTrans:
 
     def test_simple_func(self):
-        build_sqfunc(snippet.simple_func, [int])
+        compile_function(snippet.simple_func, [int])
 
     def test_if_then_else(self):
-        build_sqfunc(snippet.if_then_else, [bool, int, int])
+        compile_function(snippet.if_then_else, [bool, int, int])
 
     def test_my_gcd(self):
-        build_sqfunc(snippet.my_gcd, [int, int])
+        compile_function(snippet.my_gcd, [int, int])
 
     def test_looping(self):
-        build_sqfunc(looping, [int, int])
-
+        compile_function(looping, [int, int])
 
-# For now use pipes to communicate with squeak. This is very flaky
-# and only works for posix systems. At some later point we'll
-# probably need a socket based solution for this.
-startup_script = """
-| stdout src selector result arguments arg i |
-src := Smalltalk getSystemAttribute: 3.
-FileStream fileIn: src.
-selector := (Smalltalk getSystemAttribute: 4) asSymbol.
-arguments := OrderedCollection new.
-i := 4.
-[(arg := Smalltalk getSystemAttribute: (i := i + 1)) notNil]
-    whileTrue: [arguments add: arg asInteger].
-
-PyConstants setupConstants.
-result := (PyFunctions perform: selector withArguments: arguments asArray).
-stdout := StandardFileStream fileNamed: '/dev/stdout'.
-stdout nextPutAll: result asString.
-Smalltalk snapshot: false andQuit: true.
-"""
 
 class TestGenSqueak:
 
-    def setup_class(self):
-        self.startup_st = udir.join("startup.st")
-        f = self.startup_st.open("w")
-        f.write(startup_script)
-        f.close()
-
-    def run_on_squeak(self, function, *args):
-        # NB: only integers arguments are supported currently
-        try:
-            import posix
-        except ImportError:
-            py.test.skip("Squeak tests only work on Unix right now.")
-        try:
-            py.path.local.sysfind("squeak")
-        except py.error.ENOENT:
-            py.test.skip("Squeak is not on your path.")
-        if os.getenv("SQUEAK_IMAGE") is None:
-            py.test.skip("Squeak tests expect the SQUEAK_IMAGE environment "
-                    "variable to point to an image.")
-        arg_types = [type(arg) for arg in args]
-        gen_squeak = build_sqfunc(function, arg_types)
-        cmd = 'squeak -headless -- %s %s "%s" %s' \
-                % (self.startup_st, udir.join(gen_squeak.filename),
-                   Selector(function.__name__, len(args)).symbol(),
-                   " ".join(['"%s"' % a for a in args]))
-        squeak_process = os.popen(cmd)
-        result = squeak_process.read()
-        assert squeak_process.close() is None # exit status was 0
-        return result
-
     def test_theanswer(self):
         def theanswer():
             return 42
-        assert self.run_on_squeak(theanswer) == "42"
+        fn = compile_function(theanswer)
+        assert fn() == "42"
 
     def test_simplemethod(self):
         class A:
@@ -103,12 +40,14 @@
                 return 42
         def simplemethod():
             return A().m()
-        assert self.run_on_squeak(simplemethod) == "42"
+        fn = compile_function(simplemethod)
+        assert fn() == "42"
 
     def test_argfunction(self):
         def function(i, j=2):
             return i + j
-        assert self.run_on_squeak(function, 1, 3) == "4"
+        fn = compile_function(function, [int, int])
+        assert fn(1, 3) == "4"
 
     def test_argmethod(self):
         class A:
@@ -116,7 +55,8 @@
                 return i + j + h
         def simplemethod(i):
             return A().m(i, j=3)
-        assert self.run_on_squeak(simplemethod, 1) == "6"
+        fn = compile_function(simplemethod, [int])
+        assert fn(1) == "6"
 
     def test_nameclash_classes(self):
         from pypy.translator.squeak.test.support import A as A2
@@ -124,7 +64,8 @@
             def m(self, i): return 2 + i
         def f():
             return A().m(0) + A2().m(0)
-        assert self.run_on_squeak(f) == "3"
+        fn = compile_function(f)
+        assert fn() == "3"
 
     def test_nameclash_classes_mean(self):
         class A:
@@ -134,7 +75,8 @@
             def m(self, i): return 2 + i
         def f():
             return A().m(0) + A2().m(0)
-        assert self.run_on_squeak(f) == "3"
+        fn = compile_function(f)
+        assert fn() == "3"
 
     def test_nameclash_camel_case(self):
         class ASomething:
@@ -144,7 +86,8 @@
         def f():
             x = ASomething().m(0) + A_Something().m(0)
             return x + ASomething().m(0) + A_Something().m(0)
-        assert self.run_on_squeak(f) == "6"
+        fn = compile_function(f)
+        assert fn() == "6"
 
     def test_nameclash_functions(self):
         from pypy.translator.squeak.test.support import f as f2
@@ -152,7 +95,8 @@
             return i + 2
         def g():
             return f(0) + f2(0)
-        assert self.run_on_squeak(g) == "3"
+        fn = compile_function(g)
+        assert fn() == "3"
 
     def test_direct_call(self):
         def h(i):
@@ -161,7 +105,8 @@
             return i + 1 
         def f(i):
             return h(i) + g(i)
-        assert self.run_on_squeak(f, 1) == "5"
+        fn = compile_function(f, [int])
+        assert fn(1) == "5"
 
     def test_getfield_setfield(self):
         class A:
@@ -176,7 +121,8 @@
             a.i_var = 3
             a.inc()
             return i + a.i_var
-        assert self.run_on_squeak(f, 2) == "6"
+        fn = compile_function(f, [int])
+        assert fn(2) == "6"
 
     def test_classvars(self):
         class A: i = 1
@@ -190,8 +136,9 @@
         def f(i):
             c = pick(i)
             return c.i
-        assert self.run_on_squeak(f, 1) == "1"
-        assert self.run_on_squeak(f, 2) == "2"
+        fn = compile_function(f, [int])
+        assert fn(1) == "1"
+        assert fn(2) == "2"
 
 
 class TestSelector:



More information about the Pypy-commit mailing list