def test_dict_items_method_optional(self): # Test the instance method calls the top-level function correctly. expr = expression.dict_items().dict_items(optional=True) expected = [ create_graph( DictItemObserver(notify=True, optional=False), DictItemObserver(notify=True, optional=True), ), ] actual = expr._as_graphs() self.assertEqual(actual, expected)
def test_dict_items_optional_true(self): expr = expression.dict_items(optional=True) expected = [ create_graph(DictItemObserver(notify=True, optional=True), ), ] actual = expr._as_graphs() self.assertEqual(actual, expected)
def dict_items(notify=True, optional=False): """ Create a new expression for observing items inside a dict. Events emitted (if any) will be instances of :class:`~traits.observation.events.DictChangeEvent`. If an expression with ``dict_items`` is further extended, the **values** of the dict will be given to the next item in the expression. For example, the following observes a trait named "number" on any object that is one of the values in the dict named "mapping":: trait("mapping").dict_items().trait("number") Parameters ---------- notify : bool, optional Whether to notify for changes. Default is to notify. optional : bool, optional Whether to ignore this if the upstream object is not a dict. Default is false and an error will be raised if the object is not a dict. Returns ------- new_expression : ObserverExpression """ observer = DictItemObserver(notify=notify, optional=optional) return SingleObserverExpression(observer)
def create_observer(**kwargs): """ Convenience function for creating DictItemObserver with default values. """ values = dict( notify=True, optional=False, ) values.update(kwargs) return DictItemObserver(**values)
def test_equal_observers(self): observer1 = DictItemObserver(notify=False, optional=False) observer2 = DictItemObserver(notify=False, optional=False) self.assertEqual(observer1, observer2) self.assertEqual(hash(observer1), hash(observer2))
def test_not_equal_different_type(self): observer1 = DictItemObserver(notify=False, optional=False) imposter = mock.Mock() imposter.notify = False imposter.optional = False self.assertNotEqual(observer1, imposter)
def test_not_equal_optional(self): observer1 = DictItemObserver(notify=True, optional=True) observer2 = DictItemObserver(notify=True, optional=False) self.assertNotEqual(observer1, observer2)