context could be useful in a callback, now we can retrieve context in a function (be careful to infinite loop)
This commit is contained in:
@ -44,6 +44,13 @@ def return_calc(i, j, k):
|
||||
return i + j + k
|
||||
|
||||
|
||||
def is_config(config, **kwargs):
|
||||
if isinstance(config, Config):
|
||||
return 'yes'
|
||||
else:
|
||||
return 'no'
|
||||
|
||||
|
||||
def make_description():
|
||||
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
|
||||
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
||||
@ -254,6 +261,29 @@ def test_callback_invalid():
|
||||
raises(ValueError, "StrOption('val2', '', callback=return_value, callback_params={'': 'string'})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': (('string', False),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1, 'string'),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1, False, 'unknown'),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1,),)})")
|
||||
|
||||
|
||||
def test_callback_with_context():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'': ((None,),), 'value': ('string',)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'yes'
|
||||
|
||||
|
||||
def test_callback_with_context_named():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'config': ((None,),)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'yes'
|
||||
|
||||
|
||||
def test_callback_with_error():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'': ('string',), 'value': ('string',)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'no'
|
||||
|
||||
|
||||
def test_callback_value():
|
||||
|
Reference in New Issue
Block a user