MCPcopy Create free account
hub / github.com/ipython/traitlets / test_notify_all

Method test_notify_all

tests/test_traitlets.py:2559–2600  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2557
2558 @t.no_type_check
2559 def test_notify_all(self):
2560 class A(HasTraits):
2561 pass
2562
2563 a = A()
2564 self.assertTrue(not hasattr(a, "x"))
2565 self.assertTrue(not hasattr(a, "y"))
2566
2567 # Dynamically add trait x.
2568 a.add_traits(x=Int())
2569 self.assertTrue(hasattr(a, "x"))
2570 self.assertTrue(isinstance(a, (A,)))
2571
2572 # Dynamically add trait y.
2573 a.add_traits(y=Float())
2574 self.assertTrue(hasattr(a, "y"))
2575 self.assertTrue(isinstance(a, (A,)))
2576 self.assertEqual(a.__class__.__name__, A.__name__)
2577
2578 # Create a new instance and verify that x and y
2579 # aren't defined.
2580 b = A()
2581 self.assertTrue(not hasattr(b, "x"))
2582 self.assertTrue(not hasattr(b, "y"))
2583
2584 # Verify that notification works like normal.
2585 a.on_trait_change(self.notify1)
2586 a.x = 0
2587 self.assertEqual(len(self._notify1), 0)
2588 a.y = 0.0
2589 self.assertEqual(len(self._notify1), 0)
2590 a.x = 10
2591 self.assertTrue(("x", 0, 10) in self._notify1)
2592 a.y = 10.0
2593 self.assertTrue(("y", 0.0, 10.0) in self._notify1)
2594 self.assertRaises(TraitError, setattr, a, "x", "bad string")
2595 self.assertRaises(TraitError, setattr, a, "y", "bad string")
2596 self._notify1 = []
2597 a.on_trait_change(self.notify1, remove=True)
2598 a.x = 20
2599 a.y = 20.0
2600 self.assertEqual(len(self._notify1), 0)
2601
2602
2603def test_enum_no_default():

Callers

nothing calls this directly

Calls 5

IntClass · 0.90
FloatClass · 0.90
add_traitsMethod · 0.80
on_trait_changeMethod · 0.80
AClass · 0.70

Tested by

no test coverage detected