1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Tests for the static notifiers. """
import unittest
from traits import trait_notifiers
from traits.api import Float, HasTraits, List
calls_0 = []
class StaticNotifiers0(HasTraits):
ok = Float
def _ok_changed():
calls_0.append(True)
fail = Float
def _fail_changed():
raise Exception("error")
class StaticNotifiers1(HasTraits):
ok = Float
def _ok_changed(self):
if not hasattr(self, "calls"):
self.calls = []
self.calls.append(True)
fail = Float
def _fail_changed(self):
raise Exception("error")
class StaticNotifiers2(HasTraits):
ok = Float
def _ok_changed(self, new):
if not hasattr(self, "calls"):
self.calls = []
self.calls.append(new)
fail = Float
def _fail_changed(self, new):
raise Exception("error")
class StaticNotifiers3(HasTraits):
ok = Float
def _ok_changed(self, old, new):
if not hasattr(self, "calls"):
self.calls = []
self.calls.append((old, new))
fail = Float
def _fail_changed(self, old, new):
raise Exception("error")
class StaticNotifiers4(HasTraits):
ok = Float
def _ok_changed(self, name, old, new):
if not hasattr(self, "calls"):
self.calls = []
self.calls.append((name, old, new))
fail = Float
def _fail_changed(self, name, old, new):
raise Exception("error")
class _LeadingUnderscore(HasTraits):
_ok = Float()
calls = List()
def __ok_changed(self, name, old, new):
self.calls.append((name, old, new))
class TestNotifiers(unittest.TestCase):
""" Tests for the static notifiers, and the "anytrait" static notifiers.
"""
def setUp(self):
self.exceptions = []
trait_notifiers.push_exception_handler(self._handle_exception)
def tearDown(self):
trait_notifiers.pop_exception_handler()
def _handle_exception(self, obj, name, old, new):
self.exceptions.append((obj, name, old, new))
def test_static_notifiers_0(self):
calls_0.clear()
obj = StaticNotifiers0(ok=2)
obj.ok = 3
self.assertEqual(len(calls_0), 2)
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_static_notifiers_1(self):
obj = StaticNotifiers1(ok=2)
obj.ok = 3
self.assertEqual(len(obj.calls), 2)
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_static_notifiers_2(self):
obj = StaticNotifiers2(ok=2)
obj.ok = 3
self.assertEqual(obj.calls, [2, 3])
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_static_notifiers_3(self):
obj = StaticNotifiers3(ok=2)
obj.ok = 3
self.assertEqual(obj.calls, [(0, 2), (2, 3)])
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_static_notifiers_4(self):
obj = StaticNotifiers4(ok=2)
obj.ok = 3
self.assertEqual(obj.calls, [("ok", 0, 2), ("ok", 2, 3)])
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_leading_underscore(self):
""" Test the name-mangling for the double-underscored change handlers.
"""
obj = _LeadingUnderscore(_ok=2.0)
obj._ok = 3.0
self.assertEqual(obj.calls, [("_ok", 0.0, 2.0), ("_ok", 2.0, 3.0)])
|