Failed Conditions
Pull Request — master (#1093)
by Lasse
01:45
created

coalib.tests.parsing.ConfParserTest.setUp()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

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