testsuite.py

Mon, 22 Jan 2018 18:23:25 +0200

author
Santeri Piippo
date
Mon, 22 Jan 2018 18:23:25 +0200
changeset 23
1b9645b7ddb0
parent 19
9169bad392c4
child 29
db6ca177c6c4
permissions
-rw-r--r--

added matrix all zero test

16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
1 from warnings import warn
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
2
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
3 def report_element(bad_object, type, error_name, args):
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
4 return {
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
5 'type': type,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
6 'object': bad_object,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
7 'name': error_name,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
8 'args': args,
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
9 }
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
10
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
11 def warning(bad_object, error_name, **args):
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
12 return report_element(bad_object, 'warning', error_name, args)
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
13
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
14 def error(bad_object, error_name, **args):
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
15 return report_element(bad_object, 'error', error_name, args)
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
17 def test_discovery():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
18 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
19 Finds all test modules and yields their names.
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
20 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
21 from pkgutil import walk_packages
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
22 import tests
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
23 yield from sorted(
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
24 'tests.' + result.name
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
25 for result in walk_packages(tests.__path__)
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
26 )
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
27
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
28 def do_manifest_integrity_checks(test_suite, module):
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
29 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
30 Runs integrity checks on a given module's manifest.
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
31 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
32 def check_for_extra_keys():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
33 extra_keys = module.manifest.keys() - test_suite.keys()
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
34 if extra_keys:
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
35 warn(str.format(
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
36 '{}: extra keys in manifest: {}',
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
37 module.__name__,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
38 ', '.join(map(str, extra_keys))
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
39 ))
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
40 def check_for_manifest_duplicates():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
41 for key in test_suite.keys():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
42 duplicates = module.manifest[key].keys() & test_suite[key].keys()
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
43 if duplicates:
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
44 warn(str.format(
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
45 '{}: redefined {} in manifests: {}',
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
46 module.__name__,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
47 key,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
48 duplicates,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
49 ))
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
50 check_for_extra_keys()
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
51 check_for_manifest_duplicates()
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
52
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
53 def load_tests():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
54 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
55 Imports test modules and combines their manifests into a test suite.
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
56 '''
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
57 test_suite = {'tests': {}, 'messages': {}}
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
58 for module_name in test_discovery():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
59 from importlib import import_module
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
60 module = import_module(module_name)
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
61 if hasattr(module, 'manifest'):
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
62 do_manifest_integrity_checks(test_suite, module)
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
63 # Merge the data from the manifest
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
64 for key in module.manifest.keys() & test_suite.keys():
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
65 test_suite[key].update(module.manifest[key])
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
66 else:
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
67 warn(str.format('Module {} does not have a manifest', module_name))
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
68 return test_suite
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
69
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
70 def check_model(model, test_suite = None):
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
71 if not test_suite:
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
72 test_suite = load_tests()
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
73 report = []
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
74 line_numbers = {
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
75 element: (i, i + 1 + model.body_offset)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
76 for i, element in enumerate(model.body)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
77 }
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
78 for test_name, test_function in test_suite['tests'].items():
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
79 problems = test_function(model)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
80 for problem in problems:
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
81 problem['body-index'], problem['line-number'] \
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
82 = line_numbers[problem['object']]
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
83 del problem['object']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
84 report.append(problem)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
85 return report
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
86
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
87 def format_report(report, model, test_suite):
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
88 import colorama
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
89 colorama.init()
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
90 result = []
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
91 for problem in report:
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
92 problem_text = test_suite['messages'][problem['name']]
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
93 if callable(problem_text):
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
94 problem_text = problem_text(**problem['args'])
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
95 if problem['type'] == 'error':
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
96 text_colour = colorama.Fore.LIGHTRED_EX
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
97 elif problem['type'] == 'warning':
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
98 text_colour = colorama.Fore.LIGHTYELLOW_EX
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
99 else:
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
100 text_colour = ''
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 18
diff changeset
101 ldraw_code = model.body[problem['body-index']].textual_representation()
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
102 message = str.format(
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
103 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 18
diff changeset
104 '{colour_reset}\n\t{ldraw_code}',
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
105 text_colour = text_colour,
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
106 model_name = model.name,
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
107 line_number = problem['line-number'],
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
108 problem_type = problem['type'],
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
109 message = problem_text,
672ebc45685a made errors prettier
Santeri Piippo
parents: 17
diff changeset
110 colour_reset = colorama.Fore.RESET,
19
9169bad392c4 better handling of ldraw code
Santeri Piippo
parents: 18
diff changeset
111 ldraw_code = ldraw_code,
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
112 )
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
113 result.append((problem['line-number'], message))
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
114 return '\n'.join(
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
115 problem[1]
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
116 for problem in sorted(result)
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
117 )
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 16
diff changeset
118
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
119 if __name__ == '__main__':
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
120 from pprint import pprint
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 13
diff changeset
121 pprint(load_tests())

mercurial