-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtester.py
96 lines (79 loc) · 3.41 KB
/
tester.py
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
import asyncio
import functools
import time
import traceback
from typing import Any, Callable, Dict, List
class Tester:
__tests: List[Dict[str, Any]] = []
errors: List[str] = []
def __run(self, func: Callable, name: str, asyncTest, args = [], kwargs = {}):
t = time.time()
if asyncTest:
try:
asyncio.get_event_loop().run_until_complete(func)
except Exception as e:
self.errors.append(name)
return self.__formatError(f"Testing {name} failed! ({round(time.time() - t, 3)} seconds)", e)
else:
try:
func(*args, **kwargs)
except Exception as e:
self.errors.append(name)
return self.__formatError(f"Testing {name} failed! ({round(time.time() - t, 3)} seconds)", e)
successNotice = f"{name} passed!"
padding = ' ' * ((39 - len(successNotice)) // 2)
div = "\u001b[32m|\u001b[0m"
print("\u001b[32m========================================\u001b[0m")
print(f"{div}{padding}{successNotice}{padding}{div}")
print("\u001b[32m========================================\u001b[0m")
def addAsyncTest(self, name: str, func: asyncio.Future) -> None:
self.__tests.append({'func': func, 'name': name, 'async': True})
def addSyncTest(self, name: str, func: Callable, *args, **kwargs) -> None:
self.__tests.append({'func': func, 'name': name, 'async': False, 'args': args, 'kwargs': kwargs})
def __formatError(self, failNotice, e) -> None:
trace = ''.join(traceback.format_exception(type(e), e, e.__traceback__))
split = trace.split('\n')
split.sort(key=lambda target: len(target))
longest = len(split[-1])
paddingLeft = " "
divider = "\u001b[33m|\u001b[0m"
heading = '=' * (longest + 6)
noticePadding = ' ' * ((len(heading) - len(failNotice))//2)
split = trace.split('\n')
formattedLines = [
f"\u001b[33m{heading}\u001b[0m",
f"{divider}{noticePadding}\u001b[31m{failNotice}\u001b[0m{noticePadding[0:-3]} {divider}"
]
for line in split:
formattedLines.append(
f"{divider}{paddingLeft}{line}{' ' * (longest - len(line) + 2)}{divider}"
)
formattedLines.append(f"\u001b[33m{heading}\u001b[0m")
print()
for line in formattedLines:
print(line)
def run(self) -> None:
asyncio.new_event_loop()
t = time.time()
for test in self.__tests:
print()
if test['async']:
self.__run(test['func'], test['name'], True)
else:
self.__run(test['func'], test['name'], False, args=test['args'], kwargs=test['kwargs'])
end = time.time()
print("\nTook", round(end-t, 3), "seconds!\n")
if len(self.errors) == 0:
print(
"""\u001b[32m
=========================================
| All tests pass! |
=========================================\u001b[0m
""" )
else:
notice = f"{len(self.errors)}/{len(self.__tests)} failed!"
padding = ' ' * ((39 - len(notice)) // 2)
div = "|"
print("\u001b[31m=========================================")
print(f"{div}{padding}{notice}{padding}{div}")
print("=========================================\u001b[0m")