瀏覽代碼

Replace deprecated alias assertEquals() with assertEqual().

tags/v0.2
Ben Kurtovic 11 年之前
父節點
當前提交
9e26264d6b
共有 3 個檔案被更改,包括 248 行新增248 行删除
  1. +122
    -122
      tests/test_smart_list.py
  2. +114
    -114
      tests/test_string_mixin.py
  3. +12
    -12
      tests/test_tokens.py

+ 122
- 122
tests/test_smart_list.py 查看文件

@@ -39,80 +39,80 @@ class TestSmartList(unittest.TestCase):
list1 = builder([0, 1, 2, 3, "one", "two"])
list2 = builder(list(range(10)))

self.assertEquals(1, list1[1])
self.assertEquals("one", list1[-2])
self.assertEquals([2, 3], list1[2:4])
self.assertEqual(1, list1[1])
self.assertEqual("one", list1[-2])
self.assertEqual([2, 3], list1[2:4])
self.assertRaises(IndexError, lambda: list1[6])
self.assertRaises(IndexError, lambda: list1[-7])

self.assertEquals([0, 1, 2], list1[:3])
self.assertEquals([0, 1, 2, 3, "one", "two"], list1[:])
self.assertEquals([3, "one", "two"], list1[3:])
self.assertEquals(["one", "two"], list1[-2:])
self.assertEquals([0, 1], list1[:-4])
self.assertEquals([], list1[6:])
self.assertEquals([], list1[4:2])
self.assertEquals([0, 2, "one"], list1[0:5:2])
self.assertEquals([0, 2], list1[0:-3:2])
self.assertEquals([0, 1, 2, 3, "one", "two"], list1[::])
self.assertEquals([2, 3, "one", "two"], list1[2::])
self.assertEquals([0, 1, 2, 3], list1[:4:])
self.assertEquals([2, 3], list1[2:4:])
self.assertEquals([0, 2, 4, 6, 8], list2[::2])
self.assertEquals([2, 5, 8], list2[2::3])
self.assertEquals([0, 3], list2[:6:3])
self.assertEquals([2, 5, 8], list2[-8:9:3])
self.assertEquals([], list2[100000:1000:-100])
self.assertEqual([0, 1, 2], list1[:3])
self.assertEqual([0, 1, 2, 3, "one", "two"], list1[:])
self.assertEqual([3, "one", "two"], list1[3:])
self.assertEqual(["one", "two"], list1[-2:])
self.assertEqual([0, 1], list1[:-4])
self.assertEqual([], list1[6:])
self.assertEqual([], list1[4:2])
self.assertEqual([0, 2, "one"], list1[0:5:2])
self.assertEqual([0, 2], list1[0:-3:2])
self.assertEqual([0, 1, 2, 3, "one", "two"], list1[::])
self.assertEqual([2, 3, "one", "two"], list1[2::])
self.assertEqual([0, 1, 2, 3], list1[:4:])
self.assertEqual([2, 3], list1[2:4:])
self.assertEqual([0, 2, 4, 6, 8], list2[::2])
self.assertEqual([2, 5, 8], list2[2::3])
self.assertEqual([0, 3], list2[:6:3])
self.assertEqual([2, 5, 8], list2[-8:9:3])
self.assertEqual([], list2[100000:1000:-100])

