Debian Codegraph @

src/pyj/test.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 import traceback
6
7 import initialize # noqa: unused-import
8 import read_book.test_cfi # noqa: unused-import
9 import test_annotations # noqa: unused-import
10 import test_date # noqa: unused-import
11 import test_item_list # noqa: unused-import
12 import test_library_data # noqa: unused-import
13 import test_local_books # noqa: unused-import
14 import test_nav_list # noqa: unused-import
15 import test_saved_virtual_libraries # noqa: unused-import
16 import test_sidebar # noqa: unused-import
17 import test_theme # noqa: unused-import
18 import test_utils # noqa: unused-import
19 import test_viewer_appearance # noqa: unused-import
20 from testing import registered_tests, reset_dom
21
22
23 def get_matching_tests_for_name(name):
24 ans = []
25 for k in Object.keys(registered_tests):
26 q = k.split('.')[-1]
27 if not name or q is name:
28 ans.append(registered_tests[k])
29 return ans
30
31
32 def get_traceback(e):
33 lines = traceback.format_exception()
34 last_line = lines[-1]
35 final_lines = []
36 pat = /at assert_[0-9a-zA-Z_]+ \(/
37 for line in lines[:-1]:
38 if pat.test(line):
39 break
40 final_lines.push(line)
41 final_lines.push(last_line)
42 final_lines.push(e.toString())
43 return final_lines.join('')
44
45
46 def run_tests(tests):
47 failed_tests = []
48 for f in tests:
49 reset_dom()
50 st = window.performance.now()
51 try:
52 f()
53 time = window.performance.now() - st
54 except Exception as e:
55 tb = get_traceback(e)
56 failed_tests.append((f.test_name, tb))
57 print(f'{f.test_name} ... FAIL')
58 else:
59 print(f'{f.test_name} ... ok [{time:.1f} ms]')
60 return failed_tests
61
62
63 def main():
64 tests = get_matching_tests_for_name()
65 st = window.performance.now()
66 failed_tests = run_tests(tests)
67 time = window.performance.now() - st
68 if failed_tests.length:
69 for ft in failed_tests:
70 console.error(f'Failed {ft[0]} with traceback:')
71 console.error(ft[1])
72 console.error(f'{failed_tests.length} out of {tests.length} failed in {time:.1f} ms')
73 else:
74 print(f'Ran {tests.length} tests in {time:.1f} ms')
75 return 1 if failed_tests.length else 0
76
77
78 window.main = main
79 document.title = 'initialized'