tiramisu/test/new_api/test_option_default.py
Emmanuel Garette f1f9b83e82 owner test
2017-12-23 12:29:45 +01:00

130 lines
5.7 KiB
Python

"test all types of option default values for options, add new option in a descr"
from .autopath import do_autopath
do_autopath()
from py.test import raises
from tiramisu.setting import owners
from tiramisu.error import PropertiesOptionError, ConfigError
from tiramisu import IntOption, FloatOption, StrOption, ChoiceOption, \
BoolOption, OptionDescription, Config, getapi, undefined
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', 'Test requires', default=False,
requires=['boolop'])
wantframework_option = BoolOption('wantframework', 'Test requires',
default=False,
requires=['boolop'])
gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
wantref_option, stroption,
wantframework_option,
intoption, boolop])
return descr
#____________________________________________________________
# 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])
api = getapi(Config(group))
# so when the default value is not set, there is actually a default value
assert api.option('dummy1').value.get() is None
assert api.option('dummy2').value.get() is None
def test_set_defaut_value_from_option_object():
"""Options have an available default setting and can give it back"""
b = BoolOption("boolean", "", default=False)
assert b.impl_getdefault() is False
def test_force_default_on_freeze():
"a frozen option wich is forced returns his default"
dummy1 = BoolOption('dummy1', 'doc dummy', default=False, properties=('force_default_on_freeze',))
dummy2 = BoolOption('dummy2', 'doc dummy', default=True)
group = OptionDescription('group', '', [dummy1, dummy2])
api = getapi(Config(group))
api.property.read_write()
owner = api.owner.get()
api.option('dummy1').value.set(True)
api.option('dummy2').value.set(False)
assert api.option('dummy1').owner.get() == owner
assert api.option('dummy2').owner.get() == owner
api.option('dummy1').property.add('frozen')
api.option('dummy2').property.add('frozen')
assert api.option('dummy1').value.get() is False
assert api.option('dummy2').value.get() is False
assert api.option('dummy1').owner.isdefault()
assert api.option('dummy2').owner.get() == owner
raises(PropertiesOptionError, "api.option('dummy2').owner.set('frozen')")
raises(PropertiesOptionError, "api.option('dummy1').value.reset()")
api.option('dummy1').property.pop('frozen')
api.option('dummy1').value.reset()
api.option('dummy1').property.add('frozen')
raises(PropertiesOptionError, "api.option('dummy2').owner.set('frozen')")
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])
api = getapi(Config(group))
api.property.read_write()
api.option('dummy1').value.set([undefined, True])
api.option('dummy2').value.set([undefined, False])
owner = api.owner.get()
assert api.option('dummy1').owner.get() == owner
assert api.option('dummy2').owner.get() == owner
api.option('dummy1').property.add('frozen')
api.option('dummy2').property.add('frozen')
assert api.option('dummy1').value.get() == [False]
assert api.option('dummy2').value.get() == [True, False]
assert api.option('dummy1').owner.isdefault()
assert api.option('dummy2').owner.get() == owner
raises(PropertiesOptionError, "api.option('dummy2').owner.set('owner')")
raises(PropertiesOptionError, "api.option('dummy2').value.reset()")
api.option('dummy1').property.pop('frozen')
api.option('dummy1').value.reset()
api.option('dummy1').property.add('frozen')
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)])
api = getapi(Config(descr))
api.option('b').value.set(True)
def test_choice_with_no_default():
descr = OptionDescription("test", "", [
ChoiceOption("backend", "", ("c", "cli"))])
api = getapi(Config(descr))
assert api.option('backend').value.get() is None
api.option('backend').value.set('c')
def test_choice_with_default():
descr = OptionDescription("test", "", [
ChoiceOption("backend", "", ("c", "cli"), default="cli")])
api = getapi(Config(descr))
assert api.option('backend').value.get() == 'cli'