()
| 2321 | |
| 2322 | |
| 2323 | def test_hold_trait_notifications(): |
| 2324 | changes = [] |
| 2325 | |
| 2326 | class Test(HasTraits): |
| 2327 | a = Integer(0) |
| 2328 | b = Integer(0) |
| 2329 | |
| 2330 | def _a_changed(self, name, old, new): |
| 2331 | changes.append((old, new)) |
| 2332 | |
| 2333 | def _b_validate(self, value, trait): |
| 2334 | if value != 0: |
| 2335 | raise TraitError("Only 0 is a valid value") |
| 2336 | return value |
| 2337 | |
| 2338 | # Test context manager and nesting |
| 2339 | t = Test() |
| 2340 | with t.hold_trait_notifications(): |
| 2341 | with t.hold_trait_notifications(): |
| 2342 | t.a = 1 |
| 2343 | assert t.a == 1 |
| 2344 | assert changes == [] |
| 2345 | t.a = 2 |
| 2346 | assert t.a == 2 |
| 2347 | with t.hold_trait_notifications(): |
| 2348 | t.a = 3 |
| 2349 | assert t.a == 3 |
| 2350 | assert changes == [] |
| 2351 | t.a = 4 |
| 2352 | assert t.a == 4 |
| 2353 | assert changes == [] |
| 2354 | t.a = 4 |
| 2355 | assert t.a == 4 |
| 2356 | assert changes == [] |
| 2357 | |
| 2358 | assert changes == [(0, 4)] |
| 2359 | # Test roll-back |
| 2360 | try: |
| 2361 | with t.hold_trait_notifications(): |
| 2362 | t.b = 1 # raises a Trait error |
| 2363 | except Exception: |
| 2364 | pass |
| 2365 | assert t.b == 0 |
| 2366 | |
| 2367 | |
| 2368 | class RollBack(HasTraits): |
nothing calls this directly
no test coverage detected
searching dependent graphs…