1 | """This module is for testing the indent rainmeter module.""" |
||
2 | |||
3 | |||
4 | import sys |
||
5 | |||
6 | from unittest import TestCase |
||
7 | |||
8 | INDENT = sys.modules["Rainmeter.indentrainmeter"] |
||
9 | |||
10 | |||
11 | def indention_depth_from_initial(line): |
||
12 | """Helper method to start with an initial indention type.""" |
||
13 | return INDENT.calc_line_indention_depth(line, INDENT.IndentType.Initial, 0) |
||
14 | |||
15 | |||
16 | def indention_depth_from_fold(line): |
||
17 | """Helper method to start with a fold marker indention type.""" |
||
18 | return INDENT.calc_line_indention_depth(line, INDENT.IndentType.FoldMarker, 1) |
||
19 | |||
20 | |||
21 | def indention_depth_from_section(line): |
||
22 | """Helper method to start with a section indention type.""" |
||
23 | return INDENT.calc_line_indention_depth(line, INDENT.IndentType.Section, 1) |
||
24 | |||
25 | |||
26 | View Code Duplication | class TestCalcLineIndentionDepthFromInitial(TestCase): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
27 | """ |
||
28 | This test is for showing the behaviour detecting different indenttypes. |
||
29 | |||
30 | Context depth can increase depending how the document starts. |
||
31 | It accepts invalid Rainmeter definitions like: |
||
32 | |||
33 | Key=Value |
||
34 | |||
35 | at the beginning of your document. This will not fail the indention itself. |
||
36 | """ |
||
37 | |||
38 | def test_with_empty_line(self): |
||
39 | """An empty line should be ignored.""" |
||
40 | line = "" |
||
41 | indention_depth = indention_depth_from_initial(line) |
||
42 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0)) |
||
43 | |||
44 | def test_with_comment(self): |
||
45 | """A comment will be ignored.""" |
||
46 | line = "; This is a comment" |
||
47 | indention_depth = indention_depth_from_initial(line) |
||
48 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0)) |
||
49 | |||
50 | def test_with_fold_marker(self): |
||
51 | """Fold markers increase the indention depth.""" |
||
52 | line = ";; This is a fold marker" |
||
53 | indention_depth = indention_depth_from_initial(line) |
||
54 | self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1)) |
||
55 | |||
56 | def test_with_section(self): |
||
57 | """Section increase the indention depth.""" |
||
58 | line = "[Section]" |
||
59 | indention_depth = indention_depth_from_initial(line) |
||
60 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1)) |
||
61 | |||
62 | def test_with_key_value(self): |
||
63 | """Key values are actually invalid but they stay at the same indention level.""" |
||
64 | line = "Key = Value" |
||
65 | indention_depth = indention_depth_from_initial(line) |
||
66 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0)) |
||
67 | |||
68 | |||
69 | View Code Duplication | class TestCalcLineIndentionDepthFromFoldMarker(TestCase): |
|
0 ignored issues
–
show
|
|||
70 | """ |
||
71 | This test is to show the behaviour for an indention coming from a fold marker. |
||
72 | |||
73 | A fold marker is defined by ;; |
||
74 | and meant to be a synthatic sugar definition to fold multiple sections at once. |
||
75 | For example you can group all meters together or all about problem X. |
||
76 | """ |
||
77 | |||
78 | def test_with_empty_line(self): |
||
79 | """ |
||
80 | Due to the fold marker the indention depth is 1. |
||
81 | |||
82 | Thus the following indention depth stays at 1 |
||
83 | but the line itself is rendered as zero. |
||
84 | This prevents a lot of whitespaces in the file |
||
85 | if you split up your section. |
||
86 | """ |
||
87 | line = "" |
||
88 | indention_depth = indention_depth_from_fold(line) |
||
89 | self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1)) |
||
90 | |||
91 | def test_with_comment(self): |
||
92 | """Comment are printed in the same indention level as given.""" |
||
93 | line = "; This is a comment" |
||
94 | indention_depth = indention_depth_from_fold(line) |
||
95 | self.assertEqual(indention_depth, (1, INDENT.IndentType.FoldMarker, 1)) |
||
96 | |||
97 | def test_with_fold_marker(self): |
||
98 | """Additional fold marker will be printed at the same level as the previous fold marker.""" |
||
99 | line = ";; This is a fold marker" |
||
100 | indention_depth = indention_depth_from_fold(line) |
||
101 | self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1)) |
||
102 | |||
103 | def test_with_section(self): |
||
104 | """A section increases the depth context.""" |
||
105 | line = "[Section]" |
||
106 | indention_depth = indention_depth_from_fold(line) |
||
107 | self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 2)) |
||
108 | |||
109 | def test_with_key_value(self): |
||
110 | """ |
||
111 | Special handled case since it is invalid. |
||
112 | |||
113 | KeyValue pairs stay at level 1. |
||
114 | """ |
||
115 | line = "Key = Value" |
||
116 | indention_depth = indention_depth_from_fold(line) |
||
117 | self.assertEqual(indention_depth, (1, INDENT.IndentType.FoldMarker, 1)) |
||
118 | |||
119 | |||
120 | View Code Duplication | class TestCalcLineIndentionDepthFromSection(TestCase): |
|
0 ignored issues
–
show
|
|||
121 | """Section increase the depth level.""" |
||
122 | |||
123 | def test_with_empty_line(self): |
||
124 | """Empty lines are ignored.""" |
||
125 | line = "" |
||
126 | indention_depth = indention_depth_from_section(line) |
||
127 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1)) |
||
128 | |||
129 | def test_with_comment(self): |
||
130 | """Comment are printed on same level as key value pairs.""" |
||
131 | line = "; This is a comment" |
||
132 | indention_depth = indention_depth_from_section(line) |
||
133 | self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 1)) |
||
134 | |||
135 | def test_with_fold_marker(self): |
||
136 | """Invalid construct, but this counts as a simple comment.""" |
||
137 | line = ";; This is a fold marker" |
||
138 | indention_depth = indention_depth_from_section(line) |
||
139 | self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1)) |
||
140 | |||
141 | def test_with_section(self): |
||
142 | """Invalid construct. Section following a section are staying on the same level.""" |
||
143 | line = "[Section]" |
||
144 | indention_depth = indention_depth_from_section(line) |
||
145 | self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1)) |
||
146 | |||
147 | def test_with_key_value(self): |
||
148 | """KeyValue Pairs are printed on the next level.""" |
||
149 | line = "Key = Value" |
||
150 | indention_depth = indention_depth_from_section(line) |
||
151 | self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 1)) |
||
152 | |||
153 | |||
154 | class TestIndentWholeSection(TestCase): |
||
155 | """This is about testing a whole function test.""" |
||
156 | |||
157 | def test_one_section(self): |
||
158 | """Testing a stand alone section.""" |
||
159 | content = ''' |
||
160 | [Rainmeter] |
||
161 | Update=1000 |
||
162 | DynamicWindowSize=1 |
||
163 | DefaultUpdateDivider=1000 |
||
164 | AccurateText=1 |
||
165 | OnWakeAction=[!Refresh "(Config)"]''' |
||
166 | |||
167 | result = ''' |
||
168 | [Rainmeter] |
||
169 | \tUpdate=1000 |
||
170 | \tDynamicWindowSize=1 |
||
171 | \tDefaultUpdateDivider=1000 |
||
172 | \tAccurateText=1 |
||
173 | \tOnWakeAction=[!Refresh "(Config)"]''' |
||
174 | |||
175 | reference = INDENT.indent_text_by_tab_size(content) |
||
176 | |||
177 | self.assertEqual(reference, result) |
||
178 | |||
179 | def test_two_sections(self): |
||
180 | """Testing only two consecutive sections.""" |
||
181 | content = ''' |
||
182 | [Rainmeter] |
||
183 | Update=1000 |
||
184 | DynamicWindowSize=1 |
||
185 | DefaultUpdateDivider=1000 |
||
186 | AccurateText=1 |
||
187 | OnWakeAction=[!Refresh "(Config)"] |
||
188 | |||
189 | |||
190 | [Metadata] |
||
191 | Name=TestEnvironment |
||
192 | Author=thatsIch |
||
193 | Information=PlayGround for Metadata |
||
194 | Version=0.0.1 |
||
195 | License=MIT''' |
||
196 | |||
197 | result = ''' |
||
198 | [Rainmeter] |
||
199 | \tUpdate=1000 |
||
200 | \tDynamicWindowSize=1 |
||
201 | \tDefaultUpdateDivider=1000 |
||
202 | \tAccurateText=1 |
||
203 | \tOnWakeAction=[!Refresh "(Config)"] |
||
204 | |||
205 | |||
206 | [Metadata] |
||
207 | \tName=TestEnvironment |
||
208 | \tAuthor=thatsIch |
||
209 | \tInformation=PlayGround for Metadata |
||
210 | \tVersion=0.0.1 |
||
211 | \tLicense=MIT''' |
||
212 | |||
213 | reference = INDENT.indent_text_by_tab_size(content) |
||
214 | |||
215 | self.assertEqual(reference, result) |
||
216 | |||
217 | def test_section_with_divider(self): |
||
218 | """After a divider a section can follow which needs to be fully indented.""" |
||
219 | content = ''' |
||
220 | ;;==================================================== |
||
221 | ;; Rainmeter Section |
||
222 | ;;==================================================== |
||
223 | [Rainmeter] |
||
224 | Update=1000 |
||
225 | DynamicWindowSize=1 |
||
226 | DefaultUpdateDivider=1000 |
||
227 | AccurateText=1 |
||
228 | OnWakeAction=[!Refresh "(Config)"]''' |
||
229 | |||
230 | result = ''' |
||
231 | ;;==================================================== |
||
232 | ;; Rainmeter Section |
||
233 | ;;==================================================== |
||
234 | \t[Rainmeter] |
||
235 | \t\tUpdate=1000 |
||
236 | \t\tDynamicWindowSize=1 |
||
237 | \t\tDefaultUpdateDivider=1000 |
||
238 | \t\tAccurateText=1 |
||
239 | \t\tOnWakeAction=[!Refresh "(Config)"]''' |
||
240 | |||
241 | reference = INDENT.indent_text_by_tab_size(content) |
||
242 | |||
243 | self.assertEqual(reference, result) |
||
244 | |||
245 | def test_divider_with_two_sections(self): |
||
246 | """ |
||
247 | After a divider multiple sections can follow. |
||
248 | |||
249 | Both sections need to be fully indented. |
||
250 | """ |
||
251 | content = ''' |
||
252 | ;;==================================================== |
||
253 | ;; Rainmeter Section |
||
254 | ;;==================================================== |
||
255 | [Rainmeter] |
||
256 | Update=1000 |
||
257 | DynamicWindowSize=1 |
||
258 | DefaultUpdateDivider=1000 |
||
259 | AccurateText=1 |
||
260 | OnWakeAction=[!Refresh "(Config)"] |
||
261 | |||
262 | |||
263 | [Metadata] |
||
264 | Name=TestEnvironment |
||
265 | Author=thatsIch |
||
266 | Information=PlayGround for Metadata |
||
267 | Version=0.0.1 |
||
268 | License=MIT''' |
||
269 | |||
270 | result = ''' |
||
271 | ;;==================================================== |
||
272 | ;; Rainmeter Section |
||
273 | ;;==================================================== |
||
274 | \t[Rainmeter] |
||
275 | \t\tUpdate=1000 |
||
276 | \t\tDynamicWindowSize=1 |
||
277 | \t\tDefaultUpdateDivider=1000 |
||
278 | \t\tAccurateText=1 |
||
279 | \t\tOnWakeAction=[!Refresh "(Config)"] |
||
280 | |||
281 | |||
282 | \t[Metadata] |
||
283 | \t\tName=TestEnvironment |
||
284 | \t\tAuthor=thatsIch |
||
285 | \t\tInformation=PlayGround for Metadata |
||
286 | \t\tVersion=0.0.1 |
||
287 | \t\tLicense=MIT''' |
||
288 | |||
289 | reference = INDENT.indent_text_by_tab_size(content) |
||
290 | |||
291 | self.assertEqual(reference, result) |
||
292 |