diff options
author | Rob Dennis <rdennis@gmail.com> | 2014-03-04 08:24:45 -0500 |
---|---|---|
committer | Rob Dennis <rdennis@gmail.com> | 2014-03-04 08:24:45 -0500 |
commit | 4644d06021d82cf5f1922c660cd65301b1a1922c (patch) | |
tree | 89cf49064bc2c830e513855f9c2b88c54601ee64 | |
parent | 4dfffeb28da21a283adee01bb561b039c52ad27b (diff) | |
parent | 562185680eb673df7f0057976defda3206646d78 (diff) | |
download | configobj-git-4644d06021d82cf5f1922c660cd65301b1a1922c.tar.gz |
Merge pull request #38 from robdennis/master
#14 - moved over doctests for a bunch of random value checks
-rw-r--r-- | test_configobj.py | 125 | ||||
-rw-r--r-- | tests/test_configobj.py | 120 |
2 files changed, 120 insertions, 125 deletions
diff --git a/test_configobj.py b/test_configobj.py index 9c1eb2e..bb75cb6 100644 --- a/test_configobj.py +++ b/test_configobj.py @@ -44,130 +44,7 @@ from validate import Validator, VdtValueTooSmallError def _test_configobj(): """ Testing ConfigObj - Testing Empty values. - >>> cfg_with_empty = ''' - ... k = - ... k2 =# comment test - ... val = test - ... val2 = , - ... val3 = 1, - ... val4 = 1, 2 - ... val5 = 1, 2, '''.splitlines() - >>> cwe = ConfigObj(cfg_with_empty) - >>> cwe == {'k': '', 'k2': '', 'val': 'test', 'val2': [], - ... 'val3': ['1'], 'val4': ['1', '2'], 'val5': ['1', '2']} - 1 - >>> cwe = ConfigObj(cfg_with_empty, list_values=False) - >>> cwe == {'k': '', 'k2': '', 'val': 'test', 'val2': ',', - ... 'val3': '1,', 'val4': '1, 2', 'val5': '1, 2,'} - 1 - - Testing list values. - >>> testconfig3 = ''' - ... a = , - ... b = test, - ... c = test1, test2 , test3 - ... d = test1, test2, test3, - ... ''' - >>> d = ConfigObj(testconfig3.split('\\n'), raise_errors=True) - >>> d['a'] == [] - 1 - >>> d['b'] == ['test'] - 1 - >>> d['c'] == ['test1', 'test2', 'test3'] - 1 - >>> d['d'] == ['test1', 'test2', 'test3'] - 1 - - Testing with list values off. - - >>> e = ConfigObj( - ... testconfig3.split('\\n'), - ... raise_errors=True, - ... list_values=False) - >>> e['a'] == ',' - 1 - >>> e['b'] == 'test,' - 1 - >>> e['c'] == 'test1, test2 , test3' - 1 - >>> e['d'] == 'test1, test2, test3,' - 1 - - Testing creating from a dictionary. - - >>> f = { - ... 'key1': 'val1', - ... 'key2': 'val2', - ... 'section 1': { - ... 'key1': 'val1', - ... 'key2': 'val2', - ... 'section 1b': { - ... 'key1': 'val1', - ... 'key2': 'val2', - ... }, - ... }, - ... 'section 2': { - ... 'key1': 'val1', - ... 'key2': 'val2', - ... 'section 2b': { - ... 'key1': 'val1', - ... 'key2': 'val2', - ... }, - ... }, - ... 'key3': 'val3', - ... } - >>> g = ConfigObj(f) - >>> f == g - 1 - - Testing we correctly detect badly built list values (4 of them). - - >>> testconfig4 = ''' - ... config = 3,4,, - ... test = 3,,4 - ... fish = ,, - ... dummy = ,,hello, goodbye - ... ''' - >>> try: - ... ConfigObj(testconfig4.split('\\n')) - ... except ConfigObjError as e: - ... len(e.errors) - 4 - - Testing we correctly detect badly quoted values (4 of them). - - >>> testconfig5 = ''' - ... config = "hello # comment - ... test = 'goodbye - ... fish = 'goodbye # comment - ... dummy = "hello again - ... ''' - >>> try: - ... ConfigObj(testconfig5.split('\\n')) - ... except ConfigObjError as e: - ... len(e.errors) - 4 - - Test Multiline Comments - >>> i == { - ... 'name4': ' another single line value ', - ... 'multi section': { - ... 'name4': '\\n Well, this is a\\n multiline ' - ... 'value\\n ', - ... 'name2': '\\n Well, this is a\\n multiline ' - ... 'value\\n ', - ... 'name3': '\\n Well, this is a\\n multiline ' - ... 'value\\n ', - ... 'name1': '\\n Well, this is a\\n multiline ' - ... 'value\\n ', - ... }, - ... 'name2': ' another single line value ', - ... 'name3': ' a single line value ', - ... 'name1': ' a single line value ', - ... } - 1 - + >>> filename = a.filename >>> a.filename = None >>> values = a.write() diff --git a/tests/test_configobj.py b/tests/test_configobj.py index 7c36f87..ac976a9 100644 --- a/tests/test_configobj.py +++ b/tests/test_configobj.py @@ -903,6 +903,18 @@ class TestQuotes(object): def test_handle_unallowed_open_quote(self, i): open_quote = ' "\' ' self.assert_bad_quote_message(i, open_quote, multiline=False) + + def test_handle_multiple_bad_quote_values(self): + testconfig5 = ''' + config = "hello # comment + test = 'goodbye + fish = 'goodbye # comment + dummy = "hello again + ''' + with pytest.raises(ConfigObjError) as excinfo: + ConfigObj(testconfig5.splitlines()) + assert len(excinfo.value.errors) == 4 + def test_handle_stringify_off(): @@ -911,4 +923,110 @@ def test_handle_stringify_off(): with pytest.raises(TypeError) as excinfo: c['test'] = 1 - assert str(excinfo.value) == 'Value is not a string "1".'
\ No newline at end of file + assert str(excinfo.value) == 'Value is not a string "1".' + + +class TestValues(object): + """ + Tests specifics about behaviors with types of values + """ + @pytest.fixture + def testconfig3(self): + return ''' + a = , + b = test, + c = test1, test2 , test3 + d = test1, test2, test3, + '''.splitlines() + + def test_empty_values(self): + cfg_with_empty = ''' + k = + k2 =# comment test + val = test + val2 = , + val3 = 1, + val4 = 1, 2 + val5 = 1, 2, '''.splitlines() + cwe = ConfigObj(cfg_with_empty) + # see a comma? it's a list + assert cwe == {'k': '', 'k2': '', 'val': 'test', 'val2': [], + 'val3': ['1'], 'val4': ['1', '2'], 'val5': ['1', '2']} + # not any more + cwe = ConfigObj(cfg_with_empty, list_values=False) + assert cwe == {'k': '', 'k2': '', 'val': 'test', 'val2': ',', + 'val3': '1,', 'val4': '1, 2', 'val5': '1, 2,'} + + def test_list_values(self, testconfig3): + cfg = ConfigObj(testconfig3, raise_errors=True) + assert cfg['a'] == [] + assert cfg['b'] == ['test'] + assert cfg['c'] == ['test1', 'test2', 'test3'] + assert cfg['d'] == ['test1', 'test2', 'test3'] + + def test_list_values_off(self, testconfig3): + cfg = ConfigObj(testconfig3, raise_errors=True, list_values=False) + assert cfg['a'] == ',' + assert cfg['b'] == 'test,' + assert cfg['c'] == 'test1, test2 , test3' + assert cfg['d'] == 'test1, test2, test3,' + + def test_handle_multiple_list_value_errors(self): + testconfig4 = ''' + config = 3,4,, + test = 3,,4 + fish = ,, + dummy = ,,hello, goodbye + ''' + with pytest.raises(ConfigObjError) as excinfo: + ConfigObj(testconfig4.splitlines()) + assert len(excinfo.value.errors) == 4 + + + +def test_creating_with_a_dictionary(): + dictionary_cfg_content = { + 'key1': 'val1', + 'key2': 'val2', + 'section 1': { + 'key1': 'val1', + 'key2': 'val2', + 'section 1b': { + 'key1': 'val1', + 'key2': 'val2', + }, + }, + 'section 2': { + 'key1': 'val1', + 'key2': 'val2', + 'section 2b': { + 'key1': 'val1', + 'key2': 'val2', + }, + }, + 'key3': 'val3', + } + cfg = ConfigObj(dictionary_cfg_content) + assert dictionary_cfg_content == cfg + assert dictionary_cfg_content is not cfg + assert dictionary_cfg_content == cfg.dict() + assert dictionary_cfg_content is not cfg.dict() + + +def test_multiline_comments(i): + assert i == { + 'name4': ' another single line value ', + 'multi section': { + 'name4': '\n Well, this is a\n multiline ' + 'value\n ', + 'name2': '\n Well, this is a\n multiline ' + 'value\n ', + 'name3': '\n Well, this is a\n multiline ' + 'value\n ', + 'name1': '\n Well, this is a\n multiline ' + 'value\n ', + }, + 'name2': ' another single line value ', + 'name3': ' a single line value ', + 'name1': ' a single line value ', + }
\ No newline at end of file |