list1[3] = 100
self.assertEquals(100, list1[3])
self.assertEqual(100, list1[3])
list1[-3] = 101
self.assertEquals([0, 1, 2, 101, "one", "two"], list1)
self.assertEqual([0, 1, 2, 101, "one", "two"], list1)
list1[5:] = [6, 7, 8]
self.assertEquals([6, 7, 8], list1[5:])
self.assertEquals([0, 1, 2, 101, "one", 6, 7, 8], list1)
self.assertEqual([6, 7, 8], list1[5:])
self.assertEqual([0, 1, 2, 101, "one", 6, 7, 8], list1)
list1[2:4] = [-1, -2, -3, -4, -5]
self.assertEquals([0, 1, -1, -2, -3, -4, -5, "one", 6, 7, 8], list1)
self.assertEqual([0, 1, -1, -2, -3, -4, -5, "one", 6, 7, 8], list1)
list1[0:-3] = [99]
self.assertEquals([99, 6, 7, 8], list1)
self.assertEqual([99, 6, 7, 8], list1)
list2[0:6:2] = [100, 102, 104]
self.assertEquals([100, 1, 102, 3, 104, 5, 6, 7, 8, 9], list2)
self.assertEqual([100, 1, 102, 3, 104, 5, 6, 7, 8, 9], list2)
list2[::3] = [200, 203, 206, 209]
self.assertEquals([200, 1, 102, 203, 104, 5, 206, 7, 8, 209], list2)
self.assertEqual([200, 1, 102, 203, 104, 5, 206, 7, 8, 209], list2)
list2[::] = range(7)
self.assertEquals([0, 1, 2, 3, 4, 5, 6], list2)
self.assertEqual([0, 1, 2, 3, 4, 5, 6], list2)
self.assertRaises(ValueError, assign, list2, 0, 5, 2,
[100, 102, 104, 106])

del list2[2]
self.assertEquals([0, 1, 3, 4, 5, 6], list2)
self.assertEqual([0, 1, 3, 4, 5, 6], list2)
del list2[-3]
self.assertEquals([0, 1, 3, 5, 6], list2)
self.assertEqual([0, 1, 3, 5, 6], list2)
self.assertRaises(IndexError, delete, list2, 100)
self.assertRaises(IndexError, delete, list2, -6)
list2[:] = range(10)
del list2[3:6]
self.assertEquals([0, 1, 2, 6, 7, 8, 9], list2)
self.assertEqual([0, 1, 2, 6, 7, 8, 9], list2)
del list2[-2:]
self.assertEquals([0, 1, 2, 6, 7], list2)
self.assertEqual([0, 1, 2, 6, 7], list2)
del list2[:2]
self.assertEquals([2, 6, 7], list2)
self.assertEqual([2, 6, 7], list2)
list2[:] = range(10)
del list2[2:8:2]
self.assertEquals([0, 1, 3, 5, 7, 8, 9], list2)
self.assertEqual([0, 1, 3, 5, 7, 8, 9], list2)

def _test_add_radd_iadd(self, builder):
"""Run tests on __r/i/add__ of a list built with *builder*."""
list1 = builder(range(5))
list2 = builder(range(5, 10))
self.assertEquals([0, 1, 2, 3, 4, 5, 6], list1 + [5, 6])
self.assertEquals([0, 1, 2, 3, 4], list1)
self.assertEquals(list(range(10)), list1 + list2)
self.assertEquals([-2, -1, 0, 1, 2, 3, 4], [-2, -1] + list1)
self.assertEquals([0, 1, 2, 3, 4], list1)
self.assertEqual([0, 1, 2, 3, 4, 5, 6], list1 + [5, 6])
self.assertEqual([0, 1, 2, 3, 4], list1)
self.assertEqual(list(range(10)), list1 + list2)
self.assertEqual([-2, -1, 0, 1, 2, 3, 4], [-2, -1] + list1)
self.assertEqual([0, 1, 2, 3, 4], list1)
list1 += ["foo", "bar", "baz"]
self.assertEquals([0, 1, 2, 3, 4, "foo", "bar", "baz"], list1)
self.assertEqual([0, 1, 2, 3, 4, "foo", "bar", "baz"], list1)

