From 9bcd7b8763627c64184b0bf147ec1830fde0a5dc Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 14 Dec 2012 13:31:41 -0500 Subject: Testing framework now runs on Python 2 & 3 unmodified. --- fabfile.py | 3 +-- tests/__init__.py | 20 ++++++++++---------- tests/plugins.py | 4 ++-- tests/test_apis.py | 48 +++++++++++++++++++++++++++--------------------- tests/test_extensions.py | 1 + tests/util.py | 6 +++++- 6 files changed, 46 insertions(+), 36 deletions(-) diff --git a/fabfile.py b/fabfile.py index 16ceb1d..f9333e0 100644 --- a/fabfile.py +++ b/fabfile.py @@ -58,8 +58,7 @@ def build_tests(version=_pyversion[:3]): if version.startswith('3'): # Do 2to3 conversion local('2to3-%s -w -d build/test.%s/markdown' % (version, version)) - local('2to3-%s -w build/test.%s/tests' % (version, version)) - local('2to3-%s -w build/test.%s/run-tests.py' % (version, version)) + def generate_test(file): """ Generate a given test. """ diff --git a/tests/__init__.py b/tests/__init__.py index 4722e33..bb56bd4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,4 @@ -from __future__ import with_statement + import os import markdown import codecs @@ -6,11 +6,11 @@ import difflib try: import nose except ImportError: - raise ImportError, "The nose testing framework is required to run " \ + raise ImportError("The nose testing framework is required to run " \ "Python-Markdown tests. Run `easy_install nose` " \ - "to install the latest version." -import util -from plugins import HtmlOutput, Markdown + "to install the latest version.") +from . import util +from .plugins import HtmlOutput, Markdown try: import tidy except ImportError: @@ -84,7 +84,7 @@ class CheckSyntax(object): """ Compare expected output to actual output and report result. """ cfg_section = get_section(file, config) if config.get(cfg_section, 'skip'): - raise nose.plugins.skip.SkipTest, 'Test skipped per config.' + raise nose.plugins.skip.SkipTest('Test skipped per config.') input_file = file + config.get(cfg_section, 'input_ext') with codecs.open(input_file, encoding="utf-8") as f: input = f.read() @@ -99,7 +99,7 @@ class CheckSyntax(object): output = normalize(output) elif config.get(cfg_section, 'normalize'): # Tidy is not available. Skip this test. - raise nose.plugins.skip.SkipTest, 'Test skipped. Tidy not available in system.' + raise nose.plugins.skip.SkipTest('Test skipped. Tidy not available in system.') diff = [l for l in difflib.unified_diff(expected_output.splitlines(True), output.splitlines(True), output_file, @@ -125,17 +125,17 @@ def generate(file, config): """ Write expected output file for given input. """ cfg_section = get_section(file, config) if config.get(cfg_section, 'skip'): - print 'Skipping:', file + print('Skipping:', file) return None input_file = file + config.get(cfg_section, 'input_ext') output_file = file + config.get(cfg_section, 'output_ext') if not os.path.isfile(output_file) or \ os.path.getmtime(output_file) < os.path.getmtime(input_file): - print 'Generating:', file + print('Generating:', file) markdown.markdownFromFile(input=input_file, output=output_file, encoding='utf-8', **get_args(file, config)) else: - print 'Already up-to-date:', file + print('Already up-to-date:', file) def generate_all(): """ Generate expected output for all outdated tests. """ diff --git a/tests/plugins.py b/tests/plugins.py index 6e72024..bbeed60 100644 --- a/tests/plugins.py +++ b/tests/plugins.py @@ -1,5 +1,5 @@ import traceback -from util import MarkdownSyntaxError +from .util import MarkdownSyntaxError from nose.plugins import Plugin from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin @@ -73,7 +73,7 @@ or "")) self.html.extend(['FAILED (', 'failures=%d ' % len(result.failures), 'errors=%d' % len(result.errors)]) - for cls in result.errorClasses.keys(): + for cls in list(result.errorClasses.keys()): storage, label, isfail = result.errorClasses[cls] if len(storage): self.html.append(' %ss=%d' % (label, len(storage))) diff --git a/tests/test_apis.py b/tests/test_apis.py index b39664a..e0f7a03 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -7,6 +7,7 @@ Tests of the various APIs with the python markdown lib. """ +from __future__ import unicode_literals import unittest import os import sys @@ -14,6 +15,8 @@ import types import markdown import warnings +PY3 = sys.version_info[0] == 3 + class TestMarkdownBasics(unittest.TestCase): """ Tests basics of the Markdown class. """ @@ -140,51 +143,51 @@ class TestOrderedDict(unittest.TestCase): def testValues(self): """ Test output of OrderedDict.values(). """ - self.assertEqual(self.odict.values(), ['This', 'a', 'self', 'test']) + self.assertEqual(list(self.odict.values()), ['This', 'a', 'self', 'test']) def testKeys(self): """ Test output of OrderedDict.keys(). """ - self.assertEqual(self.odict.keys(), + self.assertEqual(list(self.odict.keys()), ['first', 'third', 'fourth', 'fifth']) def testItems(self): """ Test output of OrderedDict.items(). """ - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('third', 'a'), ('fourth', 'self'), ('fifth', 'test')]) def testAddBefore(self): """ Test adding an OrderedDict item before a given key. """ self.odict.add('second', 'is', 'first') - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('second', 'is'), ('third', 'a'), ('fourth', 'self'), ('fifth', 'test')]) def testAddAfterEnd(self): """ Test adding an OrderedDict item after the last key. """ self.odict.add('sixth', '.', '>fifth') - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('third', 'a'), ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')]) def testAdd_begin(self): """ Test adding an OrderedDict item using "_begin". """ self.odict.add('zero', 'CRAZY', '_begin') - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('zero', 'CRAZY'), ('first', 'This'), ('third', 'a'), ('fourth', 'self'), ('fifth', 'test')]) def testAdd_end(self): """ Test adding an OrderedDict item using "_end". """ self.odict.add('sixth', '.', '_end') - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('third', 'a'), ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')]) @@ -196,20 +199,20 @@ class TestOrderedDict(unittest.TestCase): def testDeleteItem(self): """ Test deletion of an OrderedDict item. """ del self.odict['fourth'] - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('third', 'a'), ('fifth', 'test')]) def testChangeValue(self): """ Test OrderedDict change value. """ self.odict['fourth'] = 'CRAZY' - self.assertEqual(self.odict.items(), + self.assertEqual(list(self.odict.items()), [('first', 'This'), ('third', 'a'), ('fourth', 'CRAZY'), ('fifth', 'test')]) def testChangeOrder(self): """ Test OrderedDict change order. """ self.odict.link('fourth', '

