예제 #1
0
 def test_trait_method_optional_true(self):
     # Test the instance method calls the top-level function correctly.
     expr = expression.trait("name").trait("attr", optional=True)
     expected = [
         create_graph(
             NamedTraitObserver(name="name", notify=True, optional=False),
             NamedTraitObserver(name="attr", notify=True, optional=True),
         ),
     ]
     actual = expr._as_graphs()
     self.assertEqual(actual, expected)
예제 #2
0
 def test_not_equal_type(self):
     observer = NamedTraitObserver(name="foo", notify=True, optional=True)
     imposter = mock.Mock()
     imposter.name = "foo"
     imposter.notify = True
     imposter.optional = True
     self.assertNotEqual(observer, imposter)
예제 #3
0
def trait(name, notify=True, optional=False):
    """ Create a new expression for observing a trait with the exact
    name given.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    Parameters
    ----------
    name : str
        Name of the trait to match.
    notify : bool, optional
        Whether to notify for changes. Default is to notify.
    optional : bool, optional
        If true, skip this observer if the requested trait is not found.
        Default is false, and an error will be raised if the requested
        trait is not found.

    Returns
    -------
    new_expression : ObserverExpression
    """
    observer = NamedTraitObserver(
        name=name, notify=notify, optional=optional)
    return SingleObserverExpression(observer)
예제 #4
0
 def test_trait_name_optional_true(self):
     # Test the top-level function
     expr = expression.trait("name", optional=True)
     expected = [
         create_graph(
             NamedTraitObserver(name="name", notify=True, optional=True)),
     ]
     actual = expr._as_graphs()
     self.assertEqual(actual, expected)
예제 #5
0
def create_observer(**kwargs):
    """ Convenient function for creating an instance of NamedTraitObserver.
    """
    values = dict(
        name="name",
        notify=True,
        optional=False,
    )
    values.update(kwargs)
    return NamedTraitObserver(**values)
예제 #6
0
 def test_equal_observers(self):
     observer1 = NamedTraitObserver(name="foo", notify=True, optional=True)
     observer2 = NamedTraitObserver(name="foo", notify=True, optional=True)
     self.assertEqual(observer1, observer2)
     self.assertEqual(hash(observer1), hash(observer2))
예제 #7
0
 def test_not_equal_optional(self):
     observer1 = NamedTraitObserver(name="foo", notify=True, optional=False)
     observer2 = NamedTraitObserver(name="foo", notify=True, optional=True)
     self.assertNotEqual(observer1, observer2)