def _test_other_magic_methods(self, builder):
"""Run tests on other magic methods of a list built with *builder*."""
@@ -122,13 +122,13 @@ class TestSmartList(unittest.TestCase):
list4 = builder([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))
self.assertEqual("[0, 1, 2, 3, 'one', 'two']", str(list1))
self.assertEqual(b"[0, 1, 2, 3, 'one', 'two']", bytes(list1))
self.assertEqual("[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.assertEqual("[0, 1, 2, 3, u'one', u'two']", unicode(list1))
self.assertEqual(b"[0, 1, 2, 3, u'one', u'two']", str(list1))
self.assertEqual(b"[0, 1, 2, 3, u'one', u'two']", repr(list1))

self.assertTrue(list1 < list3)
self.assertTrue(list1 <= list3)
@@ -164,42 +164,42 @@ class TestSmartList(unittest.TestCase):
self.assertTrue(bool(list1))
self.assertFalse(bool(list2))

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

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

out = []
for ch in list2:
out.append(ch)
self.assertEquals([], out)
self.assertEqual([], 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)
self.assertEqual([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.assertEqual(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
self.assertEqual([], 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)
self.assertEqual([], list2 * 5)
self.assertEqual([], 5 * list2)
self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
list4 *= 2
self.assertEquals([0, 1, 2, 0, 1, 2], list4)
self.assertEqual([0, 1, 2, 0, 1, 2], list4)

def _test_list_methods(self, builder):
"""Run tests on the public methods of a list built with *builder*."""
@@ -210,60 +210,60 @@ class TestSmartList(unittest.TestCase):
list1.append(5)
list1.append(1)
list1.append(2)
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2], list1)
self.assertEqual([0, 1, 2, 3, 4, 5, 1, 2], list1)

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

list1.extend(range(5, 8))
self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
self.assertEqual([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.assertEqual(1, list1.index(1))
self.assertEqual(6, list1.index(1, 3))
self.assertEqual(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)
self.assertEqual([-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)
self.assertEqual([-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.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)
self.assertEqual(8, list1.pop())
self.assertEqual(7, list1.pop())
self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
self.assertEqual(-1, list1.pop(0))
self.assertEqual(5, list1.pop(5))
self.assertEqual(6.5, list1.pop(-1))
self.assertEqual([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
self.assertEqual("foo", list2.pop())
self.assertRaises(IndexError, list2.pop)
self.assertEquals([], list2)
self.assertEqual([], list2)

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

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

list1.sort()
self.assertEquals([0, 2, 2, 3, 4, 5], list1)
self.assertEqual([0, 2, 2, 3, 4, 5], list1)
list1.sort(reverse=True)
self.assertEquals([5, 4, 3, 2, 2, 0], list1)
self.assertEqual([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)
self.assertEqual([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)
self.assertEqual([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)
self.assertEqual([("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.assertEqual([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)

def test_docs(self):
"""make sure the methods of SmartList/_ListProxy have docstrings"""
@@ -273,18 +273,18 @@ class TestSmartList(unittest.TestCase):
expected = getattr(list, meth).__doc__
smartlist_doc = getattr(SmartList, meth).__doc__
listproxy_doc = getattr(_ListProxy, meth).__doc__
self.assertEquals(expected, smartlist_doc)
self.assertEquals(expected, listproxy_doc)
self.assertEqual(expected, smartlist_doc)
self.assertEqual(expected, listproxy_doc)

def test_doctest(self):
"""make sure the test embedded in SmartList's docstring passes"""
parent = SmartList([0, 1, 2, 3])
self.assertEquals([0, 1, 2, 3], parent)
self.assertEqual([0, 1, 2, 3], parent)
child = parent[2:]
self.assertEquals([2, 3], child)
self.assertEqual([2, 3], child)
child.append(4)
self.assertEquals([2, 3, 4], child)
self.assertEquals([0, 1, 2, 3, 4], parent)
self.assertEqual([2, 3, 4], child)
self.assertEqual([0, 1, 2, 3, 4], parent)

def test_parent_get_set_del(self):
"""make sure SmartList's getitem/setitem/delitem work"""
@@ -343,46 +343,46 @@ class TestSmartList(unittest.TestCase):
parent.append(6)
child1.append(7)
child2.append(4.5)
self.assertEquals([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
self.assertEquals([2, 3, 4, 4.5, 5, 6, 7], child1)
self.assertEquals([2, 3, 4, 4.5], child2)
self.assertEqual([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
self.assertEqual([2, 3, 4, 4.5, 5, 6, 7], child1)
self.assertEqual([2, 3, 4, 4.5], child2)

parent.insert(0, -1)
parent.insert(4, 2.5)
parent.insert(10, 6.5)
self.assertEquals([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
self.assertEquals([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
self.assertEquals([2, 2.5, 3, 4, 4.5], child2)
self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
self.assertEqual([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
self.assertEqual([2, 2.5, 3, 4, 4.5], child2)

self.assertEquals(7, parent.pop())
self.assertEquals(6.5, child1.pop())
self.assertEquals(4.5, child2.pop())
self.assertEquals([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
self.assertEquals([2, 2.5, 3, 4, 5, 6], child1)
self.assertEquals([2, 2.5, 3, 4], child2)
self.assertEqual(7, parent.pop())
self.assertEqual(6.5, child1.pop())
self.assertEqual(4.5, child2.pop())
self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
self.assertEqual([2, 2.5, 3, 4, 5, 6], child1)
self.assertEqual([2, 2.5, 3, 4], child2)

parent.remove(-1)
child1.remove(2.5)
self.assertEquals([0, 1, 2, 3, 4, 5, 6], parent)
self.assertEquals([2, 3, 4, 5, 6], child1)
self.assertEquals([2, 3, 4], child2)
self.assertEqual([0, 1, 2, 3, 4, 5, 6], parent)
self.assertEqual([2, 3, 4, 5, 6], child1)
self.assertEqual([2, 3, 4], child2)

self.assertEquals(0, parent.pop(0))
self.assertEquals([1, 2, 3, 4, 5, 6], parent)
self.assertEquals([2, 3, 4, 5, 6], child1)
self.assertEquals([2, 3, 4], child2)
self.assertEqual(0, parent.pop(0))
self.assertEqual([1, 2, 3, 4, 5, 6], parent)
self.assertEqual([2, 3, 4, 5, 6], child1)
self.assertEqual([2, 3, 4], child2)

child2.reverse()
self.assertEquals([1, 4, 3, 2, 5, 6], parent)
self.assertEquals([4, 3, 2, 5, 6], child1)
self.assertEquals([4, 3, 2], child2)
self.assertEqual([1, 4, 3, 2, 5, 6], parent)
self.assertEqual([4, 3, 2, 5, 6], child1)
self.assertEqual([4, 3, 2], child2)

parent.extend([7, 8])
child1.extend([8.1, 8.2])
child2.extend([1.9, 1.8])
self.assertEquals([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
self.assertEquals([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
self.assertEquals([4, 3, 2, 1.9, 1.8], child2)
self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
self.assertEqual([4, 3, 2, 1.9, 1.8], child2)

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

+ 114
- 114
tests/test_string_mixin.py 查看文件

@@ -56,17 +56,17 @@ class TestStringMixIn(unittest.TestCase):
for meth in methods:
expected = getattr(str, meth).__doc__
actual = getattr(StringMixIn, meth).__doc__
self.assertEquals(expected, actual)
self.assertEqual(expected, actual)

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

self.assertIsInstance(str(fstr), str)
self.assertIsInstance(bytes(fstr), bytes)
@@ -119,18 +119,18 @@ class TestStringMixIn(unittest.TestCase):

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

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

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

gen1 = iter(str1)
gen2 = iter(str2)
@@ -141,16 +141,16 @@ class TestStringMixIn(unittest.TestCase):
for i in range(len(str1)):
out.append(gen1.next())
self.assertRaises(StopIteration, gen1.next)
self.assertEquals(expected, out)
self.assertEqual(expected, out)
self.assertRaises(StopIteration, gen2.next)

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

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

@@ -165,75 +165,75 @@ class TestStringMixIn(unittest.TestCase):
def test_other_methods(self):
"""test the remaining non-magic methods of StringMixIn"""
str1 = _FakeString("fake string")
self.assertEquals("Fake string", str1.capitalize())
self.assertEqual("Fake string", str1.capitalize())

self.assertEquals(" fake string ", str1.center(15))
self.assertEquals(" fake string ", str1.center(16))
self.assertEquals("qqfake stringqq", str1.center(15, "q"))
self.assertEqual(" fake string ", str1.center(15))
self.assertEqual(" fake string ", str1.center(16))
self.assertEqual("qqfake stringqq", str1.center(15, "q"))

self.assertEquals(1, str1.count("e"))
self.assertEquals(0, str1.count("z"))
self.assertEquals(1, str1.count("r", 7))
self.assertEquals(0, str1.count("r", 8))
self.assertEquals(1, str1.count("r", 5, 9))
self.assertEquals(0, str1.count("r", 5, 7))
self.assertEqual(1, str1.count("e"))
self.assertEqual(0, str1.count("z"))
self.assertEqual(1, str1.count("r", 7))
self.assertEqual(0, str1.count("r", 8))
self.assertEqual(1, str1.count("r", 5, 9))
self.assertEqual(0, str1.count("r", 5, 7))

if not py3k:
str2 = _FakeString("fo")
self.assertEquals(str1, str1.decode())
self.assertEqual(str1, str1.decode())
actual = _FakeString("\\U00010332\\U0001033f\\U00010344")
self.assertEquals("𐌲𐌿𐍄", actual.decode("unicode_escape"))
self.assertEqual("𐌲𐌿𐍄", actual.decode("unicode_escape"))
self.assertRaises(UnicodeError, str2.decode, "punycode")
self.assertEquals("", str2.decode("punycode", "ignore"))
self.assertEqual("", str2.decode("punycode", "ignore"))

str3 = _FakeString("𐌲𐌿𐍄")
self.assertEquals(b"fake string", str1.encode())
self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
self.assertEqual(b"fake string", str1.encode())
self.assertEqual(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
str3.encode("utf8"))
self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
self.assertEqual(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
str3.encode(encoding="utf8"))
self.assertRaises(UnicodeEncodeError, str3.encode)
self.assertRaises(UnicodeEncodeError, str3.encode, "ascii")
self.assertRaises(UnicodeEncodeError, str3.encode, "ascii", "strict")
self.assertRaises(UnicodeEncodeError, str3.encode, errors="strict")
self.assertEquals("", str3.encode("ascii", "ignore"))
self.assertEquals("", str3.encode(errors="ignore"))
self.assertEqual("", str3.encode("ascii", "ignore"))
self.assertEqual("", str3.encode(errors="ignore"))

self.assertTrue(str1.endswith("ing"))
self.assertFalse(str1.endswith("ingh"))

str4 = _FakeString("\tfoobar")
self.assertEquals("fake string", str1)
self.assertEquals(" foobar", str4.expandtabs())
self.assertEquals(" foobar", str4.expandtabs(4))
self.assertEqual("fake string", str1)
self.assertEqual(" foobar", str4.expandtabs())
self.assertEqual(" foobar", str4.expandtabs(4))

self.assertEquals(3, str1.find("e"))
self.assertEquals(-1, str1.find("z"))
self.assertEquals(7, str1.find("r", 7))
self.assertEquals(-1, str1.find("r", 8))
self.assertEquals(7, str1.find("r", 5, 9))
self.assertEquals(-1, str1.find("r", 5, 7))
self.assertEqual(3, str1.find("e"))
self.assertEqual(-1, str1.find("z"))
self.assertEqual(7, str1.find("r", 7))
self.assertEqual(-1, str1.find("r", 8))
self.assertEqual(7, str1.find("r", 5, 9))
self.assertEqual(-1, str1.find("r", 5, 7))

str5 = _FakeString("foo{0}baz")
str6 = _FakeString("foo{abc}baz")
str7 = _FakeString("foo{0}{abc}buzz")
str8 = _FakeString("{0}{1}")
self.assertEquals("fake string", str1.format())
self.assertEquals("foobarbaz", str5.format("bar"))
self.assertEquals("foobarbaz", str6.format(abc="bar"))
self.assertEquals("foobarbazbuzz", str7.format("bar", abc="baz"))
self.assertEqual("fake string", str1.format())
self.assertEqual("foobarbaz", str5.format("bar"))
self.assertEqual("foobarbaz", str6.format(abc="bar"))
self.assertEqual("foobarbazbuzz", str7.format("bar", abc="baz"))
self.assertRaises(IndexError, str8.format, "abc")

if py3k:
self.assertEquals("fake string", str1.format_map({}))
self.assertEquals("foobarbaz", str6.format_map({"abc": "bar"}))
self.assertEqual("fake string", str1.format_map({}))
self.assertEqual("foobarbaz", str6.format_map({"abc": "bar"}))
self.assertRaises(ValueError, str5.format_map, {0: "abc"})

self.assertEquals(3, str1.index("e"))
self.assertEqual(3, str1.index("e"))
self.assertRaises(ValueError, str1.index, "z")
self.assertEquals(7, str1.index("r", 7))
self.assertEqual(7, str1.index("r", 7))
self.assertRaises(ValueError, str1.index, "r", 8)
self.assertEquals(7, str1.index("r", 5, 9))
self.assertEqual(7, str1.index("r", 5, 9))
self.assertRaises(ValueError, str1.index, "r", 5, 7)

str9 = _FakeString("foobar")
@@ -303,120 +303,120 @@ class TestStringMixIn(unittest.TestCase):
self.assertFalse(str15.isupper())
self.assertTrue(str21.isupper())

self.assertEquals("foobar", str15.join(["foo", "bar"]))
self.assertEquals("foo123bar123baz", str12.join(("foo", "bar", "baz")))
self.assertEqual("foobar", str15.join(["foo", "bar"]))
self.assertEqual("foo123bar123baz", str12.join(("foo", "bar", "baz")))

self.assertEquals("fake string ", str1.ljust(15))
self.assertEquals("fake string ", str1.ljust(16))
self.assertEquals("fake stringqqqq", str1.ljust(15, "q"))
self.assertEqual("fake string ", str1.ljust(15))
self.assertEqual("fake string ", str1.ljust(16))
self.assertEqual("fake stringqqqq", str1.ljust(15, "q"))

str22 = _FakeString("ß")
self.assertEquals("", str15.lower())
self.assertEquals("foobar", str16.lower())
self.assertEquals("ß", str22.lower())
self.assertEqual("", str15.lower())
self.assertEqual("foobar", str16.lower())
self.assertEqual("ß", str22.lower())
if py3k:
self.assertEquals("", str15.casefold())
self.assertEquals("foobar", str16.casefold())
self.assertEquals("ss", str22.casefold())
self.assertEqual("", str15.casefold())
self.assertEqual("foobar", str16.casefold())
self.assertEqual("ss", str22.casefold())

str23 = _FakeString(" fake string ")
self.assertEquals("fake string", str1.lstrip())
self.assertEquals("fake string ", str23.lstrip())
self.assertEquals("ke string", str1.lstrip("abcdef"))
self.assertEqual("fake string", str1.lstrip())
self.assertEqual("fake string ", str23.lstrip())
self.assertEqual("ke string", str1.lstrip("abcdef"))

self.assertEquals(("fa", "ke", " string"), str1.partition("ke"))
self.assertEquals(("fake string", "", ""), str1.partition("asdf"))
self.assertEqual(("fa", "ke", " string"), str1.partition("ke"))
self.assertEqual(("fake string", "", ""), str1.partition("asdf"))

str24 = _FakeString("boo foo moo")
self.assertEquals("real string", str1.replace("fake", "real"))
self.assertEquals("bu fu moo", str24.replace("oo", "u", 2))
self.assertEqual("real string", str1.replace("fake", "real"))
self.assertEqual("bu fu moo", str24.replace("oo", "u", 2))

self.assertEquals(3, str1.rfind("e"))
self.assertEquals(-1, str1.rfind("z"))
self.assertEquals(7, str1.rfind("r", 7))
self.assertEquals(-1, str1.rfind("r", 8))
self.assertEquals(7, str1.rfind("r", 5, 9))
self.assertEquals(-1, str1.rfind("r", 5, 7))
self.assertEqual(3, str1.rfind("e"))
self.assertEqual(-1, str1.rfind("z"))
self.assertEqual(7, str1.rfind("r", 7))
self.assertEqual(-1, str1.rfind("r", 8))
self.assertEqual(7, str1.rfind("r", 5, 9))
self.assertEqual(-1, str1.rfind("r", 5, 7))

self.assertEquals(3, str1.rindex("e"))
self.assertEqual(3, str1.rindex("e"))
self.assertRaises(ValueError, str1.rindex, "z")
self.assertEquals(7, str1.rindex("r", 7))
self.assertEqual(7, str1.rindex("r", 7))
self.assertRaises(ValueError, str1.rindex, "r", 8)
self.assertEquals(7, str1.rindex("r", 5, 9))
self.assertEqual(7, str1.rindex("r", 5, 9))
self.assertRaises(ValueError, str1.rindex, "r", 5, 7)

self.assertEquals(" fake string", str1.rjust(15))
self.assertEquals(" fake string", str1.rjust(16))
self.assertEquals("qqqqfake string", str1.rjust(15, "q"))
self.assertEqual(" fake string", str1.rjust(15))
self.assertEqual(" fake string", str1.rjust(16))
self.assertEqual("qqqqfake string", str1.rjust(15, "q"))

self.assertEquals(("fa", "ke", " string"), str1.rpartition("ke"))
self.assertEquals(("", "", "fake string"), str1.rpartition("asdf"))
self.assertEqual(("fa", "ke", " string"), str1.rpartition("ke"))
self.assertEqual(("", "", "fake string"), str1.rpartition("asdf"))

str25 = _FakeString(" this is a sentence with whitespace ")
actual = ["this", "is", "a", "sentence", "with", "whitespace"]
self.assertEquals(actual, str25.rsplit())
self.assertEquals(actual, str25.rsplit(None))
self.assertEqual(actual, str25.rsplit())
self.assertEqual(actual, str25.rsplit(None))
actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
"", "whitespace", ""]
self.assertEquals(actual, str25.rsplit(" "))
self.assertEqual(actual, str25.rsplit(" "))
actual = [" this is a", "sentence", "with", "whitespace"]
self.assertEquals(actual, str25.rsplit(None, 3))
self.assertEqual(actual, str25.rsplit(None, 3))
actual = [" this is a sentence with", "", "whitespace", ""]
self.assertEquals(actual, str25.rsplit(" ", 3))
self.assertEqual(actual, str25.rsplit(" ", 3))
if py3k:
self.assertEquals(actual, str25.rsplit(maxsplit=3))
self.assertEqual(actual, str25.rsplit(maxsplit=3))

self.assertEquals("fake string", str1.rstrip())
self.assertEquals(" fake string", str23.rstrip())
self.assertEquals("fake stri", str1.rstrip("ngr"))
self.assertEqual("fake string", str1.rstrip())
self.assertEqual(" fake string", str23.rstrip())
self.assertEqual("fake stri", str1.rstrip("ngr"))

actual = ["this", "is", "a", "sentence", "with", "whitespace"]
self.assertEquals(actual, str25.split())
self.assertEquals(actual, str25.split(None))
self.assertEqual(actual, str25.split())
self.assertEqual(actual, str25.split(None))
actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
"", "whitespace", ""]
self.assertEquals(actual, str25.split(" "))
self.assertEqual(actual, str25.split(" "))
actual = ["this", "is", "a", "sentence with whitespace "]
self.assertEquals(actual, str25.split(None, 3))
self.assertEqual(actual, str25.split(None, 3))
actual = ["", "", "", "this is a sentence with whitespace "]
self.assertEquals(actual, str25.split(" ", 3))
self.assertEqual(actual, str25.split(" ", 3))
if py3k:
self.assertEquals(actual, str25.split(maxsplit=3))
self.assertEqual(actual, str25.split(maxsplit=3))

str26 = _FakeString("lines\nof\ntext\r\nare\r\npresented\nhere")
self.assertEquals(["lines", "of", "text", "are", "presented", "here"],
self.assertEqual(["lines", "of", "text", "are", "presented", "here"],
str26.splitlines())
self.assertEquals(["lines\n", "of\n", "text\r\n", "are\r\n",
self.assertEqual(["lines\n", "of\n", "text\r\n", "are\r\n",
"presented\n", "here"], str26.splitlines(True))

self.assertTrue(str1.startswith("fake"))
self.assertFalse(str1.startswith("faker"))

self.assertEquals("fake string", str1.strip())
self.assertEquals("fake string", str23.strip())
self.assertEquals("ke stri", str1.strip("abcdefngr"))
self.assertEqual("fake string", str1.strip())
self.assertEqual("fake string", str23.strip())
self.assertEqual("ke stri", str1.strip("abcdefngr"))

self.assertEquals("fOObAR", str16.swapcase())
self.assertEqual("fOObAR", str16.swapcase())

self.assertEquals("Fake String", str1.title())
self.assertEqual("Fake String", str1.title())

if py3k:
table1 = str.maketrans({97: "1", 101: "2", 105: "3", 111: "4",
117: "5"})
table2 = str.maketrans("aeiou", "12345")
table3 = str.maketrans("aeiou", "12345", "rts")
self.assertEquals("f1k2 str3ng", str1.translate(table1))
self.assertEquals("f1k2 str3ng", str1.translate(table2))
self.assertEquals("f1k2 3ng", str1.translate(table3))
self.assertEqual("f1k2 str3ng", str1.translate(table1))
self.assertEqual("f1k2 str3ng", str1.translate(table2))
self.assertEqual("f1k2 3ng", str1.translate(table3))
else:
table = {97: "1", 101: "2", 105: "3", 111: "4", 117: "5"}
self.assertEquals("f1k2 str3ng", str1.translate(table))
self.assertEqual("f1k2 str3ng", str1.translate(table))

self.assertEquals("", str15.upper())
self.assertEquals("FOOBAR", str16.upper())
self.assertEqual("", str15.upper())
self.assertEqual("FOOBAR", str16.upper())

self.assertEquals("123", str12.zfill(3))
self.assertEquals("000123", str12.zfill(6))
self.assertEqual("123", str12.zfill(3))
self.assertEqual("000123", str12.zfill(6))

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

+ 12
- 12
tests/test_tokens.py 查看文件

@@ -42,8 +42,8 @@ class TestTokens(unittest.TestCase):
token1 = tokens.Token()
token2 = tokens.Token(foo="bar", baz=123)

self.assertEquals("bar", token2.foo)
self.assertEquals(123, token2.baz)
self.assertEqual("bar", token2.foo)
self.assertEqual(123, token2.baz)
self.assertRaises(KeyError, lambda: token1.foo)
self.assertRaises(KeyError, lambda: token2.bar)

@@ -51,8 +51,8 @@ class TestTokens(unittest.TestCase):
token2.foo = "ham"
del token2.baz

self.assertEquals("eggs", token1.spam)
self.assertEquals("ham", token2.foo)
self.assertEqual("eggs", token1.spam)
self.assertEqual("ham", token2.foo)
self.assertRaises(KeyError, lambda: token2.baz)
self.assertRaises(KeyError, delattr, token2, "baz")

@@ -63,15 +63,15 @@ class TestTokens(unittest.TestCase):
token3 = tokens.Text(text="earwig" * 100)
hundredchars = ("earwig" * 100)[:97] + "..."

self.assertEquals("Token()", repr(token1))
self.assertEqual("Token()", repr(token1))
if py3k:
token2repr = "Token(foo='bar', baz=123)"
token3repr = "Text(text='" + hundredchars + "')"
else:
token2repr = "Token(foo=u'bar', baz=123)"
token3repr = "Text(text=u'" + hundredchars + "')"
self.assertEquals(token2repr, repr(token2))
self.assertEquals(token3repr, repr(token3))
self.assertEqual(token2repr, repr(token2))
self.assertEqual(token3repr, repr(token3))

def test_equality(self):
"""check that equivalent tokens are considered equal"""
@@ -82,10 +82,10 @@ class TestTokens(unittest.TestCase):
token5 = tokens.Text(text="asdf")
token6 = tokens.TemplateOpen(text="asdf")

self.assertEquals(token1, token2)
self.assertEquals(token2, token1)
self.assertEquals(token4, token5)
self.assertEquals(token5, token4)
self.assertEqual(token1, token2)
self.assertEqual(token2, token1)
self.assertEqual(token4, token5)
self.assertEqual(token5, token4)
self.assertNotEquals(token1, token3)
self.assertNotEquals(token2, token3)
self.assertNotEquals(token4, token6)
@@ -99,7 +99,7 @@ class TestTokens(unittest.TestCase):
tokens.Text(text="earwig")
]
for token in tests:
self.assertEquals(token, eval(repr(token), vars(tokens)))
self.assertEqual(token, eval(repr(token), vars(tokens)))

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

Loading…
取消
儲存