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 | |
@@ -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 |
General Comments 0
You need to be logged in to leave comments.
Login now