[pypy-svn] r66640 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Sun Jul 26 19:12:15 CEST 2009


Author: david
Date: Sun Jul 26 19:12:13 2009
New Revision: 66640

Modified:
   pypy/branch/io-lang/pypy/lang/io/object.py
   pypy/branch/io-lang/pypy/lang/io/test/test_object.py
Log:
Object if method

Modified: pypy/branch/io-lang/pypy/lang/io/object.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/object.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/object.py	Sun Jul 26 19:12:13 2009
@@ -134,4 +134,17 @@
         w_value = w_message.arguments[0].eval(space, w_context, w_context)
     
     space.return_status(w_value)
-    return w_target
\ No newline at end of file
+    return w_target
+    
+ at register_method('Object', 'if')
+def object_if(space, w_target, w_message, w_context):
+    w_condition = w_message.arguments[0].eval(space, w_context, w_context)
+
+    if w_condition is space.w_true:
+        index = 1 
+    else:
+        index = 2
+        
+    if index < len(w_message.arguments):
+        return w_message.arguments[index].eval(space, w_context, w_context)
+    return w_condition
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_object.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_object.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_object.py	Sun Jul 26 19:12:13 2009
@@ -167,3 +167,32 @@
     x(99)"""
     res, space = interpret(inp)
     assert res.value == 1024
+
+def test_object_if():
+    inp = """a := list()
+    for(i, 1, 10, 
+        if(i == 3, continue)
+        a append(i))
+    a
+    """
+    res, space = interpret(inp)
+    values = [x.value for x in res.items]
+    assert values == [1, 2 ,3, 4, 5, 6, 7, 8, 9, 10]
+
+def test_object_if2():
+    inp = """if(false, 1, 2)"""
+    res, _ = interpret(inp)
+    assert res.value == 2
+    
+    inp = """if(true, 1, 2)"""
+    res, _ = interpret(inp)
+    assert res.value == 1
+    
+def test_object_if3():
+    inp = 'if(true)'
+    res, space = interpret(inp)
+    assert res is space.w_true
+    
+    inp = 'if(false)'
+    res, space = interpret(inp)
+    assert res is space.w_false
\ No newline at end of file



More information about the Pypy-commit mailing list