validator's function can have 1 arg, 2 args or 3 args

This commit is contained in:
2017-01-12 21:58:53 +01:00
parent 01b7fc873e
commit 89fd367b20
5 changed files with 70 additions and 19 deletions

View File

@ -40,6 +40,19 @@ def is_context(value, context):
raise ValueError('not context')
def value_values(value, values):
if not (value == 'val' and values == ['val'] or
value == 'val1' and values == ['val1'] or
value == 'val2' and values == ['val1', 'val2']):
raise ValueError('error')
def value_values_index(value, values, index):
value_values(value, values)
if not (index == 0 or (value == 'val2' and index == 1)):
raise ValueError('error 2')
def test_validator():
opt1 = StrOption('opt1', '', validator=return_true, default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, default='val')")
@ -60,6 +73,24 @@ def test_validator_params():
raises(ValueError, "cfg.opt2 = 'val'")
def test_validator_params_value_values():
opt1 = StrOption('opt1', '', validator=value_values, default=['val'], multi=True)
root = OptionDescription('root', '', [opt1])
cfg = Config(root)
assert cfg.opt1 == ['val']
cfg.opt1[0] = 'val1'
cfg.opt1.append('val2')
def test_validator_params_value_values_index():
opt1 = StrOption('opt1', '', validator=value_values_index, default=['val'], multi=True)
root = OptionDescription('root', '', [opt1])
cfg = Config(root)
assert cfg.opt1 == ['val']
cfg.opt1[0] = 'val1'
cfg.opt1.append('val2')
def test_validator_params_context():
opt1 = StrOption('opt1', '', validator=is_context, validator_params={'': ((None,),)}, default='val')
root = OptionDescription('root', '', [opt1])