Make test script Python 3-compatible

This commit is contained in:
Tzu-ping Chung 2014-09-09 17:45:24 +08:00
parent 9a2166db3c
commit 855bc34742
2 changed files with 21 additions and 5 deletions

View file

@ -46,7 +46,7 @@ src/html_blocks.c: html_block_names.gperf
# Testing
test: hoedown
test/runner.py
python test/runner.py
test-pl: hoedown
perl test/MarkdownTest_1.0.3/MarkdownTest.pl \

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
from __future__ import print_function
import difflib
import json
import os
@ -20,6 +20,17 @@ CONFIG_PATH = os.path.join(TEST_ROOT, 'config.json')
SLUGIFY_PATTERN = re.compile(r'\W')
def with_metaclass(meta, *bases):
"""Metaclass injection utility from six.
See: https://pythonhosted.org/six/
"""
class metaclass(meta):
def __new__(cls, name, this_bases, d):
return meta(name, bases, d)
return type.__new__(metaclass, 'temporary_class', (), {})
class TestFailed(AssertionError):
def __init__(self, name, expected, got):
super(TestFailed, self).__init__(self)
@ -55,6 +66,11 @@ def _test_func(test_case):
expected_tidy_proc.wait()
expected = expected_tidy_proc.stdout.read().strip()
# Cleanup.
hoedown_proc.stdout.close()
got_tidy_proc.stdout.close()
expected_tidy_proc.stdout.close()
try:
assert expected == got
except AssertionError:
@ -65,7 +81,7 @@ def _make_test(test_case):
return lambda self: _test_func(test_case)
class MarkdownTestCaseMeta(type):
class MarkdownTestsMeta(type):
"""Meta class for ``MarkdownTestCase`` to inject test cases on the fly.
"""
def __new__(meta, name, bases, attrs):
@ -85,8 +101,8 @@ class MarkdownTestCaseMeta(type):
return type.__new__(meta, name, bases, attrs)
class MarkdownTestCase(unittest.TestCase):
__metaclass__ = MarkdownTestCaseMeta
class MarkdownTests(with_metaclass(MarkdownTestsMeta, unittest.TestCase)):
pass
if __name__ == '__main__':