Commits

David Schneider committed e3b24ae

enable io implementation of newSlot

  • Participants
  • Parent commits 666653c

Comments (0)

Files changed (3)

io/io/A2_Object.io

 	and sets it's default the value aValue. Returns self. For example,
 	newSlot("foo", 1) would create slot named foo with the value 1 as well as a setter method setFoo().
 	*/
-/*
 	newSlot := method(name, value, doc,
 		getSlot("self") setSlot(name, getSlot("value"))
 		getSlot("self") setSlot("set" .. name asCapitalized,
 			//if(doc, getSlot("self") docSlot(name, doc))
 		getSlot("value")
 	)
-*/
 	//doc Object launchFile(pathString) Eval file at pathString as if from the command line in it's folder.
   //doc System launchPath Returns a pathComponent of the launch file.
 	launchFile := method(path, args,
     ast = parse(space, code)
     return ast.eval(space, w_target, w_target)
 
-# XXX replace with the original one in A2_Object.io when it works
-from io.model import W_Object
-class W_setSlotFunction(W_Object):
-    def __init__(self, space, name):
-        W_Object.__init__(self, space)
-        self.name = name
-
-    def apply(self, space, w_receiver, w_message, w_context):
-        w_receiver.slots[self.name] = w_message.arguments[0].eval(space, w_context,
-                                                             w_receiver)
-        return w_receiver
-@register_method('Object', 'newSlot', unwrap_spec=[object, str, object])
-def object_new_slot(space, w_target, name, w_value):
-    from io.model import W_CFunction
-    w_target.slots[name] = w_value
-    slot_name = 'set%s%s' % (name[0].upper(), name[1:])
-    w_target.slots[slot_name] = W_setSlotFunction(space, name)
-
 @register_method('Object', 'updateSlot', unwrap_spec=[object, str, object])
 def object_update_slot(space, w_target, slotname, w_value):
     assert w_target.lookup(slotname) is not None

io/test/test_object.py

     res, space = interpret(inp)
     assert res.number_value == 5
     assert space.w_lobby.slots['a'].number_value == 5
+
 def test_object_update_slot_raises():
     inp = 'qwer = 23'
     py.test.raises(Exception, 'interpret(inp)')
 
+def test_new_slot():
+    inp = """a := Object clone 
+    a foo ::= 45
+    a bar ::= 99
+    a setBar(123)
+    a
+    """
+    res, space = interpret(inp)
+    assert res.slots['foo'].number_value == 45
+    assert res.slots['bar'].number_value == 123
+    assert 'setFoo' in res.slots
+    assert 'setBar' in res.slots
+
+def test_new_slot_with_method():
+    inp = """a := Object clone 
+    a foo := method(setBar(123))
+    a bar ::= 99
+    a foo
+    a bar
+    """
+    res, space = interpret(inp)
+    assert res.number_value == 123
+
 def test_object_write():
     inp = """
     p := Object clone do(