1 "config.set() or config.setoption() or option.setoption()"
3 from py.test import raises
5 from tiramisu.config import *
6 from tiramisu.option import *
7 from tiramisu.error import *
9 def make_description():
10 gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
11 gcdummy = BoolOption('dummy', 'dummy', default=False)
12 objspaceoption = ChoiceOption('objspace', 'Object space',
13 ['std', 'thunk'], 'std')
14 booloption = BoolOption('bool', 'Test boolean option', default=True)
15 intoption = IntOption('int', 'Test int option', default=0)
16 floatoption = FloatOption('float', 'Test float option', default=2.3)
17 stroption = StrOption('str', 'Test string option', default="abc")
18 boolop = BoolOption('boolop', 'Test boolean option op', default=True)
19 wantref_option = BoolOption('wantref', 'Test requires', default=False)
20 wantframework_option = BoolOption('wantframework', 'Test requires',
22 gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
23 descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
24 wantref_option, stroption,
28 #____________________________________________________________
29 # change with __setattr__
30 def test_attribute_access():
31 "Once set, option values can't be changed again by attribute access"
32 s = StrOption("string", "", default="string")
33 descr = OptionDescription("options", "", [s])
34 config = Config(descr)
35 # let's try to change it again
37 assert config.string == "foo"
40 s = StrOption("string", "", default=["string", "sdfsdf"], default_multi="prout", multi=True)
41 descr = OptionDescription("options", "", [s])
42 config = Config(descr)
43 print config.string[1]
44 config.string[1] = "titi"
45 print config.string[1]
48 "if value is None, resets to default owner"
49 s = StrOption("string", "", default="string")
50 descr = OptionDescription("options", "", [s])
51 config = Config(descr)
53 assert config.string == "foo"
54 assert config._cfgimpl_value_owners['string'] == 'user'
55 config.unwrap_from_path("string").reset(config)
56 assert config.string == 'string'
57 assert config._cfgimpl_value_owners['string'] == 'default'
59 def test_reset_with_multi():
60 s = StrOption("string", "", default=["string"], default_multi="string" , multi=True)
61 descr = OptionDescription("options", "", [s])
62 config = Config(descr)
64 config.unwrap_from_path("string").reset(config)
65 assert config.string == ["string"]
66 assert config._cfgimpl_value_owners['string'] == ['default']
67 config.string = ["eggs", "spam", "foo"]
68 assert config._cfgimpl_value_owners['string'] == ['user', 'user', 'user']
70 config.unwrap_from_path("string").reset(config)
71 # assert config.string == ["string"]
72 assert config._cfgimpl_value_owners['string'] == ['default']
73 raises(ConfigError, "config.string = None")
75 def test_default_with_multi():
76 "default with multi is a list"
77 s = StrOption("string", "", default=[], default_multi="string" , multi=True)
78 descr = OptionDescription("options", "", [s])
79 config = Config(descr)
80 assert config.string == []
81 s = StrOption("string", "", default=None, default_multi="string" , multi=True)
82 descr = OptionDescription("options", "", [s])
83 config = Config(descr)
84 assert config.string == []
86 def test_idontexist():
87 descr = make_description()
89 raises(AttributeError, "cfg.idontexist")
90 # ____________________________________________________________
91 def test_attribute_access_with_multi():
92 s = StrOption("string", "", default=["string"], default_multi= "string" , multi=True)
93 descr = OptionDescription("options", "", [s])
94 config = Config(descr)
95 config.string = ["foo", "bar"]
96 assert config.string == ["foo", "bar"]
98 def test_item_access_with_multi():
99 s = StrOption("string", "", default=["string"], multi=True)
100 descr = OptionDescription("options", "", [s])
101 config = Config(descr)
102 config.string = ["foo", "bar"]
103 assert config.string == ["foo", "bar"]
104 assert config.string[0] == "foo"
106 config.string[0] = 'changetest'
107 # assert config.string[0] == 'changetest'
108 # assert config.string[
110 def test_access_with_multi_default():
111 s = StrOption("string", "", default=["string"], multi=True)
112 descr = OptionDescription("options", "", [s])
113 config = Config(descr)
114 assert config._cfgimpl_value_owners["string"] == ['default']
115 config.string = ["foo", "bar"]
116 assert config.string == ["foo", "bar"]
117 assert config._cfgimpl_value_owners["string"] == ['user', 'user']
119 #def test_attribute_access_with_multi2():
120 # s = StrOption("string", "", default="string", multi=True)
121 # descr = OptionDescription("options", "", [s])
122 # config = Config(descr)
123 # config.string = ["foo", "bar"]
124 # assert config.string == ["foo", "bar"]
126 def test_multi_with_requires():
127 s = StrOption("string", "", default=["string"], default_multi="string", multi=True)
128 intoption = IntOption('int', 'Test int option', default=0)
129 stroption = StrOption('str', 'Test string option', default=["abc"], default_multi = "abc",
130 requires=[('int', 1, 'hide')], multi=True)
131 descr = OptionDescription("options", "", [s, intoption, stroption])
132 config = Config(descr)
133 assert stroption._is_hidden() == False
135 raises(PropertiesOptionError, "config.str = ['a', 'b']")
136 assert stroption._is_hidden()
138 def test__requires_with_inverted():
139 s = StrOption("string", "", default=["string"], multi=True)
140 intoption = IntOption('int', 'Test int option', default=0)
141 stroption = StrOption('str', 'Test string option', default=["abc"], default_multi = "abc",
142 requires=[('int', 1, 'hide', 'inverted')], multi=True)
143 descr = OptionDescription("options", "", [s, intoption, stroption])
144 config = Config(descr)
145 assert stroption._is_hidden() == False
147 assert stroption._is_hidden() == False
149 def test_multi_with_requires_in_another_group():
150 s = StrOption("string", "", default=["string"], multi=True)
151 intoption = IntOption('int', 'Test int option', default=0)
152 descr = OptionDescription("options", "", [intoption])
153 stroption = StrOption('str', 'Test string option', default=["abc"],
154 requires=[('int', 1, 'hide')], multi=True)
155 descr = OptionDescription("opt", "", [stroption])
156 descr2 = OptionDescription("opt2", "", [intoption, s, descr])
157 config = Config(descr2)
158 assert stroption._is_hidden() == False
160 raises(PropertiesOptionError, "config.opt.str = ['a', 'b']")
161 assert stroption._is_hidden()
163 def test_apply_requires_from_config():
164 s = StrOption("string", "", default=["string"], multi=True)
165 intoption = IntOption('int', 'Test int option', default=0)
166 descr = OptionDescription("options", "", [intoption])
167 stroption = StrOption('str', 'Test string option', default=["abc"],
168 requires=[('int', 1, 'hide')], multi=True)
169 descr = OptionDescription("opt", "", [stroption])
170 descr2 = OptionDescription("opt2", "", [intoption, s, descr])
171 config = Config(descr2)
172 assert stroption._is_hidden() == False
178 assert stroption._is_hidden()
181 def test_apply_requires_with_disabled():
182 s = StrOption("string", "", default=["string"], multi=True)
183 intoption = IntOption('int', 'Test int option', default=0)
184 descr = OptionDescription("options", "", [intoption])
185 stroption = StrOption('str', 'Test string option', default=["abc"],
186 requires=[('int', 1, 'disable')], multi=True)
187 descr = OptionDescription("opt", "", [stroption])
188 descr2 = OptionDescription("opt2", "", [intoption, s, descr])
189 config = Config(descr2)
190 assert stroption._is_disabled() == False
196 assert stroption._is_disabled()
198 def test_multi_with_requires_with_disabled_in_another_group():
199 s = StrOption("string", "", default=["string"], multi=True)
200 intoption = IntOption('int', 'Test int option', default=0)
201 descr = OptionDescription("options", "", [intoption])
202 stroption = StrOption('str', 'Test string option', default=["abc"],
203 requires=[('int', 1, 'disable')], multi=True)
204 descr = OptionDescription("opt", "", [stroption])
205 descr2 = OptionDescription("opt2", "", [intoption, s, descr])
206 config = Config(descr2)
207 assert stroption._is_disabled() == False
209 raises(PropertiesOptionError, "config.opt.str = ['a', 'b']")
210 assert stroption._is_disabled()
212 def test_multi_with_requires_that_is_multi():
213 s = StrOption("string", "", default=["string"], multi=True)
214 intoption = IntOption('int', 'Test int option', default=[0], multi=True)
215 stroption = StrOption('str', 'Test string option', default=["abc"],
216 requires=[('int', [1, 1], 'hide')], multi=True)
217 descr = OptionDescription("options", "", [s, intoption, stroption])
218 config = Config(descr)
219 assert stroption._is_hidden() == False
221 raises(PropertiesOptionError, "config.str = ['a', 'b']")
222 assert stroption._is_hidden()
224 def test_multi_with_bool():
225 s = BoolOption("bool", "", default=[False], multi=True)
226 descr = OptionDescription("options", "", [s])
227 config = Config(descr)
228 assert descr.bool.multi == True
229 config.bool = [True, False]
230 assert config._cfgimpl_values['bool'] == [True, False]
231 assert config.bool == [True, False]
233 def test_multi_with_bool_two():
234 s = BoolOption("bool", "", default=[False], multi=True)
235 descr = OptionDescription("options", "", [s])
236 config = Config(descr)
237 assert descr.bool.multi == True
238 raises(ConfigError, "config.bool = True")
240 def test_choice_access_with_multi():
241 ch = ChoiceOption("t1", "", ["a", "b"], default=["a"], multi=True)
242 descr = OptionDescription("options", "", [ch])
243 config = Config(descr)
244 config.t1 = ["a", "b", "a", "b"]
245 assert config.t1 == ["a", "b", "a", "b"]
246 # ____________________________________________________________
248 def test_setoption_from_option():
249 "a setoption directly from the option is **not** a good practice"
250 booloption = BoolOption('bool', 'Test boolean option', default=True)
251 descr = OptionDescription('descr', '', [booloption])
253 booloption.setoption(cfg, False, 'owner')
254 assert cfg.bool == False
255 #____________________________________________________________
257 descr = OptionDescription("opt", "", [
258 OptionDescription("sub", "", [
259 BoolOption("b1", ""),
260 ChoiceOption("c1", "", ['a', 'b', 'c'], 'a'),
261 BoolOption("d1", ""),
263 BoolOption("b2", ""),
264 BoolOption("d1", ""),
267 c.set(b1=False, c1='b')
269 assert c.sub.c1 == 'b'
270 # new config, because you cannot change values once they are set
272 c.set(b2=False, **{'sub.c1': 'c'})
274 assert c.sub.c1 == 'c'
275 raises(AmbigousOptionError, "c.set(d1=True)")
276 raises(NoMatchingOptionFound, "c.set(unknown='foo')")
279 descr = OptionDescription("opt", "", [
280 OptionDescription("s1", "", [
281 BoolOption("a", "", default=False)]),
282 IntOption("int", "", default=42)])
283 d = {'s1.a': True, 'int': 23}
284 config = Config(descr)
287 assert config.int == 23
289 def test_set_with_hidden_option():
290 boolopt = BoolOption("a", "", default=False)
292 descr = OptionDescription("opt", "", [
293 OptionDescription("s1", "", [boolopt]),
294 IntOption("int", "", default=42)])
295 d = {'s1.a': True, 'int': 23}
296 config = Config(descr)
297 raises(PropertiesOptionError, "config.set(**d)")
299 def test_set_with_unknown_option():
300 boolopt = BoolOption("b", "", default=False)
301 descr = OptionDescription("opt", "", [
302 OptionDescription("s1", "", [boolopt]),
303 IntOption("int", "", default=42)])
304 d = {'s1.a': True, 'int': 23}
305 config = Config(descr)
306 raises(NoMatchingOptionFound, "config.set(**d)")
309 def test_set_symlink_option():
310 boolopt = BoolOption("b", "", default=False)
311 linkopt = SymLinkOption("c", "s1.b")
312 descr = OptionDescription("opt", "",
313 [linkopt, OptionDescription("s1", "", [boolopt])])
314 config = Config(descr)
315 setattr(config, "s1.b", True)
316 setattr(config, "s1.b", False)
317 assert config.s1.b == False
318 assert config.c == False
320 assert config.s1.b == True
321 assert config.c == True
323 assert config.s1.b == False
324 assert config.c == False
326 #____________________________________________________________
327 def test_config_impl_values():
328 descr = make_description()
329 config = Config(descr)
331 # gcdummy.setoption(config, True, "user")
332 # config.setoption("gc.dummy", True, "user")
333 #config.gc.dummy = True
334 # config.setoption("bool", False, "user")
335 config.set(dummy=False)
336 assert config.gc._cfgimpl_values == {'dummy': False, 'float': 2.3, 'name': 'ref'}
337 ## acces to the option object
338 # config.gc._cfgimpl_descr.dummy.setoption(config, True, "user")
339 assert config.gc.dummy == False
340 # config.set(dummy=True)
341 # assert config.gc.dummy == True
343 #____________________________________________________________
344 def test_accepts_multiple_changes_from_option():
345 s = StrOption("string", "", default="string")
346 descr = OptionDescription("options", "", [s])
347 config = Config(descr)
348 config.string = "egg"
349 assert s.getdefault() == "string"
350 assert config.string == "egg"
351 s.setoption(config, 'blah', "default")
352 assert s.getdefault() == "string"
353 assert config.string == "blah"
354 s.setoption(config, 'bol', "user")
355 assert config.string == 'bol'
356 # config.override({'string': "blurp"})
357 # assert config.string == 'blurp'
358 # assert s.getdefault() == 'blurp'
360 def test_allow_multiple_changes_from_config():
362 a `setoption` from the config object is much like the attribute access,
363 except the fact that value owner can bet set
365 s = StrOption("string", "", default="string")
366 s2 = StrOption("string2", "", default="string")
367 suboption = OptionDescription("bip", "", [s2])
368 descr = OptionDescription("options", "", [s, suboption])
369 config = Config(descr)
370 config.setoption("string", 'blah', "user")
371 config.setoption("string", "oh", "user")
372 assert config.string == "oh"
373 config.set(string2= 'blah')
374 assert config.bip.string2 == 'blah'
375 # ____________________________________________________________
377 #def test_overrides_are_defaults():
378 # descr = OptionDescription("test", "", [
379 # BoolOption("b1", "", default=False),
380 # BoolOption("b2", "", default=False),
383 # config = Config(descr)
386 # # test with a require
390 # ____________________________________________________________
391 # accessing a value by the get method
392 def test_access_by_get():
393 descr = make_description()
395 raises(NotFoundError, "cfg.get('idontexist')" )
396 assert cfg.get('wantref') == False
397 assert cfg.gc.dummy == False
398 assert cfg.get('dummy') == False
400 def test_access_by_get_whith_hide():
401 b1 = BoolOption("b1", "")
403 descr = OptionDescription("opt", "", [
404 OptionDescription("sub", "", [
406 ChoiceOption("c1", "", ['a', 'b', 'c'], 'a'),
407 BoolOption("d1", ""),
409 BoolOption("b2", ""),
410 BoolOption("d1", ""),
413 raises(PropertiesOptionError, "c.get('b1')")