add warning ability

This commit is contained in:
2013-09-24 23:19:20 +02:00
parent 5ba97c5928
commit 06baff2f3b
3 changed files with 128 additions and 56 deletions

View File

@ -3,7 +3,6 @@ from py.test import raises
from tiramisu.config import Config
from tiramisu.option import StrOption, OptionDescription
from tiramisu.error import ConfigError
def return_true(value, param=None):
@ -13,37 +12,36 @@ def return_true(value, param=None):
def return_false(value, param=None):
if value == 'val' and param in [None, 'yes']:
return False
raise ValueError('error')
def return_val(value, param=None):
return 'val'
def return_if_val(value):
if value != 'val':
raise ValueError('error')
def test_validator():
opt1 = StrOption('opt1', '', validator=return_true, default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, default='val')")
raises(ConfigError, "StrOption('opt3', '', validator=return_val, default='val')")
opt2 = StrOption('opt2', '', validator=return_false)
opt3 = StrOption('opt3', '', validator=return_val)
root = OptionDescription('root', '', [opt1, opt2, opt3])
root = OptionDescription('root', '', [opt1, opt2])
cfg = Config(root)
assert cfg.opt1 == 'val'
raises(ValueError, "cfg.opt2 = 'val'")
raises(ConfigError, "cfg.opt3 = 'val'")
def test_validator_params():
opt1 = StrOption('opt1', '', validator=return_true, validator_params={'': ('yes',)}, default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, validator_params={'': ('yes',)}, default='val')")
raises(ConfigError, "StrOption('opt3', '', validator=return_val, validator_params={'': ('yes',)}, default='val')")
opt2 = StrOption('opt2', '', validator=return_false, validator_params={'': ('yes',)})
opt3 = StrOption('opt3', '', validator=return_val, validator_params={'': ('yes',)})
root = OptionDescription('root', '', [opt1, opt2, opt3])
root = OptionDescription('root', '', [opt1, opt2])
cfg = Config(root)
assert cfg.opt1 == 'val'
raises(ValueError, "cfg.opt2 = 'val'")
raises(ConfigError, "cfg.opt3 = 'val'")
def test_validator_params_key():
@ -57,3 +55,36 @@ def test_validator_params_key():
def test_validator_params_option():
opt0 = StrOption('opt0', '', default='val')
raises(ValueError, "opt1 = StrOption('opt1', '', validator=return_true, validator_params={'': ((opt0, False),)}, default='val')")
def test_validator_multi():
opt1 = StrOption('opt1', '', validator=return_if_val, multi=True)
root = OptionDescription('root', '', [opt1])
cfg = Config(root)
assert cfg.opt1 == []
cfg.opt1.append('val')
assert cfg.opt1 == ['val']
raises(ValueError, "cfg.opt1.append('val1')")
raises(ValueError, "cfg.opt1 = ['val', 'val1']")
def test_validator_warning():
opt1 = StrOption('opt1', '', validator=return_true, default='val', only_warning=True)
opt2 = StrOption('opt2', '', validator=return_false, only_warning=True)
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, only_warning=True)
root = OptionDescription('root', '', [opt1, opt2, opt3])
cfg = Config(root)
assert cfg.opt1 == 'val'
cfg.opt1 = 'val'
assert cfg.cfgimpl_get_values().has_warning() is False
cfg.opt2 = 'val'
assert cfg.cfgimpl_get_values().has_warning() is True
assert cfg.cfgimpl_get_values().get_last_warning() == 'invalid value val for option opt2: error'
assert cfg.cfgimpl_get_values().has_warning() is False
cfg.opt3.append('val')
assert cfg.cfgimpl_get_values().has_warning() is False
cfg.opt3.append('val1')
assert cfg.cfgimpl_get_values().has_warning() is True
assert cfg.cfgimpl_get_values().get_last_warning() == 'invalid value val1 for option opt3: error'
assert cfg.cfgimpl_get_values().has_warning() is False
raises(ValueError, "cfg.opt2 = 1")