~ubuntu-branches/debian/stretch/configobj/stretch

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Benjamin Drung
  • Date: 2014-08-28 18:42:29 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20140828184229-yisw15o0j2r334jx
Tags: 5.0.6-1
* Team upload.
* Update watch file to point to GitHub releases.
* Bump Standards-Version to 3.9.5
* Use the pybuild buildsystem.
* Release a package for Python 3. (Closes: #660172)
* Refresh eggify.diff
* Drop report-doctest-failure.diff and py27-test.diff (fixed upstream).
* Disable triplequotes.diff (fail to apply).
* Fix deprecation warning test case.
* Adjust autopkgtests to run pytest for Python 2.x and 3.x.
* Move documentation into separate package and build Sphinx documentation.
* Update debian/copyright

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)