def test_emitter_python_only(): m = MyObject() # Emitter decorator needs proper callable with raises(TypeError): event.emitter(3) if '__pypy__' in sys.builtin_module_names: pass # skip else: with raises(TypeError): event.emitter(isinstance) # Check type of the instance attribute assert isinstance(m.foo, event._emitter.Emitter) # Cannot set or delete an emitter with raises(AttributeError): m.foo = 3 with raises(AttributeError): del m.foo # Repr and docs assert 'emitter' in repr(m.__class__.foo).lower() assert 'emitter' in repr(m.foo).lower() assert 'foo' in repr(m.foo)
def test_emitter_python_only(): m = MyObject() # Emitter decorator needs proper callable with raises(TypeError): event.emitter(3) with raises((TypeError, ValueError)): # CPython, Pypy event.emitter(isinstance) # Check type of the instance attribute assert isinstance(m.foo, event._emitter.Emitter) # Cannot set or delete an emitter with raises(AttributeError): m.foo = 3 with raises(AttributeError): del m.foo # Repr and docs assert 'emitter' in repr(m.__class__.foo).lower() assert 'emitter' in repr(m.foo).lower() assert 'foo' in repr(m.foo)
def test_emitter(): class MyObject(event.HasEvents): @event.emitter def foo(self, v): return dict(value=float(v)) @event.emitter def bar(self, v): return dict(value=float(v)+1) # note plus 1 @event.connect('foo') def on_foo(self, *events): self.the_val = events[0].value # so we can test it @event.connect('bar') def on_bar(self, *events): self.the_val = events[0].value # so we can test it m = MyObject() the_vals = [] @m.connect('foo', 'bar') def handle_foo(*events): the_vals.append(events[0].value) with event.loop: m.foo(3.2) assert m.the_val == 3.2 assert the_vals[-1] == 3.2 with event.loop: m.foo('9.1') assert m.the_val == 9.1 assert the_vals[-1] == 9.1 with event.loop: m.bar(3.2) assert m.the_val == 4.2 assert the_vals[-1] == 4.2 # Fail with raises(ValueError): m.foo('bla') with raises(TypeError): event.emitter(3) # emitter decorator needs callable with raises(AttributeError): del m.foo # cannot delete an emitter with raises(AttributeError): m.foo = None # cannot set an emitter class MyObject2(event.HasEvents): @event.emitter def foo(self, v): return float(v) with raises(TypeError): m = MyObject2() m.foo(3.2) # return value of emitter must be a dict