Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

coalib.tests.parsing.ConfParserTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %
Metric Value
dl 0
loc 143
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A coalib.tests.parsing.CliParserTest.dict_from_sections() 0 8 3
B coalib.tests.parsing.CliParserTest.test_parse_cli() 0 29 1
1
from collections import OrderedDict
2
import os
3
import sys
4
import tempfile
5
import unittest
6
7
sys.path.insert(0, ".")
8
from coalib.parsing.ConfParser import ConfParser
9
from coalib.settings.Section import Section
10
11
12
class ConfParserTest(unittest.TestCase):
13
    example_file = """to be ignored
14
    a_default, another = val
15
    TEST = tobeignored  # do you know that thats a comment
16
    test = push
17
    t =
18
    [MakeFiles]
19
     j  , another = a
20
                   multiline
21
                   value
22
    # just a omment
23
    # just a omment
24
    nokey. = value
25
    default.test = content
26
    makefiles.lastone = val
27
28
    [EMPTY_ELEM_STRIP]
29
    A = a, b, c
30
    B = a, ,, d
31
    C = ,,,
32
    """
33
34
    def setUp(self):
35
        self.tempdir = tempfile.gettempdir()
36
        self.file = os.path.join(self.tempdir, ".coafile")
37
        self.nonexistentfile = os.path.join(self.tempdir, "e81k7bd98t")
38
        with open(self.file, "w") as filehandler:
39
            filehandler.write(self.example_file)
40
41
        self.uut = ConfParser()
42
        try:
43
            os.remove(self.nonexistentfile)
44
        except FileNotFoundError:
45
            pass
46
47
        self.sections = self.uut.parse(self.file)
48
49
    def tearDown(self):
50
        os.remove(self.file)
51
52
    def test_parse_nonexisting_file(self):
53
        self.assertRaises(FileNotFoundError,
54
                          self.uut.parse,
55
                          self.nonexistentfile)
56
        self.assertNotEqual(self.uut.parse(self.file, True), self.sections)
57
58
    def test_parse_nonexisting_section(self):
59
        self.assertRaises(IndexError,
60
                          self.uut.get_section,
61
                          "inexistent section")
62
63
    def test_parse_default_section(self):
64
        default_should = OrderedDict([
65
            ('a_default', 'val'),
66
            ('another', 'val'),
67
            ('comment0', '# do you know that thats a comment'),
68
            ('test', 'content'),
69
            ('t', '')])
70
71
        key, val = self.sections.popitem(last=False)
72
        self.assertTrue(isinstance(val, Section))
73
        self.assertEqual(key, 'default')
74
75
        is_dict = OrderedDict()
76
        for k in val:
77
            is_dict[k] = str(val[k])
78
        self.assertEqual(is_dict, default_should)
79
80
    def test_parse_makefiles_section(self):
81
        makefiles_should = OrderedDict([
82
            ('j', 'a\nmultiline\nvalue'),
83
            ('another', 'a\nmultiline\nvalue'),
84
            ('comment1', '# just a omment'),
85
            ('comment2', '# just a omment'),
86
            ('lastone', 'val'),
87
            ('comment3', ''),
88
            ('a_default', 'val'),
89
            ('comment0', '# do you know that thats a comment'),
90
            ('test', 'content'),
91
            ('t', '')])
92
93
        # Pop off the default section.
94
        self.sections.popitem(last=False)
95
96
        key, val = self.sections.popitem(last=False)
97
        self.assertTrue(isinstance(val, Section))
98
        self.assertEqual(key, 'makefiles')
99
100
        is_dict = OrderedDict()
101
        for k in val:
102
            is_dict[k] = str(val[k])
103
        self.assertEqual(is_dict, makefiles_should)
104
105
        self.assertEqual(val["comment1"].key, "comment1")
106
107
    def test_parse_empty_elem_strip_section(self):
108
        empty_elem_strip_should = OrderedDict([
109
            ('a', 'a, b, c'),
110
            ('b', 'a, ,, d'),
111
            ('c', ',,,'),
112
            ('comment4', ''),
113
            ('a_default', 'val'),
114
            ('another', 'val'),
115
            ('comment0', '# do you know that thats a comment'),
116
            ('test', 'content'),
117
            ('t', '')])
118
119
        # Pop off the default and makefiles section.
120
        self.sections.popitem(last=False)
121
        self.sections.popitem(last=False)
122
123
        key, val = self.sections.popitem(last=False)
124
        self.assertTrue(isinstance(val, Section))
125
        self.assertEqual(key, 'empty_elem_strip')
126
127
        is_dict = OrderedDict()
128
        for k in val:
129
            is_dict[k] = str(val[k])
130
        self.assertEqual(is_dict, empty_elem_strip_should)
131
132
    def test_remove_empty_iter_elements(self):
133
        # Test with empty-elem stripping.
134
        uut = ConfParser(remove_empty_iter_elements=True)
135
        uut.parse(self.file)
136
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]),
137
                         ["a", "b", "c"])
138
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]),
139
                         ["a", "d"])
140
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]),
141
                         [])
142
143
        # Test without stripping.
144
        uut = ConfParser(remove_empty_iter_elements=False)
145
        uut.parse(self.file)
146
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]),
147
                         ["a", "b", "c"])
148
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]),
149
                         ["a", "", "", "d"])
150
        self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]),
151
                         ["", "", "", ""])
152
153
    def test_config_directory(self):
154
        self.uut.parse(self.tempdir)
155
156
if __name__ == '__main__':
157
    unittest.main(verbosity=2)
158