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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
# (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 the "anytrait" static notifiers. """
import unittest
from traits import trait_notifiers
from traits.api import Float, HasTraits, Undefined
class AnytraitStaticNotifiers0Fail(HasTraits):
fail = Float
def _anytrait_changed():
raise Exception("error")
class AnytraitStaticNotifiers1Fail(HasTraits):
fail = Float
def _anytrait_changed(self):
raise Exception("error")
class AnytraitStaticNotifiers2Fail(HasTraits):
fail = Float
def _anytrait_changed(self, name):
raise Exception("error")
class AnytraitStaticNotifiers3Fail(HasTraits):
fail = Float
def _anytrait_changed(self, name, new):
raise Exception("error")
class AnytraitStaticNotifiers4Fail(HasTraits):
fail = Float
def _anytrait_changed(self, name, old, new):
raise Exception("error")
class TestNotifiers(unittest.TestCase):
""" Tests for the static notifiers, and the "anytrait" static notifiers.
"""
#### 'TestCase' protocol ##################################################
def setUp(self):
self.exceptions = []
trait_notifiers.push_exception_handler(self._handle_exception)
def tearDown(self):
trait_notifiers.pop_exception_handler()
#### Private protocol #####################################################
def _handle_exception(self, obj, name, old, new):
self.exceptions.append((obj, name, old, new))
#### Tests ################################################################
def test_anytrait_static_notifiers_0(self):
anycalls_0 = []
class AnytraitStaticNotifiers0(HasTraits):
ok = Float
def _anytrait_changed():
anycalls_0.append(True)
obj = AnytraitStaticNotifiers0(ok=2)
obj.ok = 3
self.assertEqual(len(anycalls_0), 2)
def test_anytrait_static_notifiers_1(self):
class AnytraitStaticNotifiers1(HasTraits):
ok = Float
def _anytrait_changed(self):
if not hasattr(self, "anycalls"):
self.anycalls = []
self.anycalls.append(True)
obj = AnytraitStaticNotifiers1(ok=2)
obj.ok = 3
# 3 calls (see test_anytrait_static_notifiers_4):
# 1 to add trait 'anycalls',
# 1 from the constructor,
# 1 to set ok to 3
self.assertEqual(len(obj.anycalls), 3)
def test_anytrait_static_notifiers_2(self):
class AnytraitStaticNotifiers2(HasTraits):
ok = Float
def _anytrait_changed(self, name):
if not hasattr(self, "anycalls"):
self.anycalls = []
self.anycalls.append(name)
obj = AnytraitStaticNotifiers2(ok=2)
obj.ok = 3
expected = ["trait_added", "ok", "ok"]
self.assertEqual(expected, obj.anycalls)
def test_anytrait_static_notifiers_3(self):
class AnytraitStaticNotifiers3(HasTraits):
ok = Float
def _anytrait_changed(self, name, new):
if not hasattr(self, "anycalls"):
self.anycalls = []
self.anycalls.append((name, new))
obj = AnytraitStaticNotifiers3(ok=2)
obj.ok = 3
expected = [("trait_added", "anycalls"), ("ok", 2), ("ok", 3)]
self.assertEqual(expected, obj.anycalls)
def test_anytrait_static_notifiers_4(self):
class AnytraitStaticNotifiers4(HasTraits):
ok = Float
def _anytrait_changed(self, name, old, new):
if not hasattr(self, "anycalls"):
self.anycalls = []
self.anycalls.append((name, old, new))
obj = AnytraitStaticNotifiers4(ok=2)
obj.ok = 3
expected = [
("trait_added", Undefined, "anycalls"),
("ok", 0, 2),
("ok", 2, 3),
]
self.assertEqual(expected, obj.anycalls)
def test_anytrait_static_notifiers_0_fail(self):
obj = AnytraitStaticNotifiers0Fail()
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_anytrait_static_notifiers_1_fail(self):
obj = AnytraitStaticNotifiers1Fail()
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_anytrait_static_notifiers_2_fail(self):
obj = AnytraitStaticNotifiers2Fail()
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_anytrait_static_notifiers_3_fail(self):
obj = AnytraitStaticNotifiers3Fail()
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
def test_anytrait_static_notifiers_4_fail(self):
obj = AnytraitStaticNotifiers4Fail()
obj.fail = 1
self.assertEqual(self.exceptions, [(obj, "fail", 0, 1)])
|