Browse Source

test_types(), test_other_magics(); add range to compat

tags/v0.2
Ben Kurtovic 11 years ago
parent
commit
6e748004d1
2 changed files with 63 additions and 7 deletions
  1. +2
    -0
      mwparserfromhell/compat.py
  2. +61
    -7
      tests/test_string_mixin.py

+ 2
- 0
mwparserfromhell/compat.py View File

@@ -16,6 +16,7 @@ if py3k:
bytes = bytes bytes = bytes
str = str str = str
basestring = str basestring = str
range = range
maxsize = sys.maxsize maxsize = sys.maxsize
import html.entities as htmlentities import html.entities as htmlentities
from io import StringIO from io import StringIO
@@ -24,6 +25,7 @@ else:
bytes = str bytes = str
str = unicode str = unicode
basestring = basestring basestring = basestring
range = xrange
maxsize = sys.maxint maxsize = sys.maxint
import htmlentitydefs as htmlentities import htmlentitydefs as htmlentities
from StringIO import StringIO from StringIO import StringIO


+ 61
- 7
tests/test_string_mixin.py View File

@@ -21,9 +21,10 @@
# SOFTWARE. # SOFTWARE.


from __future__ import unicode_literals from __future__ import unicode_literals
from types import GeneratorType
import unittest import unittest


from mwparserfromhell.compat import py3k, str
from mwparserfromhell.compat import bytes, py3k, range, str
from mwparserfromhell.string_mixin import StringMixIn from mwparserfromhell.string_mixin import StringMixIn


class _FakeString(StringMixIn): class _FakeString(StringMixIn):
@@ -55,7 +56,20 @@ class TestStringMixIn(unittest.TestCase):


def test_types(self): def test_types(self):
"""make sure StringMixIns convert to different types correctly""" """make sure StringMixIns convert to different types correctly"""
pass
fstr = _FakeString("fake string")
self.assertEquals(str(fstr), "fake string")
self.assertEquals(bytes(fstr), b"fake string")
if py3k:
self.assertEquals(repr(fstr), "'fake string'")
else:
self.assertEquals(repr(fstr), b"u'fake string'")

self.assertIsInstance(str(fstr), str)
self.assertIsInstance(bytes(fstr), bytes)
if py3k:
self.assertIsInstance(repr(fstr), str)
else:
self.assertIsInstance(repr(fstr), bytes)


def test_comparisons(self): def test_comparisons(self):
"""make sure comparison operators work""" """make sure comparison operators work"""
@@ -93,13 +107,53 @@ class TestStringMixIn(unittest.TestCase):
self.assertFalse(str1 < str5) self.assertFalse(str1 < str5)
self.assertFalse(str1 <= str5) self.assertFalse(str1 <= str5)


def test_operators(self):
"""make sure string addition and multiplication work"""
pass

def test_other_magics(self): def test_other_magics(self):
"""test other magically implemented features, like len() and iter()""" """test other magically implemented features, like len() and iter()"""
pass
str1 = _FakeString("fake string")
str2 = _FakeString("")
expected = ["f", "a", "k", "e", " ", "s", "t", "r", "i", "n", "g"]

self.assertTrue(str1)
self.assertFalse(str2)
self.assertEquals(11, len(str1))
self.assertEquals(0, len(str2))

out = []
for ch in str1:
out.append(ch)
self.assertEquals(expected, out)

out = []
for ch in str2:
out.append(ch)
self.assertEquals([], out)

gen1 = iter(str1)
gen2 = iter(str2)
self.assertIsInstance(gen1, GeneratorType)
self.assertIsInstance(gen2, GeneratorType)

out = []
for i in range(len(str1)):
out.append(gen1.next())
self.assertRaises(StopIteration, gen1.next)
self.assertEquals(expected, out)
self.assertRaises(StopIteration, gen2.next)

self.assertEquals("f", str1[0])
self.assertEquals(" ", str1[4])
self.assertEquals("g", str1[10])
self.assertEquals("n", str1[-2])
self.assertRaises(IndexError, lambda: str1[11])
self.assertRaises(IndexError, lambda: str2[0])

self.assertTrue("k" in str1)
self.assertTrue("fake" in str1)
self.assertTrue("str" in str1)
self.assertTrue("" in str1)
self.assertTrue("" in str2)
self.assertFalse("real" in str1)
self.assertFalse("s" in str2)


def test_other_methods(self): def test_other_methods(self):
"""test the remaining non-magic methods of StringMixIn""" """test the remaining non-magic methods of StringMixIn"""


Loading…
Cancel
Save