From bb1f6947e388366ec0abd6f2534a02d9def1b65e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 9 Dec 2013 17:55:52 +0100 Subject: [PATCH] better name's validation --- test/test_option.py | 24 ++++++++++++++++++++++++ tiramisu/option.py | 4 +--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/test/test_option.py b/test/test_option.py index 0492ed2..4bb31cb 100644 --- a/test/test_option.py +++ b/test/test_option.py @@ -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)") diff --git a/tiramisu/option.py b/tiramisu/option.py index 246363c..09e0dbd 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -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 \