diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-03-22 18:53:43 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-03-22 18:53:43 +0000 |
| commit | 1c30f39901be7bdaf94946d1a9b470beb366bcba (patch) | |
| tree | 074f50cf5971815c891f5099eab8bafcd4a171ae /tests | |
| parent | 88a4f947307a129d748a482c1d628384f31f1fd8 (diff) | |
| parent | f135fd041c6dee1aabea2f759d004627638862e2 (diff) | |
Merge "Finish implementing MultiStrOpt"
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/test_cfg.py | 23 | ||||
| -rw-r--r-- | tests/unit/test_iniparser.py | 111 |
2 files changed, 127 insertions, 7 deletions
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index 9e1e0e8..4e8ec21 100644 --- a/tests/unit/test_cfg.py +++ b/tests/unit/test_cfg.py @@ -445,24 +445,23 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertEquals(self.conf.foo, ['bar']) def test_conf_file_multistr_values_append(self): - self.conf.register_cli_opt(ListOpt('foo')) + self.conf.register_cli_opt(MultiStrOpt('foo')) paths = self.create_tempfiles([('1.conf', '[DEFAULT]\n' - 'foo = bar\n'), + 'foo = bar1\n'), ('2.conf', '[DEFAULT]\n' - 'foo = bar\n')]) + 'foo = bar2\n' + 'foo = bar3\n')]) - self.conf(['--foo', 'bar', + self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) - # FIXME(markmc): values spread across the CLI and multiple - # config files should be appended - # self.assertEquals(self.conf.foo, ['bar', 'bar', 'bar']) + self.assertEquals(self.conf.foo, ['bar0', 'bar1', 'bar2', 'bar3']) def test_conf_file_multiple_opts(self): self.conf.register_opts([StrOpt('foo'), StrOpt('bar')]) @@ -989,3 +988,13 @@ class CommonOptsTestCase(BaseTestCase): CommonConfigOpts.DEFAULT_LOG_DATE_FORMAT) self.assertEquals(self.conf.use_syslog, False) + + +class ConfigParserTestCase(unittest.TestCase): + def test_no_section(self): + with tempfile.NamedTemporaryFile() as tmpfile: + tmpfile.write('foo = bar') + tmpfile.flush() + + parser = ConfigParser(tmpfile.name, {}) + self.assertRaises(ParseError, parser.parse) diff --git a/tests/unit/test_iniparser.py b/tests/unit/test_iniparser.py new file mode 100644 index 0000000..5b3a0de --- /dev/null +++ b/tests/unit/test_iniparser.py @@ -0,0 +1,111 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +from openstack.common import iniparser + + +class TestParser(iniparser.BaseParser): + comment_called = False + values = None + section = '' + + def __init__(self): + self.values = {} + + def assignment(self, key, value): + self.values.setdefault(self.section, {}) + self.values[self.section][key] = value + + def new_section(self, section): + self.section = section + + def comment(self, section): + self.comment_called = True + + +class BaseParserTestCase(unittest.TestCase): + def setUp(self): + self.parser = iniparser.BaseParser() + + def _assertParseError(self, *lines): + self.assertRaises(iniparser.ParseError, self.parser.parse, lines) + + def test_invalid_assignment(self): + self._assertParseError("foo - bar") + + def test_empty_key(self): + self._assertParseError(": bar") + + def test_unexpected_continuation(self): + self._assertParseError(" baz") + + def test_invalid_section(self): + self._assertParseError("[section") + + def test_no_section_name(self): + self._assertParseError("[]") + + +class ParserTestCase(unittest.TestCase): + def setUp(self): + self.parser = TestParser() + + def test_blank_line(self): + lines = [""] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {}) + + def test_assignment_equal(self): + lines = ["foo = bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar']}}) + + def test_assignment_colon(self): + lines = ["foo: bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar']}}) + + def test_assignment_multiline(self): + lines = ["foo = bar0", " bar1"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar0', 'bar1']}}) + + def test_assignment_multline_empty(self): + lines = ["foo = bar0", "", " bar1"] + self.assertRaises(iniparser.ParseError, self.parser.parse, lines) + + def test_section_assignment(self): + lines = ["[test]", "foo = bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'test': {'foo': ['bar']}}) + + def test_new_section(self): + lines = ["[foo]"] + self.parser.parse(lines) + self.assertEquals(self.parser.section, 'foo') + + def test_comment(self): + lines = ["# foobar"] + self.parser.parse(lines) + self.assertTrue(self.parser.comment_called) + + +class ExceptionTestCase(unittest.TestCase): + def test_parseerror(self): + exc = iniparser.ParseError('test', 42, 'example') + self.assertEquals(str(exc), "at line 42, test: 'example'") |
