2012-07-13 09:42:14 +02:00
|
|
|
"test all types of option default values for options, add new option in a descr"
|
|
|
|
import autopath
|
2014-10-26 08:51:45 +01:00
|
|
|
from py.test import raises
|
2012-07-13 09:42:14 +02:00
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
from tiramisu.config import Config
|
2014-10-26 08:51:45 +01:00
|
|
|
from tiramisu.setting import owners
|
|
|
|
from tiramisu.error import PropertiesOptionError, ConfigError
|
2013-04-20 21:58:52 +02:00
|
|
|
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
|
|
|
|
BoolOption, OptionDescription
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
def make_description():
|
|
|
|
gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
|
|
|
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
|
|
objspaceoption = ChoiceOption('objspace', 'Object space',
|
2013-04-20 21:58:52 +02:00
|
|
|
['std', 'thunk'], 'std')
|
2012-07-13 09:42:14 +02:00
|
|
|
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', 'Test requires', default=False,
|
2013-04-20 21:58:52 +02:00
|
|
|
requires=['boolop'])
|
2012-07-13 09:42:14 +02:00
|
|
|
wantframework_option = BoolOption('wantframework', 'Test requires',
|
|
|
|
default=False,
|
|
|
|
requires=['boolop'])
|
2012-11-12 12:06:58 +01:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
|
|
|
|
descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
|
2013-04-20 21:58:52 +02:00
|
|
|
wantref_option, stroption,
|
|
|
|
wantframework_option,
|
|
|
|
intoption, boolop])
|
2012-07-13 09:42:14 +02:00
|
|
|
return descr
|
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
#____________________________________________________________
|
|
|
|
# default values
|
|
|
|
def test_default_is_none():
|
|
|
|
"""
|
|
|
|
Most constructors take a ``default`` argument that specifies the default
|
|
|
|
value of the option. If this argument is not supplied the default value is
|
|
|
|
assumed to be ``None``.
|
|
|
|
"""
|
|
|
|
dummy1 = BoolOption('dummy1', 'doc dummy')
|
|
|
|
dummy2 = BoolOption('dummy2', 'doc dummy')
|
|
|
|
group = OptionDescription('group', '', [dummy1, dummy2])
|
|
|
|
config = Config(group)
|
|
|
|
# so when the default value is not set, there is actually a default value
|
2013-04-20 21:58:52 +02:00
|
|
|
assert config.dummy1 is None
|
|
|
|
assert config.dummy2 is None
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
def test_set_defaut_value_from_option_object():
|
|
|
|
"""Options have an available default setting and can give it back"""
|
|
|
|
b = BoolOption("boolean", "", default=False)
|
2013-05-08 18:14:42 +02:00
|
|
|
assert b.impl_getdefault() is False
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
2012-08-13 16:06:02 +02:00
|
|
|
def test_force_default_on_freeze():
|
|
|
|
"a frozen option wich is forced returns his default"
|
2013-04-03 12:20:26 +02:00
|
|
|
dummy1 = BoolOption('dummy1', 'doc dummy', default=False, properties=('force_default_on_freeze',))
|
2012-08-13 16:06:02 +02:00
|
|
|
dummy2 = BoolOption('dummy2', 'doc dummy', default=True)
|
|
|
|
group = OptionDescription('group', '', [dummy1, dummy2])
|
|
|
|
config = Config(group)
|
2014-10-26 08:51:45 +01:00
|
|
|
config.read_write()
|
|
|
|
owners.addowner("frozen")
|
2012-08-13 16:06:02 +02:00
|
|
|
config.dummy1 = True
|
|
|
|
config.dummy2 = False
|
2014-10-26 08:51:45 +01:00
|
|
|
assert config.getowner(dummy1) == owners.user
|
|
|
|
assert config.getowner(dummy2) == owners.user
|
2013-04-20 17:30:05 +02:00
|
|
|
setting = config.cfgimpl_get_settings()
|
|
|
|
setting[dummy1].append('frozen')
|
|
|
|
setting[dummy2].append('frozen')
|
2013-04-20 21:58:52 +02:00
|
|
|
assert config.dummy1 is False
|
|
|
|
assert config.dummy2 is False
|
2014-10-26 08:51:45 +01:00
|
|
|
assert config.getowner(dummy1) == owners.default
|
|
|
|
assert config.getowner(dummy2) == owners.user
|
|
|
|
raises(ConfigError, "config.cfgimpl_get_values().setowner(dummy1, owners.frozen)")
|
|
|
|
raises(PropertiesOptionError, "config.cfgimpl_get_values().setowner(dummy2, owners.frozen)")
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
2013-08-28 22:50:35 +02:00
|
|
|
def test_force_default_on_freeze_multi():
|
|
|
|
dummy1 = BoolOption('dummy1', 'doc dummy', default=[False], properties=('force_default_on_freeze',), multi=True)
|
|
|
|
dummy2 = BoolOption('dummy2', 'doc dummy', default=[True], multi=True)
|
|
|
|
group = OptionDescription('group', '', [dummy1, dummy2])
|
|
|
|
config = Config(group)
|
2014-10-26 08:51:45 +01:00
|
|
|
config.read_write()
|
|
|
|
owners.addowner("frozenmulti")
|
2013-08-28 22:50:35 +02:00
|
|
|
config.dummy1.append(True)
|
|
|
|
config.dummy2.append(False)
|
2014-10-26 08:51:45 +01:00
|
|
|
assert config.getowner(dummy1) == owners.user
|
|
|
|
assert config.getowner(dummy2) == owners.user
|
2013-08-28 22:50:35 +02:00
|
|
|
setting = config.cfgimpl_get_settings()
|
|
|
|
setting[dummy1].append('frozen')
|
|
|
|
setting[dummy2].append('frozen')
|
|
|
|
assert config.dummy1 == [False]
|
|
|
|
assert config.dummy2 == [True, False]
|
2014-10-26 08:51:45 +01:00
|
|
|
assert config.getowner(dummy1) == owners.default
|
|
|
|
assert config.getowner(dummy2) == owners.user
|
|
|
|
raises(ConfigError, "config.cfgimpl_get_values().setowner(dummy1, owners.frozenmulti)")
|
|
|
|
raises(PropertiesOptionError, "config.cfgimpl_get_values().setowner(dummy2, owners.frozenmulti)")
|
2013-08-28 22:50:35 +02:00
|
|
|
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_overrides_changes_option_value():
|
|
|
|
"with config.override(), the default is changed and the value is changed"
|
|
|
|
descr = OptionDescription("test", "", [
|
|
|
|
BoolOption("b", "", default=False)])
|
|
|
|
config = Config(descr)
|
|
|
|
config.b = True
|
2013-04-20 21:58:52 +02:00
|
|
|
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_choice_with_no_default():
|
|
|
|
descr = OptionDescription("test", "", [
|
2013-04-03 12:20:26 +02:00
|
|
|
ChoiceOption("backend", "", ("c", "cli"))])
|
2012-07-13 09:42:14 +02:00
|
|
|
config = Config(descr)
|
|
|
|
assert config.backend is None
|
|
|
|
config.backend = "c"
|
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_choice_with_default():
|
|
|
|
descr = OptionDescription("test", "", [
|
2013-04-03 12:20:26 +02:00
|
|
|
ChoiceOption("backend", "", ("c", "cli"), default="cli")])
|
2012-07-13 09:42:14 +02:00
|
|
|
config = Config(descr)
|
|
|
|
assert config.backend == "cli"
|