Source code for yyagl.tests.test_observer

from pathlib import Path
import sys
if '' in sys.path: sys.path.remove('')
sys.path.append(str(Path(__file__).parent.parent.parent))
from unittest import TestCase
from unittest.mock import MagicMock
from yyagl.observer import Subject


[docs]class Observed(Subject): pass
[docs]class Observer: def __init__(self, observed): self.__observed = observed
[docs] def cb(self): pass
[docs]class ObserverTests(TestCase):
[docs] def test_all(self): observed = Observed() observer = Observer(observed) observer.cb = MagicMock(side_effect=observer.cb) observer.cb.__name__ = 'cb' self.assertFalse(observed.observing(observer.cb)) observed.attach(observer.cb) self.assertTrue(observed.observing(observer.cb)) observer.cb.assert_not_called() observed.notify('cb') observer.cb.assert_called() observed.detach(observer.cb) self.assertFalse(observed.observing(observer.cb))