##// END OF EJS Templates
Fix configurable test for Python 3.
Thomas Kluyver -
Show More
@@ -1,178 +1,183 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Tests for IPython.config.configurable
3 Tests for IPython.config.configurable
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez (design help)
8 * Fernando Perez (design help)
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2010 The IPython Development Team
12 # Copyright (C) 2008-2010 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 from unittest import TestCase
22 from unittest import TestCase
23
23
24 from IPython.config.configurable import (
24 from IPython.config.configurable import (
25 Configurable,
25 Configurable,
26 SingletonConfigurable
26 SingletonConfigurable
27 )
27 )
28
28
29 from IPython.utils.traitlets import (
29 from IPython.utils.traitlets import (
30 Integer, Float, Unicode
30 Integer, Float, Unicode
31 )
31 )
32
32
33 from IPython.config.loader import Config
33 from IPython.config.loader import Config
34
34 from IPython.utils.py3compat import PY3
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 # Test cases
37 # Test cases
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40
40
41 class MyConfigurable(Configurable):
41 class MyConfigurable(Configurable):
42 a = Integer(1, config=True, help="The integer a.")
42 a = Integer(1, config=True, help="The integer a.")
43 b = Float(1.0, config=True, help="The integer b.")
43 b = Float(1.0, config=True, help="The integer b.")
44 c = Unicode('no config')
44 c = Unicode('no config')
45
45
46
46
47 mc_help=u"""MyConfigurable options
47 mc_help=u"""MyConfigurable options
48 ----------------------
48 ----------------------
49 --MyConfigurable.a=<Integer>
49 --MyConfigurable.a=<Integer>
50 Default: 1
50 Default: 1
51 The integer a.
51 The integer a.
52 --MyConfigurable.b=<Float>
52 --MyConfigurable.b=<Float>
53 Default: 1.0
53 Default: 1.0
54 The integer b."""
54 The integer b."""
55
55
56 mc_help_inst=u"""MyConfigurable options
56 mc_help_inst=u"""MyConfigurable options
57 ----------------------
57 ----------------------
58 --MyConfigurable.a=<Integer>
58 --MyConfigurable.a=<Integer>
59 Current: 5
59 Current: 5
60 The integer a.
60 The integer a.
61 --MyConfigurable.b=<Float>
61 --MyConfigurable.b=<Float>
62 Current: 4.0
62 Current: 4.0
63 The integer b."""
63 The integer b."""
64
64
65 # On Python 3, the Integer trait is a synonym for Int
66 if PY3:
67 mc_help = mc_help.replace(u"<Integer>", u"<Int>")
68 mc_help_inst = mc_help_inst.replace(u"<Integer>", u"<Int>")
69
65 class Foo(Configurable):
70 class Foo(Configurable):
66 a = Integer(0, config=True, help="The integer a.")
71 a = Integer(0, config=True, help="The integer a.")
67 b = Unicode('nope', config=True)
72 b = Unicode('nope', config=True)
68
73
69
74
70 class Bar(Foo):
75 class Bar(Foo):
71 b = Unicode('gotit', config=False, help="The string b.")
76 b = Unicode('gotit', config=False, help="The string b.")
72 c = Float(config=True, help="The string c.")
77 c = Float(config=True, help="The string c.")
73
78
74
79
75 class TestConfigurable(TestCase):
80 class TestConfigurable(TestCase):
76
81
77 def test_default(self):
82 def test_default(self):
78 c1 = Configurable()
83 c1 = Configurable()
79 c2 = Configurable(config=c1.config)
84 c2 = Configurable(config=c1.config)
80 c3 = Configurable(config=c2.config)
85 c3 = Configurable(config=c2.config)
81 self.assertEquals(c1.config, c2.config)
86 self.assertEquals(c1.config, c2.config)
82 self.assertEquals(c2.config, c3.config)
87 self.assertEquals(c2.config, c3.config)
83
88
84 def test_custom(self):
89 def test_custom(self):
85 config = Config()
90 config = Config()
86 config.foo = 'foo'
91 config.foo = 'foo'
87 config.bar = 'bar'
92 config.bar = 'bar'
88 c1 = Configurable(config=config)
93 c1 = Configurable(config=config)
89 c2 = Configurable(config=c1.config)
94 c2 = Configurable(config=c1.config)
90 c3 = Configurable(config=c2.config)
95 c3 = Configurable(config=c2.config)
91 self.assertEquals(c1.config, config)
96 self.assertEquals(c1.config, config)
92 self.assertEquals(c2.config, config)
97 self.assertEquals(c2.config, config)
93 self.assertEquals(c3.config, config)
98 self.assertEquals(c3.config, config)
94 # Test that copies are not made
99 # Test that copies are not made
95 self.assert_(c1.config is config)
100 self.assert_(c1.config is config)
96 self.assert_(c2.config is config)
101 self.assert_(c2.config is config)
97 self.assert_(c3.config is config)
102 self.assert_(c3.config is config)
98 self.assert_(c1.config is c2.config)
103 self.assert_(c1.config is c2.config)
99 self.assert_(c2.config is c3.config)
104 self.assert_(c2.config is c3.config)
100
105
101 def test_inheritance(self):
106 def test_inheritance(self):
102 config = Config()
107 config = Config()
103 config.MyConfigurable.a = 2
108 config.MyConfigurable.a = 2
104 config.MyConfigurable.b = 2.0
109 config.MyConfigurable.b = 2.0
105 c1 = MyConfigurable(config=config)
110 c1 = MyConfigurable(config=config)
106 c2 = MyConfigurable(config=c1.config)
111 c2 = MyConfigurable(config=c1.config)
107 self.assertEquals(c1.a, config.MyConfigurable.a)
112 self.assertEquals(c1.a, config.MyConfigurable.a)
108 self.assertEquals(c1.b, config.MyConfigurable.b)
113 self.assertEquals(c1.b, config.MyConfigurable.b)
109 self.assertEquals(c2.a, config.MyConfigurable.a)
114 self.assertEquals(c2.a, config.MyConfigurable.a)
110 self.assertEquals(c2.b, config.MyConfigurable.b)
115 self.assertEquals(c2.b, config.MyConfigurable.b)
111
116
112 def test_parent(self):
117 def test_parent(self):
113 config = Config()
118 config = Config()
114 config.Foo.a = 10
119 config.Foo.a = 10
115 config.Foo.b = "wow"
120 config.Foo.b = "wow"
116 config.Bar.b = 'later'
121 config.Bar.b = 'later'
117 config.Bar.c = 100.0
122 config.Bar.c = 100.0
118 f = Foo(config=config)
123 f = Foo(config=config)
119 b = Bar(config=f.config)
124 b = Bar(config=f.config)
120 self.assertEquals(f.a, 10)
125 self.assertEquals(f.a, 10)
121 self.assertEquals(f.b, 'wow')
126 self.assertEquals(f.b, 'wow')
122 self.assertEquals(b.b, 'gotit')
127 self.assertEquals(b.b, 'gotit')
123 self.assertEquals(b.c, 100.0)
128 self.assertEquals(b.c, 100.0)
124
129
125 def test_override1(self):
130 def test_override1(self):
126 config = Config()
131 config = Config()
127 config.MyConfigurable.a = 2
132 config.MyConfigurable.a = 2
128 config.MyConfigurable.b = 2.0
133 config.MyConfigurable.b = 2.0
129 c = MyConfigurable(a=3, config=config)
134 c = MyConfigurable(a=3, config=config)
130 self.assertEquals(c.a, 3)
135 self.assertEquals(c.a, 3)
131 self.assertEquals(c.b, config.MyConfigurable.b)
136 self.assertEquals(c.b, config.MyConfigurable.b)
132 self.assertEquals(c.c, 'no config')
137 self.assertEquals(c.c, 'no config')
133
138
134 def test_override2(self):
139 def test_override2(self):
135 config = Config()
140 config = Config()
136 config.Foo.a = 1
141 config.Foo.a = 1
137 config.Bar.b = 'or' # Up above b is config=False, so this won't do it.
142 config.Bar.b = 'or' # Up above b is config=False, so this won't do it.
138 config.Bar.c = 10.0
143 config.Bar.c = 10.0
139 c = Bar(config=config)
144 c = Bar(config=config)
140 self.assertEquals(c.a, config.Foo.a)
145 self.assertEquals(c.a, config.Foo.a)
141 self.assertEquals(c.b, 'gotit')
146 self.assertEquals(c.b, 'gotit')
142 self.assertEquals(c.c, config.Bar.c)
147 self.assertEquals(c.c, config.Bar.c)
143 c = Bar(a=2, b='and', c=20.0, config=config)
148 c = Bar(a=2, b='and', c=20.0, config=config)
144 self.assertEquals(c.a, 2)
149 self.assertEquals(c.a, 2)
145 self.assertEquals(c.b, 'and')
150 self.assertEquals(c.b, 'and')
146 self.assertEquals(c.c, 20.0)
151 self.assertEquals(c.c, 20.0)
147
152
148 def test_help(self):
153 def test_help(self):
149 self.assertEquals(MyConfigurable.class_get_help(), mc_help)
154 self.assertEquals(MyConfigurable.class_get_help(), mc_help)
150
155
151 def test_help_inst(self):
156 def test_help_inst(self):
152 inst = MyConfigurable(a=5, b=4)
157 inst = MyConfigurable(a=5, b=4)
153 self.assertEquals(MyConfigurable.class_get_help(inst), mc_help_inst)
158 self.assertEquals(MyConfigurable.class_get_help(inst), mc_help_inst)
154
159
155
160
156 class TestSingletonConfigurable(TestCase):
161 class TestSingletonConfigurable(TestCase):
157
162
158 def test_instance(self):
163 def test_instance(self):
159 from IPython.config.configurable import SingletonConfigurable
164 from IPython.config.configurable import SingletonConfigurable
160 class Foo(SingletonConfigurable): pass
165 class Foo(SingletonConfigurable): pass
161 self.assertEquals(Foo.initialized(), False)
166 self.assertEquals(Foo.initialized(), False)
162 foo = Foo.instance()
167 foo = Foo.instance()
163 self.assertEquals(Foo.initialized(), True)
168 self.assertEquals(Foo.initialized(), True)
164 self.assertEquals(foo, Foo.instance())
169 self.assertEquals(foo, Foo.instance())
165 self.assertEquals(SingletonConfigurable._instance, None)
170 self.assertEquals(SingletonConfigurable._instance, None)
166
171
167 def test_inheritance(self):
172 def test_inheritance(self):
168 class Bar(SingletonConfigurable): pass
173 class Bar(SingletonConfigurable): pass
169 class Bam(Bar): pass
174 class Bam(Bar): pass
170 self.assertEquals(Bar.initialized(), False)
175 self.assertEquals(Bar.initialized(), False)
171 self.assertEquals(Bam.initialized(), False)
176 self.assertEquals(Bam.initialized(), False)
172 bam = Bam.instance()
177 bam = Bam.instance()
173 bam == Bar.instance()
178 bam == Bar.instance()
174 self.assertEquals(Bar.initialized(), True)
179 self.assertEquals(Bar.initialized(), True)
175 self.assertEquals(Bam.initialized(), True)
180 self.assertEquals(Bam.initialized(), True)
176 self.assertEquals(bam, Bam._instance)
181 self.assertEquals(bam, Bam._instance)
177 self.assertEquals(bam, Bar._instance)
182 self.assertEquals(bam, Bar._instance)
178 self.assertEquals(SingletonConfigurable._instance, None)
183 self.assertEquals(SingletonConfigurable._instance, None)
General Comments 0
You need to be logged in to leave comments. Login now