some text

') @@ -341,7 +347,7 @@ class testAtomicString(unittest.TestCase): """ Test that a simple AtomicString is not parsed. """ tree = markdown.util.etree.Element('div') p = markdown.util.etree.SubElement(tree, 'p') - p.text = markdown.util.AtomicString(u'some *text*') + p.text = markdown.util.AtomicString('some *text*') new = self.inlineprocessor.run(tree) self.assertEqual(markdown.serializers.to_html_string(new), '

some *text*

') @@ -350,16 +356,16 @@ class testAtomicString(unittest.TestCase): """ Test that a nested AtomicString is not parsed. """ tree = markdown.util.etree.Element('div') p = markdown.util.etree.SubElement(tree, 'p') - p.text = markdown.util.AtomicString(u'*some* ') + p.text = markdown.util.AtomicString('*some* ') span1 = markdown.util.etree.SubElement(p, 'span') - span1.text = markdown.util.AtomicString(u'*more* ') + span1.text = markdown.util.AtomicString('*more* ') span2 = markdown.util.etree.SubElement(span1, 'span') - span2.text = markdown.util.AtomicString(u'*text* ') + span2.text = markdown.util.AtomicString('*text* ') span3 = markdown.util.etree.SubElement(span2, 'span') - span3.text = markdown.util.AtomicString(u'*here*') - span3.tail = markdown.util.AtomicString(u' *to*') - span2.tail = markdown.util.AtomicString(u' *test*') - span1.tail = markdown.util.AtomicString(u' *with*') + span3.text = markdown.util.AtomicString('*here*') + span3.tail = markdown.util.AtomicString(' *to*') + span2.tail = markdown.util.AtomicString(' *test*') + span1.tail = markdown.util.AtomicString(' *with*') new = self.inlineprocessor.run(tree) self.assertEqual(markdown.serializers.to_html_string(new), '

*some* *more* *text* *here* ' diff --git a/tests/test_extensions.py b/tests/test_extensions.py index b10414e..7dab60a 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -7,6 +7,7 @@ continue to work as advertised. This used to be accomplished by doctests. """ +from __future__ import unicode_literals import unittest import markdown diff --git a/tests/util.py b/tests/util.py index 6690eed..bbf7aea 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,4 +1,8 @@ -from ConfigParser import SafeConfigParser +import sys +if sys.version_info[0] == 3: + from configparser import SafeConfigParser +else: + from ConfigParser import SafeConfigParser class MarkdownSyntaxError(Exception): pass -- cgit v1.2.3