_empty is u'' for UnicodeOption

This commit is contained in:
Emmanuel Garette 2013-04-17 22:06:10 +02:00
parent eea96cc3d1
commit 5e67522f91
2 changed files with 38 additions and 14 deletions

View File

@ -2,7 +2,7 @@ import autopath
#from py.test import raises #from py.test import raises
from tiramisu.config import Config, mandatory_warnings from tiramisu.config import Config, mandatory_warnings
from tiramisu.option import StrOption, OptionDescription from tiramisu.option import StrOption, OptionDescription, UnicodeOption
from tiramisu.error import PropertiesOptionError from tiramisu.error import PropertiesOptionError
@ -11,7 +11,7 @@ def make_description():
properties=('mandatory', )) properties=('mandatory', ))
stroption1 = StrOption('str1', 'Test string option', stroption1 = StrOption('str1', 'Test string option',
properties=('mandatory', )) properties=('mandatory', ))
stroption2 = StrOption('str2', 'Test string option', stroption2 = UnicodeOption('unicode2', 'Test string option',
properties=('mandatory', )) properties=('mandatory', ))
stroption3 = StrOption('str3', 'Test string option', multi=True, stroption3 = StrOption('str3', 'Test string option', multi=True,
properties=('mandatory', )) properties=('mandatory', ))
@ -42,9 +42,9 @@ def test_mandatory_rw():
setting = config.cfgimpl_get_settings() setting = config.cfgimpl_get_settings()
setting.read_write() setting.read_write()
#not mandatory in rw #not mandatory in rw
config.str2 config.str1
config.str2 = 'yes' config.str1 = 'yes'
assert config.str2 == 'yes' assert config.str1 == 'yes'
def test_mandatory_default(): def test_mandatory_default():
@ -180,6 +180,29 @@ def test_mandatory_disabled():
assert prop == ['disabled', 'mandatory'] assert prop == ['disabled', 'mandatory']
def test_mandatory_unicode():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
config.unicode2
setting.read_only()
prop = []
try:
config.unicode2
except PropertiesOptionError, err:
prop = err.proptype
assert prop == ['mandatory']
setting.read_write()
config.unicode2 = u''
setting.read_only()
prop = []
try:
config.unicode2
except PropertiesOptionError, err:
prop = err.proptype
assert prop == ['mandatory']
def test_mandatory_warnings_ro(): def test_mandatory_warnings_ro():
descr = make_description() descr = make_description()
config = Config(descr) config = Config(descr)
@ -192,11 +215,11 @@ def test_mandatory_warnings_ro():
except PropertiesOptionError, err: except PropertiesOptionError, err:
proc = err.proptype proc = err.proptype
assert proc == ['mandatory'] assert proc == ['mandatory']
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
setting.read_write() setting.read_write()
config.str = 'a' config.str = 'a'
setting.read_only() setting.read_only()
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']
def test_mandatory_warnings_rw(): def test_mandatory_warnings_rw():
@ -206,9 +229,9 @@ def test_mandatory_warnings_rw():
setting = config.cfgimpl_get_settings() setting = config.cfgimpl_get_settings()
setting.read_write() setting.read_write()
config.str config.str
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
config.str = 'a' config.str = 'a'
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']
def test_mandatory_warnings_disabled(): def test_mandatory_warnings_disabled():
@ -218,9 +241,9 @@ def test_mandatory_warnings_disabled():
setting = config.cfgimpl_get_settings() setting = config.cfgimpl_get_settings()
setting.read_write() setting.read_write()
config.str config.str
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
setting.add_property('disabled', descr.str) setting.add_property('disabled', descr.str)
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']
def test_mandatory_warnings_frozen(): def test_mandatory_warnings_frozen():
@ -230,7 +253,7 @@ def test_mandatory_warnings_frozen():
setting = config.cfgimpl_get_settings() setting = config.cfgimpl_get_settings()
setting.read_write() setting.read_write()
config.str config.str
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
setting.add_property('frozen', descr.str) setting.add_property('frozen', descr.str)
setting.read_only() setting.read_only()
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3'] assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']

View File

@ -79,6 +79,7 @@ class Option(BaseInformation):
__slots__ = ('_name', '_requires', '_multi', '_validator', '_default_multi', __slots__ = ('_name', '_requires', '_multi', '_validator', '_default_multi',
'_default', '_properties', '_callback', '_multitype', '_default', '_properties', '_callback', '_multitype',
'_master_slaves', '_consistencies', '_empty') '_master_slaves', '_consistencies', '_empty')
_empty = ''
def __init__(self, name, doc, default=None, default_multi=None, def __init__(self, name, doc, default=None, default_multi=None,
requires=None, multi=False, callback=None, requires=None, multi=False, callback=None,
@ -110,7 +111,6 @@ class Option(BaseInformation):
validate_requires_arg(requires, self._name) validate_requires_arg(requires, self._name)
self._requires = requires self._requires = requires
self._multi = multi self._multi = multi
self._empty = ''
self._consistencies = None self._consistencies = None
if validator is not None: if validator is not None:
if type(validator) != FunctionType: if type(validator) != FunctionType:
@ -345,6 +345,7 @@ class StrOption(Option):
class UnicodeOption(Option): class UnicodeOption(Option):
__slots__ = ('opt_type') __slots__ = ('opt_type')
opt_type = 'unicode' opt_type = 'unicode'
_empty = u''
def _validate(self, value): def _validate(self, value):
return isinstance(value, unicode) return isinstance(value, unicode)