Ver a proveniência

Abstract out public list method tests; implement test_child_methods()

tags/v0.2
Ben Kurtovic há 11 anos
ascendente
cometimento
b8e926a256
1 ficheiros alterados com 74 adições e 71 eliminações
  1. +74
    -71
      tests/test_smart_list.py

+ 74
- 71
tests/test_smart_list.py Ver ficheiro

@@ -28,6 +28,71 @@ from mwparserfromhell.smart_list import SmartList, _ListProxy

class TestSmartList(unittest.TestCase):
"""Test cases for the SmartList class and its child, _ListProxy."""

def _test_list_methods(self, builder):
"""Run tests on the public methods of a list built with *builder*."""
list1 = builder(range(5))
list2 = builder(["foo"])
list3 = builder([("a", 5), ("d", 2), ("b", 8), ("c", 3)])

list1.append(5)
list1.append(1)
list1.append(2)
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2], list1)

self.assertEquals(0, list1.count(6))
self.assertEquals(2, list1.count(1))

list1.extend(range(5, 8))
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)

self.assertEquals(1, list1.index(1))
self.assertEquals(6, list1.index(1, 3))
self.assertEquals(6, list1.index(1, 3, 7))
self.assertRaises(ValueError, list1.index, 1, 3, 5)

list1.insert(0, -1)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
list1.insert(-1, 6.5)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7], list1)
list1.insert(13, 8)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)

self.assertEquals(8, list1.pop())
self.assertEquals(7, list1.pop())
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
self.assertEquals(-1, list1.pop(0))
self.assertEquals(5, list1.pop(5))
self.assertEquals(6.5, list1.pop(-1))
self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
self.assertEquals("foo", list2.pop())
self.assertRaises(IndexError, list2.pop)
self.assertEquals([], list2)

list1.remove(6)
self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5], list1)
list1.remove(1)
self.assertEquals([0, 2, 3, 4, 1, 2, 5], list1)
list1.remove(1)
self.assertEquals([0, 2, 3, 4, 2, 5], list1)
self.assertRaises(ValueError, list1.remove, 1)

list1.reverse()
self.assertEquals([5, 2, 4, 3, 2, 0], list1)

list1.sort()
self.assertEquals([0, 2, 2, 3, 4, 5], list1)
list1.sort(reverse=True)
self.assertEquals([5, 4, 3, 2, 2, 0], list1)
list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y)) # Distance from 3
self.assertEquals([3, 4, 2, 2, 5, 0], list1)
list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y), reverse=True)
self.assertEquals([0, 5, 4, 2, 2, 3], list1)
list3.sort(key=lambda i: i[1])
self.assertEquals([("d", 2), ("c", 3), ("a", 5), ("b", 8)], list3)
list3.sort(key=lambda i: i[1], reverse=True)
self.assertEquals([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)

def test_docs(self):
"""make sure the methods of SmartList/_ListProxy have docstrings"""
methods = ["append", "count", "extend", "index", "insert", "pop",
@@ -221,68 +286,10 @@ class TestSmartList(unittest.TestCase):

def test_parent_methods(self):
"""make sure SmartList's non-magic methods work, like append()"""
list1 = SmartList(range(5))
list2 = SmartList(["foo"])
list3 = SmartList([("a", 5), ("d", 2), ("b", 8), ("c", 3)])

list1.append(5)
list1.append(1)
list1.append(2)
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2], list1)

self.assertEquals(0, list1.count(6))
self.assertEquals(2, list1.count(1))

list1.extend(range(5, 8))
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)

self.assertEquals(1, list1.index(1))
self.assertEquals(6, list1.index(1, 3))
self.assertEquals(6, list1.index(1, 3, 7))
self.assertRaises(ValueError, list1.index, 1, 3, 5)

list1.insert(0, -1)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
list1.insert(-1, 6.5)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7], list1)
list1.insert(100, 8)
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)

self.assertEquals(8, list1.pop())
self.assertEquals(7, list1.pop())
self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
self.assertEquals(-1, list1.pop(0))
self.assertEquals(5, list1.pop(5))
self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5, 6, 6.5], list1)
self.assertEquals("foo", list2.pop())
self.assertRaises(IndexError, list2.pop)
self.assertEquals([], list2)

list1.remove(6.5)
self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
list1.remove(1)
self.assertEquals([0, 2, 3, 4, 1, 2, 5, 6], list1)
list1.remove(1)
self.assertEquals([0, 2, 3, 4, 2, 5, 6], list1)
self.assertRaises(ValueError, list1.remove, 1)

list1.reverse()
self.assertEquals([6, 5, 2, 4, 3, 2, 0], list1)

list1.sort()
self.assertEquals([0, 2, 2, 3, 4, 5, 6], list1)
list1.sort(reverse=True)
self.assertEquals([6, 5, 4, 3, 2, 2, 0], list1)
list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y)) # Distance from 3
self.assertEquals([3, 4, 2, 2, 5, 6, 0], list1)
list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y), reverse=True)
self.assertEquals([6, 0, 5, 4, 2, 2, 3], list1)
list3.sort(key=lambda i: i[1])
self.assertEquals([("d", 2), ("c", 3), ("a", 5), ("b", 8)], list3)
list3.sort(key=lambda i: i[1], reverse=True)
self.assertEquals([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)
self._test_list_methods(lambda L: SmartList(L))

def test_child_magics(self):
"""make sure _ListProxy's magically implemented features work"""
pass
# if py3k:
# __str__
@@ -320,20 +327,16 @@ class TestSmartList(unittest.TestCase):
# __imul__

def test_child_methods(self):
pass
# append
# count
# extend
# index
# insert
# pop
# remove
# reverse
# sort
"""make sure _ListProxy's non-magic methods work, like append()"""
self._test_list_methods(lambda L: SmartList(list(L))[:])
self._test_list_methods(lambda L: SmartList([999] + list(L))[1:])
self._test_list_methods(lambda L: SmartList(list(L) + [999])[:-1])
builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
self._test_list_methods(builder)

def test_influence(self):
"""make sure changes are propagated from parents to children"""
pass
# test whether changes are propogated correctly
# also test whether children that exit scope are removed from parent's map

if __name__ == "__main__":


Carregando…
Cancelar
Guardar