def test_watched(): WatchableThing = expose_as("WatchableThing", Thing, "func") wt, spectator = watched(WatchableThing) assert isinstance(spectator, Spectator) assert hasattr(wt, "_instance_spectator") assert wt._instance_spectator is spectator
def __new__(cls, *args, **kwargs): self, spectator = watched(super().__new__, cls) for name in cls._model_controls: ctrl = getattr(self, name) for method in ctrl.methods: spectator.callback(method, ctrl.before, ctrl.after) object.__setattr__(self, "_model_views", []) return self
def test_method_spectator(): WatchableList = expose_as("WatchableList", list, "append") append = WatchableList.append assert append.basemethod is list.append assert append.name == "append" wl, spectator = watched(WatchableList) wl.append(1) wl.append(2) assert wl == [1, 2]
def test_watch(): WatchableThing = expose_as("WatchableThing", Thing, "func") wt = WatchableThing() spectator = watch(wt) assert isinstance(spectator, Spectator) assert hasattr(wt, "_instance_spectator") assert wt._instance_spectator is spectator WatchableList = expose_as("WatchableList", list, "append") wl, spectator = watched(WatchableList, [1, 2, 3]) assert wl == [1, 2, 3] assert isinstance(spectator, Spectator) assert hasattr(wl, "_instance_spectator") assert wl._instance_spectator is spectator
def test_callback_multiple(): class Test(object): def a(self): pass def b(self): pass WatchableTest = expose_as("WatchableTest", Test, "a", "b") wt, spectator = watched(WatchableTest) def callback(value, call): pass spectator.callback(("a", "b"), callback) for key in ("a", "b"): assert key in spectator._callback_registry assert spectator._callback_registry[key] == [(callback, None)] spectator.remove_callback(("a", "b"), callback) assert spectator._callback_registry == {}
def test_unwatch(): WatchableThing = expose_as("WatchableThing", Thing, "func") wt, spectator = watched(WatchableThing) out = unwatch(wt) assert not hasattr(wt, "_instance_spectator") assert out is spectator
def test_watcher(): WatchableThing = expose_as("WatchableThing", Thing, "func") wt, spectator = watched(WatchableThing) assert watcher(wt) is spectator
def test_method_spectator_signature(): WatchableThing = expose_as("WatchableThing", Thing, "func") thing, sectator = watched(WatchableThing) assert signature(Thing().func) == signature(thing.func)