/usr/lib/python2.7/dist-packages/traits/tests/test_delegate.py is in python-traits 4.5.0-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | # Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
from __future__ import absolute_import
from traits.testing.unittest_tools import unittest
from ..api import Delegate, HasTraits, Instance, Str
# global because event handlers are being called with wrong value for self
baz_s_handler_self = None
baz_sd_handler_self = None
baz_u_handler_self = None
baz_t_handler_self = None
foo_s_handler_self = None
foo_t_handler_self = None
class Foo(HasTraits):
s = Str('foo')
t = Str('foo.t')
u = Str('foo.u')
def _s_changed(self, name, old, new):
print 'Foo._s_changed(%s, %s, %s, %s)' % (self, name, old, new)
global foo_s_handler_self
foo_s_handler_self = self
return
def _t_changed(self, name, old, new):
print 'Foo._t_changed(%s, %s, %s, %s)' % (self, name, old, new)
global foo_t_handler_self
foo_t_handler_self = self
return
class Bar(HasTraits):
foo = Instance(Foo, ())
s = Delegate('foo')
class BazModify(HasTraits):
foo = Instance(Foo, ())
sd = Delegate('foo', prefix='s', modify=True)
t = Delegate('foo', modify=True)
u = Delegate('foo', listenable=False, modify=True)
def _s_changed(self, name, old, new):
# should never be called
print 'BazModify._s_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_s_handler_self
baz_s_handler_self = self
return
def _sd_changed(self, name, old, new):
print 'BazModify._sd_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_sd_handler_self
baz_sd_handler_self = self
return
def _t_changed(self, name, old, new):
print 'BazModify._t_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_t_handler_self
baz_t_handler_self = self
return
def _u_changed(self, name, old, new):
print 'BazModify._u_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_u_handler_self
baz_u_handler_self = self
return
class BazNoModify(HasTraits):
foo = Instance(Foo, ())
sd = Delegate('foo', prefix='s')
t = Delegate('foo')
u = Delegate('foo', listenable=False)
def _s_changed(self, name, old, new):
print 'BazNoModify._s_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_s_handler_self
baz_s_handler_self = self
return
def _sd_changed(self, name, old, new):
print 'BazNoModify._sd_changed(%s, %s, %s, %s)' % (self, name, old,
new)
global baz_sd_handler_self
baz_sd_handler_self = self
return
def _t_changed(self, name, old, new):
print 'BazNoModify._t_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_t_handler_self
baz_t_handler_self = self
return
def _u_changed(self, name, old, new):
print 'BazNoModify._u_changed(%s, %s, %s, %s)' % (self, name, old, new)
global baz_u_handler_self
baz_u_handler_self = self
return
class DelegateTestCase(unittest.TestCase):
""" Test cases for delegated traits. """
def setUp(self):
""" Reset all of the globals.
"""
global baz_s_handler_self, baz_sd_handler_self, baz_u_handler_self
global baz_t_handler_self, foo_s_handler_self, foo_t_handler_self
baz_s_handler_self = None
baz_sd_handler_self = None
baz_u_handler_self = None
baz_t_handler_self = None
foo_s_handler_self = None
foo_t_handler_self = None
def test_reset(self):
""" Test that a delegated trait may be reset.
Deleting the attribute should reset the trait back to its initial
delegation behavior.
"""
f = Foo()
b = Bar(foo=f)
# Check initial delegation.
self.assertEqual(f.s, b.s)
# Check that an override works.
b.s = 'bar'
self.assertNotEqual(f.s, b.s)
# Check that we can reset back to delegation. This is what we are
# really testing for.
del b.s
self.assertEqual(f.s, b.s)
return
# Below are 8 tests to check the calling of change notification handlers.
# There are 8 cases for the 2x2x2 matrix with axes:
# Delegate with prefix or not
# Delegate with modify write through or not
# Handler in the delegator and delegatee
def test_modify_prefix_handler_on_delegator(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.s, b.sd)
b.sd = 'changed'
self.assertEqual(f.s, b.sd)
# Don't expect _s_changed to be called because from Baz's perspective
# the trait is named 'sd'
self.assertEqual(baz_s_handler_self, None)
# Do expect '_sd_changed' to be called with b as self
self.assertEqual(baz_sd_handler_self, b)
return
def test_modify_prefix_handler_on_delegatee(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.s, b.sd)
b.sd = 'changed'
self.assertEqual(f.s, b.sd)
# Foo expects its '_s_changed' handler to be called with f as self
self.assertEqual(foo_s_handler_self, f)
return
def test_no_modify_prefix_handler_on_delegator(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.s, b.sd)
b.sd = 'changed'
self.assertNotEqual(f.s, b.sd)
# Don't expect _s_changed to be called because from Baz's perspective
# the trait is named 'sd'
self.assertEqual(baz_s_handler_self, None)
# Do expect '_sd_changed' to be called with b as self
self.assertEqual(baz_sd_handler_self, b)
return
def test_no_modify_prefix_handler_on_delegatee_not_called(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.s, b.sd)
b.sd = 'changed'
self.assertNotEqual(f.s, b.sd)
# Foo expects its '_s_changed' handler to be called with f as self
self.assertEqual(foo_s_handler_self, None)
return
def test_modify_handler_on_delegator(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.t, b.t)
b.t = 'changed'
self.assertEqual(f.t, b.t)
# Do expect '_t_changed' to be called with b as self
self.assertEqual(baz_t_handler_self, b)
return
def test_modify_handler_on_delegatee(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.t, b.t)
b.t = 'changed'
self.assertEqual(f.t, b.t)
# Foo t did change so '_t_changed' handler should be called
self.assertEqual(foo_t_handler_self, f)
return
def test_no_modify_handler_on_delegator(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.t, b.t)
b.t = 'changed'
self.assertNotEqual(f.t, b.t)
# Do expect '_t_changed' to be called with b as self
self.assertEqual(baz_t_handler_self, b)
return
def test_no_modify_handler_on_delegatee_not_called(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.t, b.t)
b.t = 'changed'
self.assertNotEqual(f.t, b.t)
# Foo t did not change so '_t_changed' handler should not be called
self.assertEqual(foo_t_handler_self, None)
return
# Below are 4 tests for notification when the delegated trait is changed
# directly rather than through the delegator.
def test_no_modify_handler_on_delegatee_direct_change(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.t, b.t)
f.t = 'changed'
self.assertEqual(f.t, b.t)
# Foo t did change so '_t_changed' handler should be called
self.assertEqual(foo_t_handler_self, f)
return
def test_no_modify_handler_on_delegator_direct_change(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.t, b.t)
f.t = 'changed'
self.assertEqual(f.t, b.t)
# Do expect '_t_changed' to be called with b as self
self.assertEqual(baz_t_handler_self, b)
return
def test_modify_handler_on_delegatee_direct_change(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.t, b.t)
f.t = 'changed'
self.assertEqual(f.t, b.t)
# Foo t did change so '_t_changed' handler should be called
self.assertEqual(foo_t_handler_self, f)
return
def test_modify_handler_on_delegator_direct_change(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.t, b.t)
f.t = 'changed'
self.assertEqual(f.t, b.t)
# Do expect '_t_changed' to be called with b as self
self.assertEqual(baz_t_handler_self, b)
return
# Below are tests which check that we can turn off listenableness.
def test_modify_handler_not_listenable(self):
f = Foo()
b = BazModify(foo=f)
self.assertEqual(f.u, b.u)
f.u = 'changed'
self.assertEqual(f.u, b.u)
# Do not expect '_u_changed' to be called.
self.assertEqual(baz_u_handler_self, None)
return
def test_no_modify_handler_not_listenable(self):
f = Foo()
b = BazNoModify(foo=f)
self.assertEqual(f.u, b.u)
f.u = 'changed'
self.assertEqual(f.u, b.u)
# Do not expect '_u_changed' to be called.
self.assertEqual(baz_u_handler_self, None)
return
if __name__ == '__main__':
unittest.main()
# EOF #
|