Show More
@@ -16,7 +16,7 b' import nose.tools as nt' | |||
|
16 | 16 | from nose import SkipTest |
|
17 | 17 | |
|
18 | 18 | from IPython.utils.traitlets import ( |
|
19 | HasTraits, MetaHasTraits, TraitType, Any, Bool, CBytes, Dict, | |
|
19 | HasTraits, MetaHasTraits, TraitType, Any, Bool, CBytes, Dict, Enum, | |
|
20 | 20 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, |
|
21 | 21 | Union, Undefined, Type, This, Instance, TCPAddress, List, Tuple, |
|
22 | 22 | ObjectName, DottedObjectName, CRegExp, link, directional_link, |
@@ -979,19 +979,6 b' class TestInstanceList(TraitTestBase):' | |||
|
979 | 979 | _good_values = [[Foo(), Foo(), None], None] |
|
980 | 980 | _bad_values = [['1', 2,], '1', [Foo]] |
|
981 | 981 | |
|
982 | class UnionListTrait(HasTraits): | |
|
983 | ||
|
984 | value = List(Int() | Bool()) | |
|
985 | ||
|
986 | class TestUnionListTrait(HasTraits): | |
|
987 | ||
|
988 | obj = UnionListTrait() | |
|
989 | ||
|
990 | _default_value = [] | |
|
991 | _good_values = [[True, 1], [False, True]] | |
|
992 | _bad_values = [[1, 'True'], False] | |
|
993 | ||
|
994 | ||
|
995 | 982 | class LenListTrait(HasTraits): |
|
996 | 983 | |
|
997 | 984 | value = List(Int, [0], minlen=1, maxlen=2) |
@@ -1099,7 +1086,36 b' def test_dict_default_value():' | |||
|
1099 | 1086 | d1, d2 = Dict(), Dict() |
|
1100 | 1087 | nt.assert_false(d1.get_default_value() is d2.get_default_value()) |
|
1101 | 1088 | |
|
1089 | ||
|
1090 | class TestValidationHook(TestCase): | |
|
1091 | ||
|
1092 | def test_parity_trait(self): | |
|
1093 | """Verify that the early validation hook is effective""" | |
|
1094 | ||
|
1095 | class Parity(HasTraits): | |
|
1096 | ||
|
1097 | value = Int(0) | |
|
1098 | parity = Enum(['odd', 'even'], default_value='even', allow_none=False) | |
|
1099 | ||
|
1100 | def _value_validate(self, value, trait): | |
|
1101 | if self.parity == 'even' and value % 2: | |
|
1102 | raise TraitError('Expected an even number') | |
|
1103 | if self.parity == 'odd' and (value % 2 == 0): | |
|
1104 | raise TraitError('Expected an odd number') | |
|
1105 | return value | |
|
1106 | ||
|
1107 | u = Parity() | |
|
1108 | u.parity = 'odd' | |
|
1109 | u.value = 1 # OK | |
|
1110 | with self.assertRaises(TraitError): | |
|
1111 | u.value = 2 # Trait Error | |
|
1112 | ||
|
1113 | u.parity = 'even' | |
|
1114 | u.value = 2 # OK | |
|
1115 | ||
|
1116 | ||
|
1102 | 1117 | class TestLink(TestCase): |
|
1118 | ||
|
1103 | 1119 | def test_connect_same(self): |
|
1104 | 1120 | """Verify two traitlets of the same type can be linked together using link.""" |
|
1105 | 1121 | |
@@ -1198,25 +1214,6 b' class TestLink(TestCase):' | |||
|
1198 | 1214 | a.value = 4 |
|
1199 | 1215 | self.assertEqual(''.join(callback_count), 'ab') |
|
1200 | 1216 | del callback_count[:] |
|
1201 | ||
|
1202 | def test_validate_args(self): | |
|
1203 | class A(HasTraits): | |
|
1204 | value = Int() | |
|
1205 | class B(HasTraits): | |
|
1206 | count = Int() | |
|
1207 | a = A(value=9) | |
|
1208 | b = B(count=8) | |
|
1209 | b.value = 5 | |
|
1210 | ||
|
1211 | with self.assertRaises(TypeError): | |
|
1212 | link((a, 'value')) | |
|
1213 | with self.assertRaises(TypeError): | |
|
1214 | link((a, 'value', 'count'), (b, 'count')) | |
|
1215 | with self.assertRaises(TypeError): | |
|
1216 | link((a, 'value'), (b, 'value')) | |
|
1217 | with self.assertRaises(TypeError): | |
|
1218 | link((a, 'traits'), (b, 'count')) | |
|
1219 | ||
|
1220 | 1217 | |
|
1221 | 1218 | class TestDirectionalLink(TestCase): |
|
1222 | 1219 | def test_connect_same(self): |
@@ -1283,25 +1280,6 b' class TestDirectionalLink(TestCase):' | |||
|
1283 | 1280 | a.value = 5 |
|
1284 | 1281 | self.assertNotEqual(a.value, b.value) |
|
1285 | 1282 | |
|
1286 | def test_validate_args(self): | |
|
1287 | class A(HasTraits): | |
|
1288 | value = Int() | |
|
1289 | class B(HasTraits): | |
|
1290 | count = Int() | |
|
1291 | a = A(value=9) | |
|
1292 | b = B(count=8) | |
|
1293 | b.value = 5 | |
|
1294 | ||
|
1295 | with self.assertRaises(TypeError): | |
|
1296 | directional_link((a, 'value')) | |
|
1297 | with self.assertRaises(TypeError): | |
|
1298 | directional_link((a, 'value', 'count'), (b, 'count')) | |
|
1299 | with self.assertRaises(TypeError): | |
|
1300 | directional_link((a, 'value'), (b, 'value')) | |
|
1301 | with self.assertRaises(TypeError): | |
|
1302 | directional_link((a, 'traits'), (b, 'count')) | |
|
1303 | ||
|
1304 | ||
|
1305 | 1283 | class Pickleable(HasTraits): |
|
1306 | 1284 | i = Int() |
|
1307 | 1285 | j = Int() |
General Comments 0
You need to be logged in to leave comments.
Login now