summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2009-07-24 15:11:13 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2009-07-24 15:11:13 +0200
commit4885a6cfa051b58bcf9ecefd0652121c84725a7d (patch)
tree047c668170bf27d3b96e8dfe5fe2dec0736ca449
parent3965875da48a2960e3f0d0f754f13f3bdeb6771f (diff)
downloadlogilab-common-4885a6cfa051b58bcf9ecefd0652121c84725a7d.tar.gz
fix config.__setitem__, should take an option (not attribute) name as key
-rw-r--r--configuration.py2
-rw-r--r--test/unittest_configuration.py56
2 files changed, 32 insertions, 26 deletions
diff --git a/configuration.py b/configuration.py
index d29d591..f6dec1b 100644
--- a/configuration.py
+++ b/configuration.py
@@ -830,7 +830,7 @@ class ConfigurationMixIn(OptionsManagerMixIn, OptionsProviderMixIn):
raise KeyError(key)
def __setitem__(self, key, value):
- self.set_option(self.option_name(key), value)
+ self.set_option(key, value)
def get(self, key, default=None):
try:
diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py
index 0cc2494..c68f0d3 100644
--- a/test/unittest_configuration.py
+++ b/test/unittest_configuration.py
@@ -23,16 +23,16 @@ options = [('dothis', {'type':'yn', 'action': 'store', 'default': True, 'metavar
('diffgroup', {'type':'string', 'default':'pouet', 'metavar': '<key=val>',
'group': 'agroup'}),
-
+
]
class MyConfiguration(Configuration):
"""test configuration"""
def get_named(self):
return {'key': 'val'}
-
+
class ConfigurationTC(TestCase):
-
+
def setUp(self):
self.cfg = MyConfiguration(name='test', options=options, usage='Just do it ! (tm)')
@@ -76,7 +76,7 @@ class ConfigurationTC(TestCase):
self.assertEquals(cfg['multiple'], ['1', '2', '3'])
self.assertEquals(cfg['number'], 4)
self.assertEquals(cfg['choice'], 'ye')
-
+
def test_load_configuration(self):
cfg = self.cfg
args = cfg.load_configuration(choice='ye', number='4',
@@ -125,7 +125,7 @@ diffgroup=zou
self.assertEquals(self.cfg['diffgroup'], 'zou')
finally:
os.remove(file)
-
+
def test_generate_config(self):
stream = StringIO()
self.cfg.generate_config(stream)
@@ -153,7 +153,7 @@ named=key:val
diffgroup=pouet
""")
-
+
def test_generate_config_with_space_string(self):
self.cfg['value'] = ' '
stream = StringIO()
@@ -182,7 +182,7 @@ named=key:val
diffgroup=pouet
""")
-
+
def test_loopback(self):
cfg = self.cfg
@@ -201,7 +201,13 @@ diffgroup=pouet
self.assertEquals(cfg['multiple-choice'], new_cfg['multiple-choice'])
finally:
os.remove(f)
-
+
+ def test_setitem(self):
+ self.assertRaises(OptionValueError,
+ self.cfg.__setitem__, 'multiple-choice', ('a', 'b'))
+ self.cfg['multiple-choice'] = ('yi', 'ya')
+ self.assertEquals(self.cfg['multiple-choice'], ('yi', 'ya'))
+
def test_help(self):
self.cfg.add_help_section('bonus', 'a nice additional help')
help = self.cfg.help().strip()
@@ -215,55 +221,55 @@ diffgroup=pouet
Options:
-h, --help show this help message and exit
- --dothis=<y or n>
+ --dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option [current: yop,yep]
--number=<int> boom [current: 2]
- --choice=<yo|ye>
+ --choice=<yo|ye>
--multiple-choice=<yo|ye>
- --named=<key=val>
+ --named=<key=val>
Agroup:
--diffgroup=<key=val>
Bonus:
a nice additional help
-""".strip())
+""", striplines=True)
elif version_info >= (2, 4):
self.assertLinesEquals(help, """usage: Just do it ! (tm)
options:
-h, --help show this help message and exit
- --dothis=<y or n>
+ --dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option [current: yop,yep]
--number=<int> boom [current: 2]
- --choice=<yo|ye>
+ --choice=<yo|ye>
--multiple-choice=<yo|ye>
- --named=<key=val>
+ --named=<key=val>
Bonus:
a nice additional help
-""".strip())
+""", striplines=True)
else:
self.assertLinesEquals(help, """usage: Just do it ! (tm)
options:
-h, --help show this help message and exit
- --dothis=<y or n>
+ --dothis=<y or n>
-v<string>, --value=<string>
--multiple=<comma separated values>
you can also document the option
- --number=<int>
- --choice=<yo|ye>
+ --number=<int>
+ --choice=<yo|ye>
--multiple-choice=<yo|ye>
- --named=<key=val>
+ --named=<key=val>
Bonus:
a nice additional help
-""".strip())
+""", striplines=True)
def test_manpage(self):
@@ -301,7 +307,7 @@ named=key:val
diffgroup=pouet
""")
-
+
class Linter(OptionsManagerMixIn, OptionsProviderMixIn):
options = (
('profile', {'type' : 'yn', 'metavar' : '<y_or_n>',
@@ -318,11 +324,11 @@ class RegrTC(TestCase):
def setUp(self):
self.linter = Linter()
-
+
def test_load_defaults(self):
self.linter.load_command_line_configuration([])
self.assertEquals(self.linter.config.profile, False)
-
-
+
+
if __name__ == '__main__':
unittest_main()