From f1f9b83e8209e1eaf032ae79c5eb7f19e7c447b3 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 23 Dec 2017 12:29:45 +0100 Subject: [PATCH] owner test --- test/new_api/test_option_default.py | 130 +++++++++++++++++++++ test/new_api/test_option_owner.py | 168 ++++++++++++++++++++++++++++ tiramisu/api.py | 27 +++-- tiramisu/value.py | 30 ++--- 4 files changed, 327 insertions(+), 28 deletions(-) create mode 100644 test/new_api/test_option_default.py create mode 100644 test/new_api/test_option_owner.py diff --git a/test/new_api/test_option_default.py b/test/new_api/test_option_default.py new file mode 100644 index 0000000..a564700 --- /dev/null +++ b/test/new_api/test_option_default.py @@ -0,0 +1,130 @@ +"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' diff --git a/test/new_api/test_option_owner.py b/test/new_api/test_option_owner.py new file mode 100644 index 0000000..9412c43 --- /dev/null +++ b/test/new_api/test_option_owner.py @@ -0,0 +1,168 @@ +from .autopath import do_autopath +do_autopath() + +from py.test import raises + +from tiramisu.setting import owners, groups +from tiramisu import ChoiceOption, BoolOption, IntOption, FloatOption, \ + StrOption, OptionDescription, SymLinkOption, MasterSlaves, Config, getapi +from tiramisu.error import ConfigError, ConstError, PropertiesOptionError, APIError + + +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) + wantframework_option = BoolOption('wantframework', 'Test requires', + default=False) + + gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption]) + descr = OptionDescription('tiram', '', [gcgroup, booloption, objspaceoption, + wantref_option, stroption, + wantframework_option, + intoption, boolop]) + return descr + + +def test_default_owner(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == 'default' + api.option('dummy').value.set(True) + owner = api.owner.get() + assert api.option('dummy').owner.get() == owner + + +def test_hidden_owner(): + gcdummy = BoolOption('dummy', 'dummy', default=False, properties=('hidden',)) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + api.property.read_write() + #raises(PropertiesOptionError, "api.forcepermissive.option('dummy').owner.get()") + #raises(PropertiesOptionError, "api.option('dummy').owner.isdefault()") + #raises(PropertiesOptionError, "api.forcepermissive.option('dummy').owner.isdefault()") + api.permissive.set(('hidden',)) + api.forcepermissive.option('dummy').value.get() + api.forcepermissive.option('dummy').owner.isdefault() + + +def test_addowner(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == 'default' + assert api.option('dummy').owner.isdefault() + api.owner.set('gen_config') + api.option('dummy').value.set(True) + assert api.option('dummy').owner.get() == owners.gen_config + assert not api.option('dummy').owner.isdefault() + + +def test_addowner_multiple_time(): + owners.addowner("testowner") + raises(ConstError, 'owners.addowner("testowner")') + + +def test_delete_owner(): + owners.addowner('deleted') + raises(ConstError, 'del(owners.deleted)') + + +def test_owner_is_not_a_string(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == owners.default + assert api.option('dummy').owner.get() == 'default' + assert isinstance(api.option('dummy').owner.get(), owners.Owner) + api.option('dummy').value.set(True) + assert api.option('dummy').owner.get() == 'user' + + +def test_setowner_without_valid_owner(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == 'default' + + +def test_setowner_for_value(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == 'default' + owners.addowner("new") + raises(ConfigError, "api.option('dummy').owner.set('new')") + api.option('dummy').value.set(False) + assert api.option('dummy').owner.get() == owners.user + api.option('dummy').owner.set('new') + assert api.option('dummy').owner.get() == owners.new + + +def test_setowner_read_only(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr = OptionDescription('tiramisu', '', [gcdummy]) + api = getapi(Config(descr)) + api.property.read_write() + assert api.option('dummy').value.get() is False + assert api.option('dummy').owner.get() == 'default' + owners.addowner("readonly") + api.option('dummy').value.set(False) + assert api.option('dummy').owner.get() == owners.user + api.property.read_only() + raises(PropertiesOptionError, + "api.option('dummy').owner.set('readonly')") + assert api.option('dummy').owner.get() == owners.user + + +def test_setowner_optiondescription(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + descr1 = OptionDescription('tiramisu', '', [gcdummy]) + descr = OptionDescription('tiramisu', '', [descr1]) + api = getapi(Config(descr)) + raises(APIError, "api.option('tiramisu').owner.get()") + raises(APIError, "api.option('tiramisu').owner.set('user')") + + +def test_setowner_symlinkoption(): + gcdummy = BoolOption('dummy', 'dummy', default=False) + s = SymLinkOption('symdummy', gcdummy) + descr1 = OptionDescription('tiramisu', '', [gcdummy, s]) + descr = OptionDescription('tiramisu', '', [descr1]) + api = getapi(Config(descr)) + assert api.option('tiramisu.symdummy').owner.isdefault() + api.option('tiramisu.dummy').value.set(True) + assert not api.option('tiramisu.symdummy').owner.isdefault() + raises(TypeError, "api.option('tiramisu.symdummy').owner.set('user')") + + +def test_owner_masterslaves(): + b = IntOption('int', 'Test int option', default=[0], multi=True) + c = StrOption('str', 'Test string option', multi=True) + descr = MasterSlaves("int", "", [b, c]) + od = OptionDescription('od', '', [descr]) + api = getapi(Config(od)) + raises(ConfigError, "api.option('int.str', 0).owner.set('user')") + + api.option('int.int').value.set([0, 1]) + api.option('int.str', 0).value.set('yes') + assert not api.option('int.str', 0).owner.isdefault() + assert api.option('int.str', 1).owner.isdefault() + api.option('int.str', 0).owner.set('user') + assert api.option('int.str', 0).owner.get() == owners.user + assert api.option('int.str', 1).owner.isdefault() + assert api.option('int.str', 0).value.get() == 'yes' + assert api.option('int.str', 1).value.get() == None diff --git a/tiramisu/api.py b/tiramisu/api.py index 610d132..6cbe713 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -494,6 +494,15 @@ class TiramisuContextOwner(TiramisuContext): def get(self): return self.config_bag.config.cfgimpl_get_settings().getowner() + @count + def set(self, owner): + try: + obj_owner = getattr(owners, owner) + except AttributeError: + owners.addowner(owner) + obj_owner = getattr(owners, owner) + self.config_bag.config.cfgimpl_get_settings().setowner(obj_owner) + class TiramisuContextProperty(TiramisuContext): @count @@ -595,15 +604,15 @@ class TiramisuDispatcherOption(TiramisuContextOption): config_bag.option = opt if index is not None and not opt.impl_is_master_slaves('slave'): raise APIError('index must be set only with a slave option') - if opt.impl_is_symlinkoption(): - config_bag.ori_option = config_bag.option - config_bag.option = opt.impl_getopt() - true_path = config_bag.option.impl_getpath(self.config_bag.config) - config_bag.config.unwrap_from_path(true_path, - index, - config_bag) - else: - true_path = None + #if opt.impl_is_symlinkoption(): + # config_bag.ori_option = config_bag.option + # config_bag.option = opt.impl_getopt() + # true_path = config_bag.option.impl_getpath(self.config_bag.config) + # config_bag.config.unwrap_from_path(true_path, + # index, + # config_bag) + #else: + # true_path = None return TiramisuOption(path, index, config_bag) diff --git a/tiramisu/value.py b/tiramisu/value.py index 8607758..5f3cb5b 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -450,6 +450,17 @@ class Values(object): #______________________________________________________________________ # owner + def is_default_owner(self, + path, + index, + config_bag, + validate_meta=undefined): + return self._getowner(path, + index, + config_bag, + validate_meta=validate_meta, + only_default=True) == owners.default + def getowner(self, path, index, @@ -462,12 +473,6 @@ class Values(object): was present :returns: a `setting.owners.Owner` object """ - opt = config_bag.option - if opt.impl_is_symlinkoption(): - config_bag.ori_option = opt - opt = opt.impl_getopt() - config_bag.option = opt - path = opt.impl_getpath(self._getcontext()) return self._getowner(path, index, config_bag) @@ -543,19 +548,6 @@ class Values(object): index, config_bag) self._p_.setowner(path, owner, index=index) - - def is_default_owner(self, - path, - index, - config_bag, - validate_meta=undefined): - owner = self._getowner(path, - index, - config_bag, - validate_meta=validate_meta, - only_default=True) - return owner == owners.default - #______________________________________________________________________ # reset