diff options
Diffstat (limited to 'test_configobj.py')
-rw-r--r-- | test_configobj.py | 386 |
1 files changed, 0 insertions, 386 deletions
diff --git a/test_configobj.py b/test_configobj.py index 47d1959..b8e68a2 100644 --- a/test_configobj.py +++ b/test_configobj.py @@ -295,392 +295,6 @@ def _test_reload(): """ -def _doctest(): - """ - Dummy function to hold some of the doctests. - - >>> a.depth - 0 - >>> a == { - ... 'key2': 'val', - ... 'key1': 'val', - ... 'lev1c': { - ... 'lev2c': { - ... 'lev3c': { - ... 'key1': 'val', - ... }, - ... }, - ... }, - ... 'lev1b': { - ... 'key2': 'val', - ... 'key1': 'val', - ... 'lev2ba': { - ... 'key1': 'val', - ... }, - ... 'lev2bb': { - ... 'key1': 'val', - ... }, - ... }, - ... 'lev1a': { - ... 'key2': 'val', - ... 'key1': 'val', - ... }, - ... } - 1 - >>> b.depth - 0 - >>> b == { - ... 'key3': 'val3', - ... 'key2': 'val2', - ... 'key1': 'val1', - ... 'section 1': { - ... 'keys11': 'val1', - ... 'keys13': 'val3', - ... 'keys12': 'val2', - ... }, - ... 'section 2': { - ... 'section 2 sub 1': { - ... 'fish': '3', - ... }, - ... 'keys21': 'val1', - ... 'keys22': 'val2', - ... 'keys23': 'val3', - ... }, - ... } - 1 - >>> t = ''' - ... 'a' = b # !"$%^&*(),::;'@~#= 33 - ... "b" = b #= 6, 33 - ... ''' .split('\\n') - >>> t2 = ConfigObj(t) - >>> assert t2 == {'a': 'b', 'b': 'b'} - >>> t2.inline_comments['b'] = '' - >>> del t2['a'] - >>> assert t2.write() == ['','b = b', ''] - - # Test ``list_values=False`` stuff - >>> c = ''' - ... key1 = no quotes - ... key2 = 'single quotes' - ... key3 = "double quotes" - ... key4 = "list", 'with', several, "quotes" - ... ''' - >>> cfg = ConfigObj(c.splitlines(), list_values=False) - >>> cfg == {'key1': 'no quotes', 'key2': "'single quotes'", - ... 'key3': '"double quotes"', - ... 'key4': '"list", \\'with\\', several, "quotes"' - ... } - 1 - >>> cfg = ConfigObj(list_values=False) - >>> cfg['key1'] = 'Multiline\\nValue' - >>> cfg['key2'] = '''"Value" with 'quotes' !''' - >>> cfg.write() - ["key1 = '''Multiline\\nValue'''", 'key2 = "Value" with \\'quotes\\' !'] - >>> cfg.list_values = True - >>> cfg.write() == ["key1 = '''Multiline\\nValue'''", - ... 'key2 = \\'\\'\\'"Value" with \\'quotes\\' !\\'\\'\\''] - 1 - - Test flatten_errors: - - >>> config = ''' - ... test1=40 - ... test2=hello - ... test3=3 - ... test4=5.0 - ... [section] - ... test1=40 - ... test2=hello - ... test3=3 - ... test4=5.0 - ... [[sub section]] - ... test1=40 - ... test2=hello - ... test3=3 - ... test4=5.0 - ... '''.split('\\n') - >>> configspec = ''' - ... test1= integer(30,50) - ... test2= string - ... test3=integer - ... test4=float(6.0) - ... [section] - ... test1=integer(30,50) - ... test2=string - ... test3=integer - ... test4=float(6.0) - ... [[sub section]] - ... test1=integer(30,50) - ... test2=string - ... test3=integer - ... test4=float(6.0) - ... '''.split('\\n') - >>> val = Validator() - >>> c1 = ConfigObj(config, configspec=configspec) - >>> res = c1.validate(val) - >>> flatten_errors(c1, res) == [([], 'test4', False), (['section'], 'test4', False), (['section', 'sub section'], 'test4', False)] - True - >>> res = c1.validate(val, preserve_errors=True) - >>> check = flatten_errors(c1, res) - >>> check[0][:2] - ([], 'test4') - >>> check[1][:2] - (['section'], 'test4') - >>> check[2][:2] - (['section', 'sub section'], 'test4') - >>> for entry in check: - ... isinstance(entry[2], VdtValueTooSmallError) - ... print(str(entry[2])) - True - the value "5.0" is too small. - True - the value "5.0" is too small. - True - the value "5.0" is too small. - - Test unicode handling, BOM, write witha file like object and line endings : - >>> u_base = ''' - ... # initial comment - ... # inital comment 2 - ... - ... test1 = some value - ... # comment - ... test2 = another value # inline comment - ... # section comment - ... [section] # inline comment - ... test = test # another inline comment - ... test2 = test2 - ... - ... # final comment - ... # final comment2 - ... ''' - >>> u = u_base.encode('utf_8').splitlines(True) - >>> u[0] = BOM_UTF8 + u[0] - >>> uc = ConfigObj(u) - >>> uc.encoding = None - >>> uc.BOM == True - 1 - >>> uc == {'test1': 'some value', 'test2': 'another value', - ... 'section': {'test': 'test', 'test2': 'test2'}} - 1 - >>> uc = ConfigObj(u, encoding='utf_8', default_encoding='latin-1') - >>> uc.BOM - 1 - >>> isinstance(uc['test1'], str) - 1 - >>> uc.encoding - 'utf_8' - >>> uc.newlines - '\\n' - >>> uc['latin1'] = "This costs lot's of " - >>> a_list = uc.write() - >>> len(a_list) - 15 - >>> isinstance(a_list[0], bytes) - 1 - >>> a_list[0].startswith(BOM_UTF8) - 1 - >>> u = u_base.replace('\\n', '\\r\\n').encode('utf_8').splitlines(True) - >>> uc = ConfigObj(u) - >>> uc.newlines - '\\r\\n' - >>> uc.newlines = '\\r' - >>> file_like = StringIO() - >>> uc.write(file_like) - >>> _ = file_like.seek(0) - >>> uc2 = ConfigObj(file_like) - >>> uc2 == uc - 1 - >>> uc2.filename == None - 1 - >>> uc2.newlines == '\\r' - 1 - - Test validate in copy mode - >>> a = ''' - ... # Initial Comment - ... - ... key1 = string(default=Hello) - ... - ... # section comment - ... [section] # inline comment - ... # key1 comment - ... key1 = integer(default=6) - ... # key2 comment - ... key2 = boolean(default=True) - ... - ... # subsection comment - ... [[sub-section]] # inline comment - ... # another key1 comment - ... key1 = float(default=3.0)'''.splitlines() - >>> b = ConfigObj(configspec=a) - >>> b.validate(val, copy=True) - 1 - >>> b.write() == ['', - ... '# Initial Comment', - ... '', - ... 'key1 = Hello', - ... '', - ... '# section comment', - ... '[section] # inline comment', - ... ' # key1 comment', - ... ' key1 = 6', - ... ' # key2 comment', - ... ' key2 = True', - ... ' ', - ... ' # subsection comment', - ... ' [[sub-section]] # inline comment', - ... ' # another key1 comment', - ... ' key1 = 3.0'] - 1 - - Test Writing Empty Values - >>> a = ''' - ... key1 = - ... key2 =# a comment''' - >>> b = ConfigObj(a.splitlines()) - >>> b.write() - ['', 'key1 = ""', 'key2 = "" # a comment'] - >>> b.write_empty_values = True - >>> b.write() - ['', 'key1 = ', 'key2 = # a comment'] - - Test unrepr when reading - >>> a = ''' - ... key1 = (1, 2, 3) # comment - ... key2 = True - ... key3 = 'a string' - ... key4 = [1, 2, 3, 'a mixed list'] - ... '''.splitlines() - >>> b = ConfigObj(a, unrepr=True) - >>> b == {'key1': (1, 2, 3), - ... 'key2': True, - ... 'key3': 'a string', - ... 'key4': [1, 2, 3, 'a mixed list']} - 1 - - Test unrepr when writing - >>> c = ConfigObj(b.write(), unrepr=True) - >>> c == b - 1 - - Test unrepr with multiline values - >>> a = '''k = \"""{ - ... 'k1': 3, - ... 'k2': 6.0}\""" - ... '''.splitlines() - >>> c = ConfigObj(a, unrepr=True) - >>> c == {'k': {'k1': 3, 'k2': 6.0}} - 1 - - Test unrepr with a dictionary - >>> a = 'k = {"a": 1}'.splitlines() - >>> c = ConfigObj(a, unrepr=True) - >>> isinstance(c['k'], dict) - True - - >>> a = ConfigObj() - >>> a['a'] = 'fish' - >>> a.as_bool('a') - Traceback (most recent call last): - ValueError: Value "fish" is neither True nor False - >>> a['b'] = 'True' - >>> a.as_bool('b') - 1 - >>> a['b'] = 'off' - >>> a.as_bool('b') - 0 - - >>> a = ConfigObj() - >>> a['a'] = 'fish' - >>> try: - ... a.as_int('a') - ... except ValueError as e: - ... err_mess = str(e) - >>> err_mess.startswith('invalid literal for int()') - 1 - >>> a['b'] = '1' - >>> a.as_int('b') - 1 - >>> a['b'] = '3.2' - >>> try: - ... a.as_int('b') - ... except ValueError as e: - ... err_mess = str(e) - >>> err_mess.startswith('invalid literal for int()') - 1 - - >>> a = ConfigObj() - >>> a['a'] = 'fish' - >>> a.as_float('a') - Traceback (most recent call last): - ValueError: invalid literal for float(): fish - >>> a['b'] = '1' - >>> a.as_float('b') - 1.0 - >>> a['b'] = '3.2' - >>> a.as_float('b') - 3.2... - - Test # with unrepr - >>> a = ''' - ... key1 = (1, 2, 3) # comment - ... key2 = True - ... key3 = 'a string' - ... key4 = [1, 2, 3, 'a mixed list#'] - ... '''.splitlines() - >>> b = ConfigObj(a, unrepr=True) - >>> b == {'key1': (1, 2, 3), - ... 'key2': True, - ... 'key3': 'a string', - ... 'key4': [1, 2, 3, 'a mixed list#']} - 1 - """ - - # Comments are no longer parsed from values in configspecs - # so the following test fails and is disabled - untested = """ - Test validate in copy mode - >>> a = ''' - ... # Initial Comment - ... - ... key1 = string(default=Hello) # comment 1 - ... - ... # section comment - ... [section] # inline comment - ... # key1 comment - ... key1 = integer(default=6) # an integer value - ... # key2 comment - ... key2 = boolean(default=True) # a boolean - ... - ... # subsection comment - ... [[sub-section]] # inline comment - ... # another key1 comment - ... key1 = float(default=3.0) # a float'''.splitlines() - >>> b = ConfigObj(configspec=a) - >>> b.validate(val, copy=True) - 1 - >>> b.write() - >>> b.write() == ['', - ... '# Initial Comment', - ... '', - ... 'key1 = Hello # comment 1', - ... '', - ... '# section comment', - ... '[section] # inline comment', - ... ' # key1 comment', - ... ' key1 = 6 # an integer value', - ... ' # key2 comment', - ... ' key2 = True # a boolean', - ... ' ', - ... ' # subsection comment', - ... ' [[sub-section]] # inline comment', - ... ' # another key1 comment', - ... ' key1 = 3.0 # a float'] - 1 - """ - - def _test_configobj(): """ Testing ConfigObj |