60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
|
import autopath
|
||
|
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):
|
||
|
if value == 'val' and param in [None, 'yes']:
|
||
|
return True
|
||
|
|
||
|
|
||
|
def return_false(value, param=None):
|
||
|
if value == 'val' and param in [None, 'yes']:
|
||
|
return False
|
||
|
|
||
|
|
||
|
def return_val(value, param=None):
|
||
|
return 'val'
|
||
|
|
||
|
|
||
|
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])
|
||
|
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])
|
||
|
cfg = Config(root)
|
||
|
assert cfg.opt1 == 'val'
|
||
|
raises(ValueError, "cfg.opt2 = 'val'")
|
||
|
raises(ConfigError, "cfg.opt3 = 'val'")
|
||
|
|
||
|
|
||
|
def test_validator_params_key():
|
||
|
opt1 = StrOption('opt1', '', validator=return_true, validator_params={'param': ('yes',)}, default='val')
|
||
|
raises(TypeError, "StrOption('opt2', '', validator=return_true, validator_params={'param_unknown': ('yes',)}, default='val')")
|
||
|
root = OptionDescription('root', '', [opt1])
|
||
|
cfg = Config(root)
|
||
|
assert cfg.opt1 == 'val'
|
||
|
|
||
|
|
||
|
def test_validator_params_option():
|
||
|
opt0 = StrOption('opt0', '', default='val')
|
||
|
raises(ValueError, "opt1 = StrOption('opt1', '', validator=return_true, validator_params={'': ((opt0, False),)}, default='val')")
|