diff options
| -rwxr-xr-x | test/spec_tests.py | 70 | 
1 files changed, 39 insertions, 31 deletions
| diff --git a/test/spec_tests.py b/test/spec_tests.py index 9588b53..23f45a0 100755 --- a/test/spec_tests.py +++ b/test/spec_tests.py @@ -28,36 +28,48 @@ if __name__ == "__main__":      parser.add_argument('--debug-normalization', dest='debug_normalization',              action='store_const', const=True,              default=False, help='filter stdin through normalizer for testing') +    parser.add_argument('-n', '--number', type=int, default=None, +            help='only consider the test with the given number. Overrides --pattern.')      args = parser.parse_args(sys.argv[1:])  def print_test_header(headertext, example_number, start_line, end_line):      print "Example %d (lines %d-%d) %s" % (example_number,start_line,end_line,headertext) -def do_test(test, normalize): +def do_test(test, normalize, result_counts):      [retcode, actual_html, err] = cmark.to_html(test['markdown'])      if retcode == 0:          expected_html = test['html'] +        unicode_error = None          if normalize: -            passed = normalize_html(actual_html) == normalize_html(expected_html) +            try: +                passed = normalize_html(actual_html) == normalize_html(expected_html) +            except UnicodeDecodeError, e: +                unicode_error = e +                passed = False          else:              passed = actual_html == expected_html          if passed: -            return 'pass' +            result_counts['pass'] += 1          else:              print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])              sys.stdout.write(test['markdown']) -            expected_html_lines = expected_html.splitlines(True) -            actual_html_lines = actual_html.splitlines(True) -            for diffline in unified_diff(expected_html_lines, actual_html_lines, -                            "expected HTML", "actual HTML"): -                sys.stdout.write(diffline) +            if unicode_error: +                print "Unicode error: " + str(unicode_error) +                print "Expected: " + repr(expected_html) +                print "Got:      " + repr(actual_html) +            else: +                expected_html_lines = expected_html.splitlines(True) +                actual_html_lines = actual_html.splitlines(True) +                for diffline in unified_diff(expected_html_lines, actual_html_lines, +                                "expected HTML", "actual HTML"): +                    sys.stdout.write(diffline)              sys.stdout.write('\n') -            return 'fail' +            result_counts['fail'] += 1      else:          print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])          print "program returned error code %d" % retcode          print(err) -        return 'error' +        result_counts['error'] += 1  def get_tests(specfile):      line_number = 0 @@ -100,28 +112,24 @@ def get_tests(specfile):                  html_lines.append(line)      return tests -def do_tests(cmark, tests, pattern, normalize): -    passed = 0 -    errored = 0 -    failed = 0 -    skipped = 0 -    if pattern: -        pattern_re = re.compile(pattern, re.IGNORECASE) +def do_tests(cmark, tests, pattern, normalize, number): +    result_counts = dict.fromkeys(['pass', 'error', 'fail', 'skip'], 0) +    if number: +        test = tests[number - 1] +        do_test(test, normalize, result_counts) +        result_counts['skip'] = len(tests) - 1      else: -        pattern_re = re.compile('.') -    for test in tests: -        if re.search(pattern_re, test['section']): -            result = do_test(test, normalize) -            if result == 'pass': -                passed += 1 -            elif result == 'fail': -                failed += 1 -            else: -                errored += 1 +        if pattern: +            pattern_re = re.compile(pattern, re.IGNORECASE)          else: -            skipped += 1 -    print "%d passed, %d failed, %d errored, %d skipped" % (passed, failed, errored, skipped) -    return (failed == 0 and errored == 0) +            pattern_re = re.compile('.') +        for test in tests: +            if re.search(pattern_re, test['section']): +                do_test(test, normalize, result_counts) +            else: +                result_counts['skip'] += 1 +    print "{pass} passed, {fail} failed, {error} errored, {skip} skipped".format(**result_counts) +    return (result_counts['fail'] == 0 and result_counts['error'] == 0)  if __name__ == "__main__":      if args.debug_normalization: @@ -134,7 +142,7 @@ if __name__ == "__main__":          exit(0)      else:          cmark = CMark(prog=args.program, library_dir=args.library_dir) -        if do_tests(cmark, tests, args.pattern, args.normalize): +        if do_tests(cmark, tests, args.pattern, args.normalize, args.number):              exit(0)          else:              exit(1) | 
