~ubuntu-branches/ubuntu/natty/configobj/natty

« back to all changes in this revision

Viewing changes to .pc/triplequotes.diff/tests/functionaltests/test_configobj.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij, Stefano Rivera, Jelmer Vernooij
  • Date: 2011-03-16 11:17:14 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20110316111714-p0199jx0lfyd5az9
[ Stefano Rivera ]
* Don't leak uid and umask into source tarball and set -e.
* Bumped Standards-Version to 3.9.1 (no changes needed).
* Enable test suites.
  - Build Depend on python-all, python-unittest2.
  - New patch: report-doctest-failure.diff: Fail on failures.
  - New patch: py27-test.diff: Convert float-comparing doctests to unit
    tests.
* Wrap long lines in debian/control.
* Merge Build-Depends-Indep into Build-Depends (no arch-dependant packages).
* Switch to dh_python2
  - Use X-Python-Version (requires python-all >= 2.6.5-13~).
  - Use ${python:Breaks}.
* Update my e-mail address.
* Use DEP5 format in debian/copyright.

[ Jelmer Vernooij ]
* Properly handle triple quotes. Closes: #618349, LP: #710410
* Add myself to uploaders.

Show diffs side-by-side

added added

removed removed

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