1
from __future__ import with_statement
5
import unittest2 as unittest
9
from configobj import ConfigObj
13
from warnings import catch_warnings
15
# this will cause an error, but at least the other tests
16
# will run on Python 2.5
19
class TestConfigObj(unittest.TestCase):
21
def test_order_preserved(self):
30
c['section']['section'] = {}
31
c['section']['section2'] = {}
32
c['section']['section3'] = {}
37
self.assertEqual(c2.scalars, ['a', 'b', 'c'])
38
self.assertEqual(c2.sections, ['section', 'section2', 'section3'])
39
self.assertEqual(c2['section'].scalars, ['a', 'b', 'c'])
40
self.assertEqual(c2['section'].sections, ['section', 'section2', 'section3'])
42
self.assertFalse(c['section'] is c2['section'])
43
self.assertFalse(c['section']['section'] is c2['section']['section'])
45
if catch_warnings is not None:
47
def test_options_deprecation(self):
48
with catch_warnings(record=True) as log:
51
# unpack the only member of log
53
self.assertEqual(warning.category, DeprecationWarning)
55
def test_list_members(self):
59
self.assertEqual(c['a'], ['foo'])
61
def test_list_interpolation_with_pop(self):
64
c['a'].append('%(b)s')
66
self.assertEqual(c.pop('a'), ['bar'])
68
def test_with_default(self):
72
self.assertEqual(c.pop('a'), 3)
73
self.assertEqual(c.pop('b', 3), 3)
74
self.assertRaises(KeyError, c.pop, 'c')
77
def test_interpolation_with_section_names(self):
85
why = would you do this?
87
item2 = '$item1'""".splitlines()
88
c = ConfigObj(cfg, interpolation='Template')
90
# This raises an exception in 4.7.1 and earlier due to the section
91
# being found as the interpolation value
94
def test_interoplation_repr(self):
95
c = ConfigObj(['foo = $bar'], interpolation='Template')
97
c['baz']['spam'] = '%(bar)s'
99
# This raises a MissingInterpolationOption exception in 4.7.1 and earlier
102
def test_as_functions(self):
105
self.assertRaises(ValueError, a.as_bool, 'a')
107
self.assertEquals(a.as_bool('b'),True)
109
self.assertEquals(a.as_bool('b'),False)
111
self.assertRaises(ValueError, a.as_int, 'a')
113
self.assertEquals(a.as_int('b'),1)
115
self.assertRaises(ValueError,a.as_int,'b')
117
self.assertRaises(ValueError,a.as_float, 'a')
119
self.assertEqual(a.as_float('b'),1.0)
121
self.assertEqual(a.as_float('b'),3.2)