1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
from __future__ import with_statement
import os
try:
import unittest2 as unittest
except ImportError:
import unittest
from configobj import ConfigObj
from StringIO import StringIO
try:
# Python 2.6 only
from warnings import catch_warnings
except ImportError:
# this will cause an error, but at least the other tests
# will run on Python 2.5
catch_warnings = None
class TestConfigObj(unittest.TestCase):
def test_order_preserved(self):
c = ConfigObj()
c['a'] = 1
c['b'] = 2
c['c'] = 3
c['section'] = {}
c['section']['a'] = 1
c['section']['b'] = 2
c['section']['c'] = 3
c['section']['section'] = {}
c['section']['section2'] = {}
c['section']['section3'] = {}
c['section2'] = {}
c['section3'] = {}
c2 = ConfigObj(c)
self.assertEqual(c2.scalars, ['a', 'b', 'c'])
self.assertEqual(c2.sections, ['section', 'section2', 'section3'])
self.assertEqual(c2['section'].scalars, ['a', 'b', 'c'])
self.assertEqual(c2['section'].sections, ['section', 'section2', 'section3'])
self.assertFalse(c['section'] is c2['section'])
self.assertFalse(c['section']['section'] is c2['section']['section'])
if catch_warnings is not None:
# poor man's skipTest
def test_options_deprecation(self):
with catch_warnings(record=True) as log:
ConfigObj(options={})
# unpack the only member of log
warning, = log
self.assertEqual(warning.category, DeprecationWarning)
def test_list_members(self):
c = ConfigObj()
c['a'] = []
c['a'].append('foo')
self.assertEqual(c['a'], ['foo'])
def test_list_interpolation_with_pop(self):
c = ConfigObj()
c['a'] = []
c['a'].append('%(b)s')
c['b'] = 'bar'
self.assertEqual(c.pop('a'), ['bar'])
def test_with_default(self):
c = ConfigObj()
c['a'] = 3
self.assertEqual(c.pop('a'), 3)
self.assertEqual(c.pop('b', 3), 3)
self.assertRaises(KeyError, c.pop, 'c')
def test_interpolation_with_section_names(self):
cfg = """
item1 = 1234
[section]
[[item1]]
foo='bar'
[[DEFAULT]]
[[[item1]]]
why = would you do this?
[[other-subsection]]
item2 = '$item1'""".splitlines()
c = ConfigObj(cfg, interpolation='Template')
# This raises an exception in 4.7.1 and earlier due to the section
# being found as the interpolation value
repr(c)
def test_interoplation_repr(self):
c = ConfigObj(['foo = $bar'], interpolation='Template')
c['baz'] = {}
c['baz']['spam'] = '%(bar)s'
# This raises a MissingInterpolationOption exception in 4.7.1 and earlier
repr(c)
def test_triple_quotes(self):
triple_quotes_value = '''spam
""" that's my spam """
eggs'''
co = ConfigObj()
co['test'] = triple_quotes_value
outfile = StringIO()
co.write(outfile=outfile)
lines = outfile.getvalue().splitlines()
# now we're trying to read it back
co2 = ConfigObj(lines)
self.assertEquals(triple_quotes_value, co2['test'])
def test_as_functions(self):
a = ConfigObj()
a['a'] = 'fish'
self.assertRaises(ValueError, a.as_bool, 'a')
a['b'] = 'True'
self.assertEquals(a.as_bool('b'),True)
a['b'] = 'off'
self.assertEquals(a.as_bool('b'),False)
a['a'] = 'fish'
self.assertRaises(ValueError, a.as_int, 'a')
a['b'] = '1'
self.assertEquals(a.as_int('b'),1)
a['b'] = '3.2'
self.assertRaises(ValueError,a.as_int,'b')
a['a'] = 'fish'
self.assertRaises(ValueError,a.as_float, 'a')
a['b'] = '1'
self.assertEqual(a.as_float('b'),1.0)
a['b'] = '3.2'
self.assertEqual(a.as_float('b'),3.2)
|