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