From 1b69b5e882944abf0909816d2daed76c37cbe9c8 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 30 Mar 2013 16:46:39 -0400 Subject: [PATCH] Moving compat stuff exclusively for unit tests to its own file. --- mwparserfromhell/compat.py | 7 ------- tests/compat.py | 20 ++++++++++++++++++++ tests/test_docs.py | 4 +++- tests/test_parser.py | 3 ++- tests/test_smart_list.py | 4 +++- tests/test_string_mixin.py | 4 +++- 6 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 tests/compat.py diff --git a/mwparserfromhell/compat.py b/mwparserfromhell/compat.py index 34870e6..bb81513 100755 --- a/mwparserfromhell/compat.py +++ b/mwparserfromhell/compat.py @@ -16,21 +16,14 @@ if py3k: bytes = bytes str = str basestring = str - range = range maxsize = sys.maxsize import html.entities as htmlentities - from io import StringIO - from urllib.parse import urlencode - from urllib.request import urlopen else: bytes = str str = unicode basestring = basestring - range = xrange maxsize = sys.maxint import htmlentitydefs as htmlentities - from StringIO import StringIO - from urllib import urlencode, urlopen del sys diff --git a/tests/compat.py b/tests/compat.py new file mode 100644 index 0000000..8bed40e --- /dev/null +++ b/tests/compat.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +""" +Serves the same purpose as mwparserfromhell.compat, but only for objects +required by unit tests. This avoids unnecessary imports (like urllib) within +the main library. +""" + +from mwparserfromhell.compat import py3k + +if py3k: + range = range + from io import StringIO + from urllib.parse import urlencode + from urllib.request import urlopen + +else: + range = xrange + from StringIO import StringIO + from urllib import urlencode, urlopen diff --git a/tests/test_docs.py b/tests/test_docs.py index 3b23bb7..8d95c47 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -25,7 +25,9 @@ import json import unittest import mwparserfromhell -from mwparserfromhell.compat import py3k, str, StringIO, urlencode, urlopen +from mwparserfromhell.compat import py3k, str + +from .compat import StringIO, urlencode, urlopen class TestDocs(unittest.TestCase): """Integration test cases for mwparserfromhell's documentation.""" diff --git a/tests/test_parser.py b/tests/test_parser.py index 4f718c8..1c37a85 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -24,12 +24,13 @@ from __future__ import unicode_literals import unittest from mwparserfromhell import parser -from mwparserfromhell.compat import range from mwparserfromhell.nodes import Template, Text, Wikilink from mwparserfromhell.nodes.extras import Parameter from mwparserfromhell.smart_list import SmartList from mwparserfromhell.wikicode import Wikicode +from .compat import range + class TestParser(unittest.TestCase): """Tests for the Parser class itself, which tokenizes and builds nodes.""" diff --git a/tests/test_smart_list.py b/tests/test_smart_list.py index 3423bb7..25df555 100644 --- a/tests/test_smart_list.py +++ b/tests/test_smart_list.py @@ -23,9 +23,11 @@ from __future__ import unicode_literals import unittest -from mwparserfromhell.compat import py3k, range +from mwparserfromhell.compat import py3k from mwparserfromhell.smart_list import SmartList, _ListProxy +from .compat import range + class TestSmartList(unittest.TestCase): """Test cases for the SmartList class and its child, _ListProxy.""" diff --git a/tests/test_string_mixin.py b/tests/test_string_mixin.py index 6d10609..306f2fd 100644 --- a/tests/test_string_mixin.py +++ b/tests/test_string_mixin.py @@ -25,9 +25,11 @@ from sys import getdefaultencoding from types import GeneratorType import unittest -from mwparserfromhell.compat import bytes, py3k, range, str +from mwparserfromhell.compat import bytes, py3k, str from mwparserfromhell.string_mixin import StringMixIn +from .compat import range + class _FakeString(StringMixIn): def __init__(self, data): self._data = data