Sfoglia il codice sorgente

Some tests for SmartLists; __reversed__ in StringMixIn

tags/v0.2
Ben Kurtovic 11 anni fa
parent
commit
ff51d7f5e5
3 ha cambiato i file con 169 aggiunte e 1 eliminazioni
  1. +3
    -0
      mwparserfromhell/string_mixin.py
  2. +163
    -1
      tests/test_smart_list.py
  3. +3
    -0
      tests/test_string_mixin.py

+ 3
- 0
mwparserfromhell/string_mixin.py Vedi File

@@ -113,6 +113,9 @@ class StringMixIn(object):
def __getitem__(self, key):
return self.__unicode__()[key]

def __reversed__(self):
return reversed(self.__unicode__())

def __contains__(self, item):
if isinstance(item, StringMixIn):
return str(item) in self.__unicode__()


+ 163
- 1
tests/test_smart_list.py Vedi File

@@ -23,6 +23,7 @@
from __future__ import unicode_literals
import unittest

from mwparserfromhell.compat import py3k
from mwparserfromhell.smart_list import SmartList, _ListProxy

class TestSmartList(unittest.TestCase):
@@ -39,7 +40,7 @@ class TestSmartList(unittest.TestCase):
self.assertEquals(expected, listproxy_doc)

def test_doctest(self):
"""make sure a test embedded in SmartList's docstring passes"""
"""make sure the test embedded in SmartList's docstring passes"""
parent = SmartList([0, 1, 2, 3])
self.assertEquals([0, 1, 2, 3], parent)
child = parent[2:]
@@ -48,5 +49,166 @@ class TestSmartList(unittest.TestCase):
self.assertEquals([2, 3, 4], child)
self.assertEquals([0, 1, 2, 3, 4], parent)

def test_parent_magics(self):
"""make sure magically implemented SmartList features work"""
# __getitem__
# __setitem__
# __delitem__
# if not py3k:
# __getslice__
# __setslice__
# __delslice__
# __add__
# __radd__
# __iadd__

def test_parent_unaffected_magics(self):
"""sanity checks against SmartList features that were not modified"""
list1 = SmartList([0, 1, 2, 3, "one", "two"])
list2 = SmartList([])
list3 = SmartList([0, 2, 3, 4])
list4 = SmartList([0, 1, 2])

if py3k:
self.assertEquals("[0, 1, 2, 3, 'one', 'two']", str(list1))
self.assertEquals(b"[0, 1, 2, 3, 'one', 'two']", bytes(list1))
self.assertEquals("[0, 1, 2, 3, 'one', 'two']", repr(list1))
else:
self.assertEquals("[0, 1, 2, 3, u'one', u'two']", unicode(list1))
self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", str(list1))
self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", repr(list1))

self.assertTrue(list1 < list3)
self.assertTrue(list1 <= list3)
self.assertFalse(list1 == list3)
self.assertTrue(list1 != list3)
self.assertFalse(list1 > list3)
self.assertFalse(list1 >= list3)

other1 = [0, 2, 3, 4]
self.assertTrue(list1 < other1)
self.assertTrue(list1 <= other1)
self.assertFalse(list1 == other1)
self.assertTrue(list1 != other1)
self.assertFalse(list1 > other1)
self.assertFalse(list1 >= other1)

other2 = [0, 0, 1, 2]
self.assertFalse(list1 < other2)
self.assertFalse(list1 <= other2)
self.assertFalse(list1 == other2)
self.assertTrue(list1 != other2)
self.assertTrue(list1 > other2)
self.assertTrue(list1 >= other2)

other3 = [0, 1, 2, 3, "one", "two"]
self.assertFalse(list1 < other3)
self.assertTrue(list1 <= other3)
self.assertTrue(list1 == other3)
self.assertFalse(list1 != other3)
self.assertFalse(list1 > other3)
self.assertTrue(list1 >= other3)

self.assertTrue(bool(list1))
self.assertFalse(bool(list2))

self.assertEquals(6, len(list1))
self.assertEquals(0, len(list2))

out = []
for obj in list1:
out.append(obj)
self.assertEquals([0, 1, 2, 3, "one", "two"], out)

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

gen1 = iter(list1)
out = []
for i in range(len(list1)):
out.append(gen1.next())
self.assertRaises(StopIteration, gen1.next)
self.assertEquals([0, 1, 2, 3, "one", "two"], out)
gen2 = iter(list2)
self.assertRaises(StopIteration, gen2.next)

self.assertEquals(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
self.assertEquals([], list(reversed(list2)))

self.assertTrue("one" in list1)
self.assertTrue(3 in list1)
self.assertFalse(10 in list1)
self.assertFalse(0 in list2)

self.assertEquals([], list2 * 5)
self.assertEquals([], 5 * list2)
self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
list4 *= 2
self.assertEquals([0, 1, 2, 0, 1, 2], list4)

def test_parent_methods(self):
# append
# count
# extend
# index
# insert
# pop
# remove
# reverse
# sort

def test_child_magics(self):
# if py3k:
# __str__
# __bytes__
# else:
# __unicode__
# __str__
# __repr__
# __lt__
# __le__
# __eq__
# __ne__
# __gt__
# __ge__
# if py3k:
# __bool__
# else:
# __nonzero__
# __len__
# __getitem__
# __setitem__
# __delitem__
# __iter__
# __reversed__
# __contains__
# if not py3k:
# __getslice__
# __setslice__
# __delslice__
# __add__
# __radd__
# __iadd__
# __mul__
# __rmul__
# __imul__

def test_child_methods(self):
# append
# count
# extend
# index
# insert
# pop
# remove
# reverse
# sort

def test_influence(self):
pass

if __name__ == "__main__":
unittest.main(verbosity=2)

+ 3
- 0
tests/test_string_mixin.py Vedi File

@@ -143,6 +143,9 @@ class TestStringMixIn(unittest.TestCase):
self.assertEquals(expected, out)
self.assertRaises(StopIteration, gen2.next)

self.assertEquals("gnirts ekaf", "".join(list(reversed(str1))))
self.assertEquals([], list(reversed(str2)))

self.assertEquals("f", str1[0])
self.assertEquals(" ", str1[4])
self.assertEquals("g", str1[10])


Caricamento…
Annulla
Salva