1 | import unittest |
||
2 | import os |
||
3 | |||
4 | from coalib.misc import Constants |
||
5 | from coalib.settings.Section import Section, Setting, append_to_sections |
||
6 | from coalib.settings.ConfigurationGathering import get_config_directory |
||
7 | from coalib.parsing.Globbing import glob_escape |
||
8 | |||
9 | |||
10 | class SectionTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
11 | |||
12 | def test_construction(self): |
||
13 | uut = Section(Constants.COMPLEX_TEST_STRING, None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
14 | uut = Section(Constants.COMPLEX_TEST_STRING, uut) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
15 | self.assertRaises(TypeError, Section, "irrelevant", 5) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
16 | self.assertRaises(ValueError, uut.__init__, "name", uut) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
17 | |||
18 | def test_append(self): |
||
19 | uut = Section(Constants.COMPLEX_TEST_STRING, None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
20 | self.assertRaises(TypeError, uut.append, 5) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
21 | uut.append(Setting(5, 5, 5)) |
||
22 | self.assertEqual(str(uut.get("5 ")), "5") |
||
23 | self.assertEqual(int(uut.get("nonexistent", 5)), 5) |
||
24 | |||
25 | def test_enabled(self): |
||
26 | uut = Section("name") |
||
27 | self.assertTrue(uut.is_enabled([])) |
||
28 | self.assertTrue(uut.is_enabled(["name", "wrongname"])) |
||
29 | self.assertFalse(uut.is_enabled(["wrongname"])) |
||
30 | |||
31 | uut.append(Setting("enabled", "false")) |
||
32 | self.assertFalse(uut.is_enabled([])) |
||
33 | self.assertFalse(uut.is_enabled(["wrong_name"])) |
||
34 | self.assertTrue(uut.is_enabled(["name", "wrongname"])) |
||
35 | |||
36 | def test_iter(self): |
||
37 | defaults = Section("default", None) |
||
38 | uut = Section("name", defaults) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
39 | uut.append(Setting(5, 5, 5)) |
||
40 | uut.add_or_create_setting(Setting("TEsT", 4, 5)) |
||
41 | defaults.append(Setting("tEsT", 1, 3)) |
||
42 | defaults.append(Setting(" great ", 3, 8)) |
||
43 | defaults.append(Setting(" great ", 3, 8), custom_key="custom") |
||
44 | uut.add_or_create_setting(Setting(" NEW ", "val", 8)) |
||
45 | uut.add_or_create_setting(Setting(" NEW ", "vl", 8), |
||
46 | allow_appending=False) |
||
47 | uut.add_or_create_setting(Setting("new", "val", 9), |
||
48 | custom_key="teSt ", |
||
49 | allow_appending=True) |
||
50 | self.assertEqual(list(uut), ["5", "test", "new", "great", "custom"]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
51 | |||
52 | for index in uut: |
||
53 | t = uut[index] |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
54 | self.assertNotEqual(t, None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
55 | |||
56 | self.assertIn("teST", defaults) |
||
57 | self.assertIn(" GREAT", defaults) |
||
58 | self.assertNotIn(" GrEAT !", defaults) |
||
59 | self.assertNotIn("", defaults) |
||
60 | self.assertEqual(str(uut['test']), "4\nval") |
||
61 | self.assertEqual(int(uut["GREAT "]), 3) |
||
62 | self.assertRaises(IndexError, uut.__getitem__, "doesnotexist") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
63 | self.assertRaises(IndexError, uut.__getitem__, "great", True) |
||
64 | self.assertRaises(IndexError, uut.__getitem__, " ") |
||
65 | |||
66 | def test_setitem(self): |
||
67 | uut = Section("section", None) |
||
68 | uut["key1"] = "value1" |
||
69 | self.assertEqual(str(uut), "section {key1 : 'value1'}") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
70 | uut["key1"] = "changed_value1" |
||
71 | self.assertEqual(str(uut), "section {key1 : 'changed_value1'}") |
||
72 | uut["key1"] = Setting("any key", "value1") |
||
73 | self.assertEqual(str(uut), "section {key1 : 'value1'}") |
||
74 | |||
75 | def test_string_conversion(self): |
||
76 | uut = Section("name") |
||
77 | self.assertEqual(str(uut), "name {}") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
78 | uut.append(Setting("key", "value")) |
||
79 | self.assertEqual(str(uut), "name {key : 'value'}") |
||
80 | uut.append(Setting("another_key", "another_value")) |
||
81 | self.assertEqual(str(uut), |
||
82 | "name {key : 'value', another_key : 'another_value'}") |
||
83 | |||
84 | def test_copy(self): |
||
85 | uut = Section("name") |
||
86 | uut.append(Setting("key", "value")) |
||
87 | self.assertEqual(str(uut["key"]), "value") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
88 | copy = uut.copy() |
||
89 | self.assertEqual(str(copy), str(uut)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
90 | uut.append(Setting("key", "another_value")) |
||
91 | self.assertNotEqual(str(copy), str(uut)) |
||
92 | |||
93 | uut.defaults = copy |
||
94 | copy = uut.copy() |
||
95 | self.assertEqual(str(uut.defaults), str(copy.defaults)) |
||
96 | uut.defaults.append(Setting("key", "quite_something_else")) |
||
97 | self.assertNotEqual(str(uut.defaults), str(copy.defaults)) |
||
98 | |||
99 | def test_update(self): |
||
100 | cli = Section("cli", None) |
||
101 | conf = Section("conf", None) |
||
102 | |||
103 | self.assertRaises(TypeError, cli.update, 4) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
104 | |||
105 | cli.append(Setting("key1", "value11")) |
||
106 | cli.append(Setting("key2", "value12")) |
||
107 | conf.append(Setting("key1", "value21")) |
||
108 | conf.append(Setting("key3", "value23")) |
||
109 | |||
110 | # Values are overwritten, new keys appended |
||
111 | self.assertEqual(str(conf.copy().update(cli)), |
||
112 | "conf {key1 : 'value11', key3 : 'value23', " |
||
113 | "key2 : 'value12'}") |
||
114 | |||
115 | cli.defaults = Section("clidef", None) |
||
116 | cli.defaults.append(Setting("def1", "dval1")) |
||
117 | |||
118 | self.assertEqual(str(conf.copy().update(cli).defaults), |
||
119 | "clidef {def1 : 'dval1'}") |
||
120 | |||
121 | conf.defaults = Section("confdef", None) |
||
122 | conf.defaults.append(Setting("def2", "dval2")) |
||
123 | |||
124 | self.assertEqual(str(conf.copy().update(cli).defaults), |
||
125 | "confdef {def2 : 'dval2', def1 : 'dval1'}") |
||
126 | |||
127 | def test_append_to_sections(self): |
||
128 | sections = {} |
||
129 | |||
130 | append_to_sections(sections, "", "", "") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
131 | self.assertEqual(sections, {}) |
||
132 | |||
133 | append_to_sections(sections, "key", None, "") |
||
134 | self.assertEqual(sections, {}) |
||
135 | |||
136 | append_to_sections(sections, "test", "val", "origin") |
||
137 | self.assertIn("default", sections) |
||
138 | self.assertEqual(len(sections), 1) |
||
139 | self.assertEqual(len(sections["default"].contents), 1) |
||
140 | |||
141 | append_to_sections(sections, "test1", "val", "origin", "default") |
||
142 | self.assertIn("default", sections) |
||
143 | self.assertEqual(len(sections), 1) |
||
144 | self.assertEqual(len(sections["default"].contents), 2) |
||
145 | |||
146 | def test_update_setting(self): |
||
147 | section = Section("section", None) |
||
148 | |||
149 | section.append(Setting("key1", "value11")) |
||
150 | section.append(Setting("key2", "value12")) |
||
151 | |||
152 | section.update_setting("key1", new_value="value13") |
||
153 | self.assertEqual(str(section), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
154 | "section {key1 : 'value13', key2 : 'value12'}") |
||
155 | section.update_setting("key1", "key3") |
||
156 | self.assertEqual(str(section), |
||
157 | "section {key3 : 'value13', key2 : 'value12'}") |
||
158 | section.update_setting("key3", "key4", "value14") |
||
159 | self.assertEqual(str(section), |
||
160 | "section {key4 : 'value14', key2 : 'value12'}") |
||
161 | |||
162 | def test_delete_setting(self): |
||
163 | section = Section("section", None) |
||
164 | |||
165 | section.append(Setting("key1", "value11")) |
||
166 | section.append(Setting("key2", "value12")) |
||
167 | |||
168 | section.delete_setting("key1") |
||
169 | self.assertEqual(str(section), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
170 | "section {key2 : 'value12'}") |
||
171 | |||
172 | section.append(Setting("key3", "value13")) |
||
173 | section.append(Setting("key4", "value14")) |
||
174 | |||
175 | section.delete_setting("key3") |
||
176 | self.assertEqual(str(section), |
||
177 | "section {key2 : 'value12', key4 : 'value14'}") |
||
178 | |||
179 | def test_bear_dirs(self): |
||
180 | section = Section("section", None) |
||
181 | empty_bear_dirs_len = len(section.bear_dirs()) |
||
182 | section.append(Setting("bear_dirs", "test1, test2 (1)")) |
||
183 | self.assertEqual(len(section.bear_dirs()), empty_bear_dirs_len + 2) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
184 | # Verify if bear directories are properly escaped |
||
185 | root = get_config_directory(section) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
186 | path = os.path.join(glob_escape(root), glob_escape("test2 (1)"), "**") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
187 | self.assertIn(path, section.bear_dirs()) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
188 |