def __init__(self, backend=None): self._pins = {} #: pin: mode self.onRising = Event() self.onFalling = Event() if backend and issubclass(backend, GpioInterface): self._backend = backend(self) elif backend: raise RuntimeError('Invalid backend interface') else: self._backend = NativeBackend(self) atexit.register(self.cleanup)
def test_repr(self): "Return a nice summary of the args when printed" e = Event('one', 'two', 'three') actual = repr(e) self.assertTrue("one" in actual) self.assertTrue("two" in actual) self.assertTrue("three" in actual)
def test(self): e = Event() e += lambda one, two, three: (three, two, one) e += lambda one, two, three, four: (three, four) actual = list(e(1, 2, 3, 4, 5, 6)) expected = [(3, 2, 1), (3, 4)] self.assertEqual(actual, expected)
def add(self, handler): argcount = handler.__code__.co_argcount argnames = handler.__code__.co_varnames[:argcount] # remove 'self' if isinstance(handler, types.MethodType): argnames = argnames[1:] check_missing(self.args, argnames) return Event.add(self, handler)
def test(self): e = Event('one', 'two', 'three') e += lambda one, two, three: (one, two, three) e += lambda one, two, three: (three, one) actual = list(e(1, 2, 3)) expected = [ (1, 2, 3), (3, 1) ] self.assertEqual(actual, expected)
def test_method(self): "Methods add an extra arg 'self' and should not be counted" e = Event("one", "two") class target(object): def callme(self, a, b): return self, a, b o = target() e += o.callme actual = list(e(1, 2)) expected = [ (o, 1, 2) ] self.assertEqual(actual, expected)
def test_local_vars(self): """ Determine args from locals previously 'three' would throw an ArgsError because it was believed to be an argument and therefore not matching the strict args requirement """ e = Event('one', 'two') def has_locals(one, two): three = 3 return one, three e += has_locals actual = list(e(1, 2)) expected = [ (1, 3) ] self.assertEqual(actual, expected)
class Event_test(unittest.TestCase): def setUp(self): self.event = Event() def tearDown(self): self.event = None def test_regular(self): self.event.add(lambda s, a: a) self.event.add(lambda s, b: b) actual = self.event.fire(self, "a") expected = ('a', 'a') self.assertEqual(actual, expected) def test_magic(self): self.event += lambda s, a: a self.event += lambda s, b: b actual = self.event(self, "a") expected = ('a', 'a') self.assertEqual(actual, expected) def test_clear(self): expected1 = lambda a: a expected2 = lambda a: a self.event += expected1 self.event += expected2 self.assertTrue(expected1 in self.event) self.assertTrue(expected2 in self.event) self.event.remove(expected1) self.assertTrue(expected1 not in self.event) self.assertTrue(expected2 in self.event) self.event.clear() self.assertTrue(expected1 not in self.event) self.assertTrue(expected2 not in self.event)
def __init__(self, *arg_names): if len(set(arg_names)) != len(arg_names): raise ArgsError("Cannot accept args of same name") self.args = arg_names Event.__init__(self)
def test_init_err(self): "Init should throw because of 'one' and 'one' being the same" self.assertRaises(ArgsError, lambda: Event('one', 'one', 'three'))
def setUp(self): self.event = Event()