-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyTesting.py
More file actions
359 lines (281 loc) · 10.5 KB
/
pyTesting.py
File metadata and controls
359 lines (281 loc) · 10.5 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Copyright 2026 Joachim REY
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
class Testing :
"""
Testing allow to create Python tests easily.
It require testing function to be referenced with the
@Testing.reference decorator.
Then, Testing.test() can be used to do tests.
Testing.display() display the results of all the tests.
"""
# Variables
_messages = { }
_current_function_path = [ ]
_total_tests = 0
_failed_tests = 0
# Parameters
_display_results = True
_catch_errors = True
_display_starting_char = ''
_display_color = True
_display_style = 0
# Constants
_colors = [
"\033[33m", # Yellow
"\033[32m", # Green
"\033[31m" # Red
]
_pipes = [
[
'\u2570',
'\u2574',
'\u251C',
'\u2502'
],
[
'\u2514'
],
[
'\u255A',
'\u2550',
'\u2560',
'\u2551'
],
[
'-',
' ',
'-',
'|'
]
]
@staticmethod
def setDisplayLiveResults( display : bool ) -> None :
"""
Change to wether or not display result of each tests when Testing.test()
is called.
Default: True
In :
display, bool : True will display the result, False hide them.
Out :
None
"""
Testing._display_results = display
return None
@staticmethod
def setCatchErrors( catch : bool ) -> None :
"""
Change to wether or not catch errors in referenced testing functions.
Catched errors will be added in the _messages dictionnary
Default: True
In :
catch, bool : True will catch errors, False will not.
Out :
None
"""
Testing._catch_errors = catch
return None
@staticmethod
def setDisplayStartingChar( char : str ) -> None :
"""
Define the starting character(s) of the tree-view created when
displayed with Testing.display().
Default: ''
In :
char, str : The new starting character.
Out :
None
"""
Testing._display_starting_char = char
return None
@staticmethod
def setDisplayColors( display : bool ) -> None :
"""
Change to wether or not display ANSI colors.
Default: True
In :
display, bool : True will display color, False won't.
Out :
None
"""
Testing._display_color = display
return None
@staticmethod
def setDisplayStyle( display : int ) -> None :
"""
Change the style of the tree-view.
0 uses round pipes,
1 uses square pipes,
2 uses hollow pipes and
3 uses ASCII.
Default: True
In :
display, bool : True will display color, False won't.
Out :
None
"""
if display < len( Testing._pipes ) :
Testing._display_style = display
return None
@staticmethod
def test( valid : bool, message : str ) -> bool :
"""
Test a statement assert style. the statement is False, the
test fail and is added to the messages dictionnary.
In :
valid, bool : Statement to be tested.
message, str : Message if the test fail.
Out :
bool : Tested statement.
"""
res = Testing._colorize( ".", 1 )
if not valid :
res = Testing._colorize( "F", 2 )
Testing._addMessage( Testing._current_function_path,
Testing._messages, message )
Testing._failed_tests += 1
Testing._total_tests += 1
if Testing._display_results :
print( res, end="" )
return valid
@staticmethod
def _colorize( text: str, color: int ) -> str :
"""
Colorize the given text with the given color indice in the
_colors list.
If _display_color if False or the indice is wrong, it will
return the base text.
In :
text, str : Text to colorize.
color, int : Color indice in _colors.
Out :
str : Colorized text.
"""
if ( not Testing._display_color ) or \
color > len( Testing._colors ) or color < 0:
return text
return Testing._colors[ color ] + text + "\033[0m"
@staticmethod
def _get_pipe( indice : int ) -> chr :
"""
Return the correct pipe given an indice. If the pipe does not
exists in the current style, it will return the pipe in the
default style. If the indice does not correspond to any pipe,
it will return an empty char.
In:
indice, int : Indice of the pipe
Out:
chr: The resulting pipe
"""
if indice >= len( Testing._pipes[ 0 ] ) :
return ''
if indice >= len( Testing._pipes[ Testing._display_style ] ):
return Testing._pipes[ 0 ][ indice ]
return Testing._pipes[ Testing._display_style ][ indice ]
@staticmethod
def _addHierarchy( hierarchy : list, tree : dict ) -> None :
"""
Private function to add a function in the _messages hierarchy.
In :
hierarchy, list: Path to the function to add.
tree, dict : The dictionnary where to function should be added.
Out :
None
"""
if len( hierarchy ) == 1 :
tree[ hierarchy[0] ] = [ ]
else :
if not hierarchy[0] in tree.keys() :
tree[ hierarchy[0] ] = {}
Testing._addHierarchy( hierarchy[1:], tree[ hierarchy[0] ] )
return None
@staticmethod
def _addMessage( hierarchy : list, tree : dict, message : str ) -> None :
"""
Private function to add a message in the _messages at the given hierarchy.
In :
hierarchy, list : Path to the _messages to add.
tree, dict: The dictionnary where to _messages should be added.
_messages, str : Message to add.
Out :
None
"""
if len( hierarchy ) == 1 :
tree[ hierarchy[0] ].append( message )
else:
if not hierarchy[0] in tree.keys() :
raise Exception( "Testing used out of the context of a"
f"referenced function! ({ hierarchy })" )
Testing._addMessage( hierarchy[1:], tree[hierarchy[0]], message )
return None
@staticmethod
def _displayHierarchy( tree : dict, start : str ) -> str :
"""
Private function to display the _messages dictionnary.
In :
tree, dict : The dictionnary where to function should be added.
start, str : Characters to add before an entry.
Out :
str : The resulting text.
"""
res = ""
if isinstance( tree, list ) :
last = len( tree ) - 1
for i in range( 0, last + 1 ) :
if ( i == last ):
res += Testing._colorize( f"{ start }{ Testing._get_pipe( 0 ) }{ Testing._get_pipe( 1 ) }", 0 )
else:
res += Testing._colorize( f"{ start }{ Testing._get_pipe( 2 ) }{ Testing._get_pipe( 1 ) }", 0 )
res += tree[i] + '\n'
return res
else :
keys = list( tree.keys() )
last = len( keys ) - 1
start_c = Testing._get_pipe( 3 )
for i in range( 0, last + 1 ) :
if ( i == last ) :
res += Testing._colorize( f"{ start }{ Testing._get_pipe( 0 ) }{ Testing._get_pipe( 1 ) }", 0 )
start_c = ' '
else :
res += Testing._colorize( f"{ start }{ Testing._get_pipe( 2 ) }{ Testing._get_pipe( 1 ) }", 0 )
res += Testing._colorize( f"{ keys[i] }\n", 0 )
res += Testing._displayHierarchy( tree[ keys[i] ], f"{ start }{ start_c } " )
return res
@staticmethod
def reference(func : type) -> None :
"""
Reference a function in the _messages dictionnary
and execute it.
In :
func, type : Test function to reference.
Out :
None
"""
def wrapper( *args, **kwargs ) :
Testing._current_function_path = func.__qualname__.split( "." )
Testing._addHierarchy( Testing._current_function_path, Testing._messages )
if not Testing._catch_errors:
return func( *args, **kwargs )
try:
return func( *args, **kwargs )
except Exception as e:
Testing._addMessage( Testing._current_function_path, Testing._messages,
Testing._colorize( e.__str__(), 2 ) )
return wrapper
@staticmethod
def display( ) -> None :
"""
Display the result of all tests ran so far and their hierarchy.
In :
Out :
None
"""
print(
Testing._colorize( f"\n{ Testing._total_tests - Testing._failed_tests } tests passed, ", 1 ) +
Testing._colorize( f"{ Testing._failed_tests } tests failed\n", 2 ) +
Testing._displayHierarchy( Testing._messages, Testing._display_starting_char )
)
return None