better name's validation

This commit is contained in:
Emmanuel Garette 2013-12-09 17:55:52 +01:00
parent 6e4f19eebe
commit bb1f6947e3
2 changed files with 25 additions and 3 deletions

View File

@ -2,6 +2,7 @@
and to compare them
"""
import autopath
from py.test import raises
from tiramisu.option import BoolOption, IntOption
@ -36,3 +37,26 @@ from tiramisu.option import BoolOption, IntOption
# assert dummy1 != dummy5
# assert dummy1 == dummy6
# assert dummy1 != dummy7
def test_option_valid_name():
IntOption('test', '')
raises(ValueError, 'IntOption(1, "")')
raises(ValueError, 'IntOption("impl_test", "")')
raises(ValueError, 'IntOption("_test", "")')
raises(ValueError, 'IntOption("unwrap_from_path", "")')
def test_option_get_information():
i = IntOption('test', '')
string = 'some informations'
i.impl_set_information('info', string)
assert i.impl_get_information('info') == string
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
def test_option_multi():
IntOption('test', '', multi=True)
IntOption('test', '', multi=True, default_multi=1)
IntOption('test', '', default=[1], multi=True, default_multi=1)
raises(ValueError, "IntOption('test', '', default_multi=1)")

View File

@ -40,9 +40,7 @@ forbidden_names = ('iter_all', 'iter_group', 'find', 'find_first',
def valid_name(name):
"an option's name is a str and does not start with 'impl' or 'cfgimpl'"
try:
name = str(name)
except:
if not isinstance(name, str):
return False
if re.match(name_regexp, name) is None and not name.startswith('_') \
and name not in forbidden_names \