From d1922eb6f17578774866a13fd5428cdd3bc2280d Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 27 Dec 2014 14:39:47 -0700 Subject: Updated tests to use python3. --- test/CMakeLists.txt | 3 ++- test/cmark.py | 10 ++++++---- test/normalize.py | 42 +++++++++++++++++++++--------------------- test/pathological_tests.py | 20 ++++++++++---------- test/spec_tests.py | 20 ++++++++++---------- 5 files changed, 49 insertions(+), 46 deletions(-) (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6a46dc9..f08b325 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,7 @@ # To get verbose output: cmake --build build --target "test" -- ARGS='-V' -set(PYTHON python) +find_package(PythonInterp 3 REQUIRED) +set(PYTHON ${PYTHON_EXECUTABLE}) if (WIN32) file(TO_NATIVE_PATH ${CMAKE_BINARY_DIR}/src WIN_DLL_DIR) diff --git a/test/cmark.py b/test/cmark.py index 432a3e0..21d0b3f 100644 --- a/test/cmark.py +++ b/test/cmark.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from ctypes import CDLL, c_char_p, c_long @@ -7,11 +7,13 @@ import platform def pipe_through_prog(prog, text): p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) - [result, err] = p1.communicate(input=text) - return [p1.returncode, result, err] + [result, err] = p1.communicate(input=text.encode('utf-8')) + return [p1.returncode, result.decode('utf-8'), err] def use_library(lib, text): - return [0, lib(text, len(text)), ''] + textbytes = text.encode('utf-8') + textlen = len(textbytes) + return [0, lib(textbytes, textlen).decode('utf-8'), ''] class CMark: def __init__(self, prog=None, library_dir=None): diff --git a/test/normalize.py b/test/normalize.py index 894a837..03d958e 100644 --- a/test/normalize.py +++ b/test/normalize.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from HTMLParser import HTMLParser, HTMLParseError -from htmlentitydefs import name2codepoint +from html.parser import HTMLParser, HTMLParseError +from html.entities import name2codepoint import sys import re import cgi @@ -14,7 +14,7 @@ class MyHTMLParser(HTMLParser): HTMLParser.__init__(self) self.last = "starttag" self.in_pre = False - self.output = u"" + self.output = "" self.last_tag = "" def handle_data(self, data): after_tag = self.last == "endtag" or self.last == "starttag" @@ -74,7 +74,7 @@ class MyHTMLParser(HTMLParser): self.last = "pi" def handle_entityref(self, name): try: - c = unichr(name2codepoint[name]) + c = chr(name2codepoint[name]) except KeyError: c = None self.output_char(c, '&' + name + ';') @@ -82,22 +82,22 @@ class MyHTMLParser(HTMLParser): def handle_charref(self, name): try: if name.startswith("x"): - c = unichr(int(name[1:], 16)) + c = chr(int(name[1:], 16)) else: - c = unichr(int(name)) + c = chr(int(name)) except ValueError: c = None self.output_char(c, '&' + name + ';') self.last = "ref" # Helpers. def output_char(self, c, fallback): - if c == u'<': + if c == '<': self.output += "<" - elif c == u'>': + elif c == '>': self.output += ">" - elif c == u'&': + elif c == '&': self.output += "&" - elif c == u'"': + elif c == '"': self.output += """ elif c == None: self.output += fallback @@ -122,43 +122,43 @@ def normalize_html(html): in pre tags): >>> normalize_html("

a \t b

") - u'

a b

' + '

a b

' >>> normalize_html("

a \t\nb

") - u'

a b

' + '

a b

' * Whitespace surrounding block-level tags is removed. >>> normalize_html("

a b

") - u'

a b

' + '

a b

' >>> normalize_html("

a b

") - u'

a b

' + '

a b

' >>> normalize_html("

a b

") - u'

a b

' + '

a b

' >>> normalize_html("\n\t

\n\t\ta b\t\t

\n\t") - u'

a b

' + '

a b

' >>> normalize_html("a b ") - u'a b ' + 'a b ' * Self-closing tags are converted to open tags. >>> normalize_html("
") - u'
' + '
' * Attributes are sorted and lowercased. >>> normalize_html('x') - u'x' + 'x' * References are converted to unicode, except that '<', '>', '&', and '"' are rendered using entities. >>> normalize_html("∀&><"") - u'\u2200&><"' + '\u2200&><"' """ html_chunk_re = re.compile("(\|\<[^>]*\>|[^<]+)") @@ -171,7 +171,7 @@ def normalize_html(html): if chunk.group(0)[:8] == " " * 50000) + "a"), re.compile("(
\n){50000}")), "U+0000 in input": - ("abc\0de\0", - re.compile("abc(�)?de(�)?")) + ("abc\u0000de\u0000", + re.compile("abc\ufffd?de\ufffd?")) } whitespace_re = re.compile('/s+/') @@ -39,24 +39,24 @@ passed = 0 errored = 0 failed = 0 -print "Testing pathological cases:" +print("Testing pathological cases:") for description in pathological: - print description + print(description) (inp, regex) = pathological[description] [rc, actual, err] = cmark.to_html(inp) if rc != 0: errored += 1 - print description - print "program returned error code %d" % rc + print(description) + print("program returned error code %d" % rc) print(err) elif regex.search(actual): passed += 1 else: - print description, 'failed' - print(actual) + print(description, 'failed') + print(repr(actual)) failed += 1 -print "%d passed, %d failed, %d errored" % (passed, failed, errored) +print("%d passed, %d failed, %d errored" % (passed, failed, errored)) if (failed == 0 and errored == 0): exit(0) else: diff --git a/test/spec_tests.py b/test/spec_tests.py index aa172a0..b8b480e 100755 --- a/test/spec_tests.py +++ b/test/spec_tests.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys @@ -33,7 +33,7 @@ if __name__ == "__main__": 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) + print("Example %d (lines %d-%d) %s" % (example_number,start_line,end_line,headertext)) def do_test(test, normalize, result_counts): [retcode, actual_html, err] = cmark.to_html(test['markdown']) @@ -43,7 +43,7 @@ def do_test(test, normalize, result_counts): if normalize: try: passed = normalize_html(actual_html) == normalize_html(expected_html) - except UnicodeDecodeError, e: + except UnicodeDecodeError as e: unicode_error = e passed = False else: @@ -54,9 +54,9 @@ def do_test(test, normalize, result_counts): print_test_header(test['section'], test['example'], test['start_line'], test['end_line']) sys.stdout.write(test['markdown']) if unicode_error: - print "Unicode error: " + str(unicode_error) - print "Expected: " + repr(expected_html) - print "Got: " + repr(actual_html) + 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) @@ -67,7 +67,7 @@ def do_test(test, normalize, result_counts): 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("program returned error code %d" % retcode) print(err) result_counts['error'] += 1 @@ -114,7 +114,7 @@ def get_tests(specfile): if __name__ == "__main__": if args.debug_normalization: - print normalize_html(sys.stdin.read()) + print(normalize_html(sys.stdin.read())) exit(0) all_tests = get_tests(args.spec) @@ -124,7 +124,7 @@ if __name__ == "__main__": pattern_re = re.compile('.') tests = [ test for test in all_tests if re.search(pattern_re, test['section']) and (not args.number or test['example'] == args.number) ] if args.dump_tests: - print json.dumps(tests, ensure_ascii=False, indent=2) + print(json.dumps(tests, ensure_ascii=False, indent=2)) exit(0) else: skipped = len(all_tests) - len(tests) @@ -132,7 +132,7 @@ if __name__ == "__main__": result_counts = {'pass': 0, 'fail': 0, 'error': 0, 'skip': skipped} for test in tests: do_test(test, args.normalize, result_counts) - print "{pass} passed, {fail} failed, {error} errored, {skip} skipped".format(**result_counts) + print("{pass} passed, {fail} failed, {error} errored, {skip} skipped".format(**result_counts)) if result_counts['fail'] == 0 and result_counts['error'] == 0: exit(0) else: -- cgit v1.2.3