187 lines
7.5 KiB
Python
187 lines
7.5 KiB
Python
"configuration objects global API"
|
|
import autopath
|
|
from py.test import raises
|
|
|
|
from tiramisu.config import *
|
|
from tiramisu.option import *
|
|
|
|
def make_description():
|
|
gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
|
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
objspaceoption = ChoiceOption('objspace', 'Object space',
|
|
['std', 'thunk'], 'std')
|
|
booloption = BoolOption('bool', 'Test boolean option', default=True)
|
|
intoption = IntOption('int', 'Test int option', default=0)
|
|
floatoption = FloatOption('float', 'Test float option', default=2.3)
|
|
stroption = StrOption('str', 'Test string option', default="abc")
|
|
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
|
|
wantref_option = BoolOption('wantref', 'Tests', default=False)
|
|
wantframework_option = BoolOption('wantframework', 'Test', default=False)
|
|
|
|
gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
|
|
descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
|
|
wantref_option, stroption,
|
|
wantframework_option,
|
|
intoption, boolop])
|
|
return descr
|
|
|
|
|
|
def test_compare_configs():
|
|
"config object comparison"
|
|
descr = make_description()
|
|
conf1 = Config(descr)
|
|
conf2 = Config(descr)
|
|
conf2.wantref = True
|
|
assert conf1 != conf2
|
|
assert hash(conf1) != hash(conf2)
|
|
assert conf1.getkey() != conf2.getkey()
|
|
conf1.wantref = True
|
|
assert conf1 == conf2
|
|
assert hash(conf1) == hash(conf2)
|
|
assert conf1.getkey() == conf2.getkey()
|
|
# ____________________________________________________________
|
|
|
|
def test_iter_config():
|
|
"iteration on config object"
|
|
s = StrOption("string", "", default="string")
|
|
s2 = StrOption("string2", "", default="string2")
|
|
descr = OptionDescription("options", "", [s,s2])
|
|
config = Config(descr)
|
|
assert [(name, value) for name, value in config] == \
|
|
[('string', 'string'), ('string2', 'string2')]
|
|
|
|
def test_iter_subconfig():
|
|
"iteration on config sub object"
|
|
descr = make_description()
|
|
conf = Config(descr)
|
|
for (name, value), (gname, gvalue) in \
|
|
zip(conf.gc, [("name", "ref"), ("dummy", False)]):
|
|
assert name == gname
|
|
assert value == gvalue
|
|
#____________________________________________________________
|
|
def test_getpaths():
|
|
descr = make_description()
|
|
config = Config(descr)
|
|
|
|
assert config.getpaths() == ['gc.name', 'gc.dummy', 'gc.float', 'bool',
|
|
'objspace', 'wantref', 'str', 'wantframework',
|
|
'int', 'boolop']
|
|
assert config.getpaths() == descr.getpaths()
|
|
assert config.gc.getpaths() == ['name', 'dummy', 'float']
|
|
assert config.gc.getpaths() == descr.gc.getpaths()
|
|
assert config.getpaths(include_groups=True) == [
|
|
'gc', 'gc.name', 'gc.dummy', 'gc.float',
|
|
'bool', 'objspace', 'wantref', 'str', 'wantframework', 'int', 'boolop']
|
|
|
|
assert config.getpaths(True) == descr.getpaths(True)
|
|
|
|
def test_getpaths_with_hidden():
|
|
objspaceoption = ChoiceOption('objspace', 'Object space',
|
|
['std', 'thunk'], 'std')
|
|
booloption = BoolOption('bool', 'Test boolean option', default=True)
|
|
booloption.hide()
|
|
intoption = IntOption('int', 'Test int option', default=0)
|
|
stroption = StrOption('str', 'Test string option', default="abc")
|
|
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
|
|
wantref_option = BoolOption('wantref', 'Test requires', default=False)
|
|
wantframework_option = BoolOption('wantframework', 'Test requires',
|
|
default=False)
|
|
|
|
descr = OptionDescription('tiramisu', '', [booloption, objspaceoption,
|
|
wantref_option, stroption,
|
|
wantframework_option,
|
|
intoption, boolop])
|
|
|
|
config = Config(descr)
|
|
result = ['objspace', 'wantref', 'str', 'wantframework', 'int', 'boolop']
|
|
assert config.getpaths() == result
|
|
r2 = ['bool', 'objspace', 'wantref', 'str', 'wantframework', 'int', 'boolop']
|
|
assert config.getpaths(allpaths=True) == r2
|
|
|
|
def test_str():
|
|
descr = make_description()
|
|
c = Config(descr)
|
|
print c # does not crash
|
|
|
|
#def test_dir():
|
|
# descr = make_description()
|
|
# c = Config(descr)
|
|
# print dir(c)
|
|
|
|
def test_make_dict():
|
|
"serialization of the whole config to a dict"
|
|
descr = OptionDescription("opt", "", [
|
|
OptionDescription("s1", "", [
|
|
BoolOption("a", "", default=False)]),
|
|
IntOption("int", "", default=42)])
|
|
config = Config(descr)
|
|
d = make_dict(config)
|
|
assert d == {"s1.a": False, "int": 42}
|
|
config.int = 43
|
|
config.s1.a = True
|
|
d = make_dict(config)
|
|
assert d == {"s1.a": True, "int": 43}
|
|
d2 = make_dict(config, flatten=True)
|
|
assert d2 == {'a': True, 'int': 43}
|
|
|
|
def test_delattr():
|
|
"delattr, means suppression of an option in a config"
|
|
descr = OptionDescription("opt", "", [
|
|
OptionDescription("s1", "", [
|
|
BoolOption("a", "", default=False)]),
|
|
IntOption("int", "", default=42)])
|
|
c = Config(descr)
|
|
c.int = 45
|
|
assert c.int == 45
|
|
del c.int
|
|
assert c.int == 42
|
|
c.int = 45
|
|
assert c.int == 45
|
|
|
|
def test_find_in_config():
|
|
"finds option in config"
|
|
descr = make_description()
|
|
conf = Config(descr)
|
|
assert conf.find(byname='dummy') == [conf.unwrap_from_name('dummy')]
|
|
assert conf.find_first(byname='dummy') == conf.unwrap_from_name('dummy')
|
|
assert conf.find(bytype=ChoiceOption) == [conf.unwrap_from_name('name'), conf.unwrap_from_name('objspace')]
|
|
assert conf.find_first(bytype=ChoiceOption) == conf.unwrap_from_name('name')
|
|
assert conf.find(byvalue='ref') == [conf.unwrap_from_name('name')]
|
|
assert conf.find_first(byvalue='ref') == conf.unwrap_from_name('name')
|
|
# combinaison of filters
|
|
assert conf.find(bytype=BoolOption, byname='dummy') == [conf.unwrap_from_name('dummy')]
|
|
assert conf.find_first(bytype=BoolOption, byname='dummy') == conf.unwrap_from_name('dummy')
|
|
assert conf.find(byvalue=False, byname='dummy') == [conf.unwrap_from_name('dummy')]
|
|
assert conf.find_first(byvalue=False, byname='dummy') == conf.unwrap_from_name('dummy')
|
|
# byattrs
|
|
assert conf.find_first(byattrs= dict(default=2.3)) == conf.unwrap_from_name('float')
|
|
assert conf.find_first(byvalue=False, byname='dummy', byattrs=dict(default=False)) == conf.unwrap_from_name('dummy')
|
|
|
|
#def test_validator():
|
|
# "validates the integrity of an option towards a whole configuration"
|
|
# def my_validator_1(config):
|
|
# assert config is c
|
|
|
|
# def my_validator_2(config):
|
|
# assert config is c
|
|
# raise ConflictConfigError
|
|
|
|
# descr = OptionDescription("opt", "", [
|
|
# BoolOption('booloption1', 'option test1', default=False,
|
|
# validator=my_validator_1),
|
|
# BoolOption('booloption2', 'option test2', default=False,
|
|
# validator=my_validator_2),
|
|
# BoolOption('booloption4', 'option test4', default=False,
|
|
# ),
|
|
# ])
|
|
# c = Config(descr)
|
|
# c.booloption1 = True
|
|
## raises(ConfigError, "c.booloption2 = True")
|
|
## assert c.booloption2 is False
|
|
## raises(ConfigError, "c.booloption3 = True")
|
|
# assert c.booloption2 is False
|
|
# c.booloption4 = True
|
|
# assert c.booloption2 is False
|
|
# c.booloption2 = False
|
|
# assert c.booloption2 is False
|
|
#
|