summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_iniparser.py
blob: 1dcb9a0a307996b64ac3ca2bdd92be007a19049f (plain)
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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 OpenStack LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from openstack.common import iniparser
from tests import utils


class TestParser(iniparser.BaseParser):
    comment_called = False
    values = None
    section = ''

    def __init__(self):
        self.values = {}

    def assignment(self, key, value):
        self.values.setdefault(self.section, {})
        self.values[self.section][key] = value

    def new_section(self, section):
        self.section = section

    def comment(self, section):
        self.comment_called = True


class BaseParserTestCase(utils.BaseTestCase):
    def setUp(self):
        super(BaseParserTestCase, self).setUp()
        self.parser = iniparser.BaseParser()

    def _assertParseError(self, *lines):
        self.assertRaises(iniparser.ParseError, self.parser.parse, lines)

    def test_invalid_assignment(self):
        self._assertParseError("foo - bar")

    def test_empty_key(self):
        self._assertParseError(": bar")

    def test_unexpected_continuation(self):
        self._assertParseError("   baz")

    def test_invalid_section(self):
        self._assertParseError("[section")

    def test_no_section_name(self):
        self._assertParseError("[]")


class ParserTestCase(utils.BaseTestCase):
    def setUp(self):
        super(ParserTestCase, self).setUp()
        self.parser = TestParser()

    def test_blank_line(self):
        lines = [""]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {})

    def test_assignment_equal(self):
        lines = ["foo = bar"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': ['bar']}})

    def test_assignment_colon(self):
        lines = ["foo: bar"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': ['bar']}})

    def test_assignment_multiline(self):
        lines = ["foo = bar0", "  bar1"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': ['bar0', 'bar1']}})

    def test_assignment_multline_empty(self):
        lines = ["foo = bar0", "", "  bar1"]
        self.assertRaises(iniparser.ParseError, self.parser.parse, lines)

    def test_section_assignment(self):
        lines = ["[test]", "foo = bar"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'test': {'foo': ['bar']}})

    def test_new_section(self):
        lines = ["[foo]"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.section, 'foo')

    def test_comment(self):
        lines = ["# foobar"]
        self.parser.parse(lines)
        self.assertTrue(self.parser.comment_called)

    def test_empty_assignment(self):
        lines = ["foo = "]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': ['']}})

    def test_assignment_space_single_quote(self):
        lines = ["foo = ' bar '"]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': [' bar ']}})

    def test_assignment_space_double_quote(self):
        lines = ["foo = \" bar \""]
        self.parser.parse(lines)
        self.assertEquals(self.parser.values, {'': {'foo': [' bar ']}})


class ExceptionTestCase(utils.BaseTestCase):
    def test_parseerror(self):
        exc = iniparser.ParseError('test', 42, 'example')
        self.assertEquals(str(exc), "at line 42, test: 'example'")