diff -r 02e7e1d73ebb -r 75f44d3063da testsuite.py --- a/testsuite.py Wed Jan 24 23:14:01 2018 +0200 +++ b/testsuite.py Wed Jan 31 14:34:23 2018 +0200 @@ -14,6 +14,9 @@ def error(bad_object, error_name, **args): return report_element(bad_object, 'error', error_name, args) +def notice(bad_object, error_name, **args): + return report_element(bad_object, 'notice', error_name, args) + def test_discovery(): ''' Finds all test modules and yields their names. @@ -67,22 +70,31 @@ warn(str.format('Module {} does not have a manifest', module_name)) return test_suite +def problem_key(problem): + problem_hierarchy = ['error', 'warning', 'notice'] + return (problem_hierarchy.index(problem['type']), problem['line-number']) + def check_model(model, test_suite = None): if not test_suite: test_suite = load_tests() - report = [] + problems = [] line_numbers = { element: (i, i + 1 + model.body_offset) for i, element in enumerate(model.body) } for test_name, test_function in test_suite['tests'].items(): - problems = test_function(model) - for problem in problems: + for problem in test_function(model): problem['body-index'], problem['line-number'] \ = line_numbers[problem['object']] del problem['object'] - report.append(problem) - return report + problems.append(problem) + return { + 'passed': not any( + problem['type'] == 'error' + for problem in problems + ), + 'problems': sorted(problems, key = problem_key), + } def problem_text(problem, test_suite): message = test_suite['messages'][problem['name']] @@ -91,8 +103,8 @@ return message def format_report_html(report, model, test_suite): - result = [] - for problem in report: + messages = [] + for problem in report['problems']: ldraw_code = model.body[problem['body-index']].textual_representation() message = str.format( '
  • {model_name}:{line_number}:' @@ -103,21 +115,20 @@ message = problem_text(problem, test_suite), ldraw_code = ldraw_code, ) - result.append((problem['line-number'], message)) - return '\n'.join( - problem[1] - for problem in sorted(result) - ) + messages.append(message) + return '\n'.join(messages) def format_report(report, model, test_suite): import colorama colorama.init() - result = [] - for problem in report: + messages = [] + for problem in report['problems']: if problem['type'] == 'error': text_colour = colorama.Fore.LIGHTRED_EX elif problem['type'] == 'warning': text_colour = colorama.Fore.LIGHTYELLOW_EX + elif problem['type'] == 'notice': + text_colour = colorama.Fore.LIGHTBLUE_EX else: text_colour = '' ldraw_code = model.body[problem['body-index']].textual_representation() @@ -132,11 +143,8 @@ colour_reset = colorama.Fore.RESET, ldraw_code = ldraw_code, ) - result.append((problem['line-number'], message)) - return '\n'.join( - problem[1] - for problem in sorted(result) - ) + messages.append(message) + return '\n'.join(messages) if __name__ == '__main__': from pprint import pprint