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

Method test_callbacks

tests/test_traitlets.py:2097–2138  ·  view source on GitHub ↗

Verify two linked traitlets have their callbacks called once.

(self)

Source from the content-addressed store, hash-verified

2095 self.assertEqual(a.value, b.value)
2096
2097 def test_callbacks(self):
2098 """Verify two linked traitlets have their callbacks called once."""
2099
2100 # Create two simple classes with Int traitlets.
2101 class A(HasTraits):
2102 value = Int()
2103
2104 class B(HasTraits):
2105 count = Int()
2106
2107 a = A(value=9)
2108 b = B(count=8)
2109
2110 # Register callbacks that count.
2111 callback_count = []
2112
2113 def a_callback(name, old, new):
2114 callback_count.append("a")
2115
2116 a.on_trait_change(a_callback, "value")
2117
2118 def b_callback(name, old, new):
2119 callback_count.append("b")
2120
2121 b.on_trait_change(b_callback, "count")
2122
2123 # Connect the two classes.
2124 c = link((a, "value"), (b, "count"))
2125
2126 # Make sure b's count was set to a's value once.
2127 self.assertEqual("".join(callback_count), "b")
2128 del callback_count[:]
2129
2130 # Make sure a's value was set to b's count once.
2131 b.count = 5
2132 self.assertEqual("".join(callback_count), "ba")
2133 del callback_count[:]
2134
2135 # Make sure b's count was set to a's value once.
2136 a.value = 4
2137 self.assertEqual("".join(callback_count), "ab")
2138 del callback_count[:]
2139
2140 def test_tranform(self):
2141 """Test transform link."""

Callers

nothing calls this directly

Calls 4

linkClass · 0.90
on_trait_changeMethod · 0.80
AClass · 0.70
BClass · 0.70

Tested by

no test coverage detected