Show More
@@ -16,7 +16,7 b' import nose.tools as nt' | |||||
16 | from nose import SkipTest |
|
16 | from nose import SkipTest | |
17 |
|
17 | |||
18 | from IPython.utils.traitlets import ( |
|
18 | from IPython.utils.traitlets import ( | |
19 | HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict, |
|
19 | HasTraits, MetaHasTraits, TraitType, AllowNone, Any, CBytes, Dict, | |
20 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, |
|
20 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, | |
21 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, |
|
21 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, | |
22 | ObjectName, DottedObjectName, CRegExp, link |
|
22 | ObjectName, DottedObjectName, CRegExp, link | |
@@ -73,7 +73,7 b' class TestTraitType(TestCase):' | |||||
73 | self.assertEqual(a.tt, -1) |
|
73 | self.assertEqual(a.tt, -1) | |
74 |
|
74 | |||
75 | def test_default_validate(self): |
|
75 | def test_default_validate(self): | |
76 |
class MyIntTT( |
|
76 | class MyIntTT(AllowNone): | |
77 | def validate(self, obj, value): |
|
77 | def validate(self, obj, value): | |
78 | if isinstance(value, int): |
|
78 | if isinstance(value, int): | |
79 | return value |
|
79 | return value | |
@@ -354,29 +354,29 b' class TestHasTraitsNotify(TestCase):' | |||||
354 |
|
354 | |||
355 | class A(HasTraits): |
|
355 | class A(HasTraits): | |
356 | listen_to = ['a'] |
|
356 | listen_to = ['a'] | |
357 |
|
357 | |||
358 | a = Int(0) |
|
358 | a = Int(0) | |
359 | b = 0 |
|
359 | b = 0 | |
360 |
|
360 | |||
361 | def __init__(self, **kwargs): |
|
361 | def __init__(self, **kwargs): | |
362 | super(A, self).__init__(**kwargs) |
|
362 | super(A, self).__init__(**kwargs) | |
363 | self.on_trait_change(self.listener1, ['a']) |
|
363 | self.on_trait_change(self.listener1, ['a']) | |
364 |
|
364 | |||
365 | def listener1(self, name, old, new): |
|
365 | def listener1(self, name, old, new): | |
366 | self.b += 1 |
|
366 | self.b += 1 | |
367 |
|
367 | |||
368 | class B(A): |
|
368 | class B(A): | |
369 |
|
369 | |||
370 | c = 0 |
|
370 | c = 0 | |
371 | d = 0 |
|
371 | d = 0 | |
372 |
|
372 | |||
373 | def __init__(self, **kwargs): |
|
373 | def __init__(self, **kwargs): | |
374 | super(B, self).__init__(**kwargs) |
|
374 | super(B, self).__init__(**kwargs) | |
375 | self.on_trait_change(self.listener2) |
|
375 | self.on_trait_change(self.listener2) | |
376 |
|
376 | |||
377 | def listener2(self, name, old, new): |
|
377 | def listener2(self, name, old, new): | |
378 | self.c += 1 |
|
378 | self.c += 1 | |
379 |
|
379 | |||
380 | def _a_changed(self, name, old, new): |
|
380 | def _a_changed(self, name, old, new): | |
381 | self.d += 1 |
|
381 | self.d += 1 | |
382 |
|
382 | |||
@@ -442,7 +442,7 b' class TestHasTraits(TestCase):' | |||||
442 | def __init__(self, i): |
|
442 | def __init__(self, i): | |
443 | super(A, self).__init__() |
|
443 | super(A, self).__init__() | |
444 | self.i = i |
|
444 | self.i = i | |
445 |
|
445 | |||
446 | a = A(5) |
|
446 | a = A(5) | |
447 | self.assertEqual(a.i, 5) |
|
447 | self.assertEqual(a.i, 5) | |
448 | # should raise TypeError if no positional arg given |
|
448 | # should raise TypeError if no positional arg given | |
@@ -673,6 +673,23 b' class TraitTestBase(TestCase):' | |||||
673 | if hasattr(self, '_default_value'): |
|
673 | if hasattr(self, '_default_value'): | |
674 | self.assertEqual(self._default_value, self.obj.value) |
|
674 | self.assertEqual(self._default_value, self.obj.value) | |
675 |
|
675 | |||
|
676 | def test_allow_none(self): | |||
|
677 | if (hasattr(self, '_bad_values') and hasattr(self, '_good_values') and | |||
|
678 | None in self._bad_values): | |||
|
679 | trait=self.obj.traits()['value'] | |||
|
680 | if isinstance(trait, AllowNone) and not trait._allow_none: | |||
|
681 | trait._allow_none = True | |||
|
682 | self._bad_values.remove(None) | |||
|
683 | #skip coerce. Allow None casts None to None. | |||
|
684 | self.assign(None) | |||
|
685 | self.assertEqual(self.obj.value,None) | |||
|
686 | self.test_good_values() | |||
|
687 | self.test_bad_values() | |||
|
688 | #tear down | |||
|
689 | trait._allow_none = False | |||
|
690 | self._bad_values.append(None) | |||
|
691 | ||||
|
692 | ||||
676 | def tearDown(self): |
|
693 | def tearDown(self): | |
677 | # restore default value after tests, if set |
|
694 | # restore default value after tests, if set | |
678 | if hasattr(self, '_default_value'): |
|
695 | if hasattr(self, '_default_value'): | |
@@ -830,7 +847,7 b' class TestObjectName(TraitTestBase):' | |||||
830 | _default_value = "abc" |
|
847 | _default_value = "abc" | |
831 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] |
|
848 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] | |
832 | _bad_values = [1, "", u"€", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", |
|
849 | _bad_values = [1, "", u"€", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", | |
833 |
|
|
850 | None, object(), object] | |
834 | if sys.version_info[0] < 3: |
|
851 | if sys.version_info[0] < 3: | |
835 | _bad_values.append(u"þ") |
|
852 | _bad_values.append(u"þ") | |
836 | else: |
|
853 | else: | |
@@ -845,7 +862,7 b' class TestDottedObjectName(TraitTestBase):' | |||||
845 |
|
862 | |||
846 | _default_value = "a.b" |
|
863 | _default_value = "a.b" | |
847 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] |
|
864 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] | |
848 | _bad_values = [1, u"abc.€", "_.@", ".", ".abc", "abc.", ".abc."] |
|
865 | _bad_values = [1, u"abc.€", "_.@", ".", ".abc", "abc.", ".abc.", None] | |
849 | if sys.version_info[0] < 3: |
|
866 | if sys.version_info[0] < 3: | |
850 | _bad_values.append(u"t.þ") |
|
867 | _bad_values.append(u"t.þ") | |
851 | else: |
|
868 | else: | |
@@ -862,7 +879,7 b' class TestTCPAddress(TraitTestBase):' | |||||
862 |
|
879 | |||
863 | _default_value = ('127.0.0.1',0) |
|
880 | _default_value = ('127.0.0.1',0) | |
864 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] |
|
881 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] | |
865 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1)] |
|
882 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1), None] | |
866 |
|
883 | |||
867 | class ListTrait(HasTraits): |
|
884 | class ListTrait(HasTraits): | |
868 |
|
885 | |||
@@ -875,7 +892,7 b' class TestList(TraitTestBase):' | |||||
875 | _default_value = [] |
|
892 | _default_value = [] | |
876 | _good_values = [[], [1], list(range(10)), (1,2)] |
|
893 | _good_values = [[], [1], list(range(10)), (1,2)] | |
877 | _bad_values = [10, [1,'a'], 'a'] |
|
894 | _bad_values = [10, [1,'a'], 'a'] | |
878 |
|
895 | |||
879 | def coerce(self, value): |
|
896 | def coerce(self, value): | |
880 | if value is not None: |
|
897 | if value is not None: | |
881 | value = list(value) |
|
898 | value = list(value) | |
@@ -1054,7 +1071,7 b' class TestLink(TestCase):' | |||||
1054 | count = Int() |
|
1071 | count = Int() | |
1055 | a = A(value=9) |
|
1072 | a = A(value=9) | |
1056 | b = B(count=8) |
|
1073 | b = B(count=8) | |
1057 |
|
1074 | |||
1058 | # Register callbacks that count. |
|
1075 | # Register callbacks that count. | |
1059 | callback_count = [] |
|
1076 | callback_count = [] | |
1060 | def a_callback(name, old, new): |
|
1077 | def a_callback(name, old, new): |
General Comments 0
You need to be logged in to leave comments.
Login now