/Lib/test/test_userstring.py

http://unladen-swallow.googlecode.com/ · Python · 144 lines · 111 code · 19 blank · 14 comment · 5 complexity · 20faa00e6c12a4e63fcb381957daf0b8 MD5 · raw file

  1. #!/usr/bin/env python
  2. # UserString is a wrapper around the native builtin string type.
  3. # UserString instances should behave similar to builtin string objects.
  4. import string
  5. from test import test_support, string_tests
  6. from UserString import UserString, MutableString
  7. import warnings
  8. class UserStringTest(
  9. string_tests.CommonTest,
  10. string_tests.MixinStrUnicodeUserStringTest,
  11. string_tests.MixinStrStringUserStringTest,
  12. string_tests.MixinStrUserStringTest
  13. ):
  14. type2test = UserString
  15. # Overwrite the three testing methods, because UserString
  16. # can't cope with arguments propagated to UserString
  17. # (and we don't test with subclasses)
  18. def checkequal(self, result, object, methodname, *args):
  19. result = self.fixtype(result)
  20. object = self.fixtype(object)
  21. # we don't fix the arguments, because UserString can't cope with it
  22. realresult = getattr(object, methodname)(*args)
  23. self.assertEqual(
  24. result,
  25. realresult
  26. )
  27. def checkraises(self, exc, object, methodname, *args):
  28. object = self.fixtype(object)
  29. # we don't fix the arguments, because UserString can't cope with it
  30. self.assertRaises(
  31. exc,
  32. getattr(object, methodname),
  33. *args
  34. )
  35. def checkcall(self, object, methodname, *args):
  36. object = self.fixtype(object)
  37. # we don't fix the arguments, because UserString can't cope with it
  38. getattr(object, methodname)(*args)
  39. class MutableStringTest(UserStringTest):
  40. type2test = MutableString
  41. # MutableStrings can be hashed => deactivate test
  42. def test_hash(self):
  43. pass
  44. def test_setitem(self):
  45. s = self.type2test("foo")
  46. self.assertRaises(IndexError, s.__setitem__, -4, "bar")
  47. self.assertRaises(IndexError, s.__setitem__, 3, "bar")
  48. s[-1] = "bar"
  49. self.assertEqual(s, "fobar")
  50. s[0] = "bar"
  51. self.assertEqual(s, "barobar")
  52. def test_delitem(self):
  53. s = self.type2test("foo")
  54. self.assertRaises(IndexError, s.__delitem__, -4)
  55. self.assertRaises(IndexError, s.__delitem__, 3)
  56. del s[-1]
  57. self.assertEqual(s, "fo")
  58. del s[0]
  59. self.assertEqual(s, "o")
  60. del s[0]
  61. self.assertEqual(s, "")
  62. def test_setslice(self):
  63. s = self.type2test("foo")
  64. s[:] = "bar"
  65. self.assertEqual(s, "bar")
  66. s[1:2] = "foo"
  67. self.assertEqual(s, "bfoor")
  68. s[1:-1] = UserString("a")
  69. self.assertEqual(s, "bar")
  70. s[0:10] = 42
  71. self.assertEqual(s, "42")
  72. def test_delslice(self):
  73. s = self.type2test("foobar")
  74. del s[3:10]
  75. self.assertEqual(s, "foo")
  76. del s[-1:10]
  77. self.assertEqual(s, "fo")
  78. def test_extended_set_del_slice(self):
  79. indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
  80. orig = string.ascii_letters + string.digits
  81. for start in indices:
  82. for stop in indices:
  83. # Use indices[1:] when MutableString can handle real
  84. # extended slices
  85. for step in (None, 1, -1):
  86. s = self.type2test(orig)
  87. L = list(orig)
  88. # Make sure we have a slice of exactly the right length,
  89. # but with (hopefully) different data.
  90. data = L[start:stop:step]
  91. data.reverse()
  92. L[start:stop:step] = data
  93. s[start:stop:step] = "".join(data)
  94. self.assertEquals(s, "".join(L))
  95. del L[start:stop:step]
  96. del s[start:stop:step]
  97. self.assertEquals(s, "".join(L))
  98. def test_immutable(self):
  99. s = self.type2test("foobar")
  100. s2 = s.immutable()
  101. self.assertEqual(s, s2)
  102. self.assert_(isinstance(s2, UserString))
  103. def test_iadd(self):
  104. s = self.type2test("foo")
  105. s += "bar"
  106. self.assertEqual(s, "foobar")
  107. s += UserString("baz")
  108. self.assertEqual(s, "foobarbaz")
  109. s += 42
  110. self.assertEqual(s, "foobarbaz42")
  111. def test_imul(self):
  112. s = self.type2test("foo")
  113. s *= 1
  114. self.assertEqual(s, "foo")
  115. s *= 2
  116. self.assertEqual(s, "foofoo")
  117. s *= -1
  118. self.assertEqual(s, "")
  119. def test_main():
  120. with warnings.catch_warnings():
  121. warnings.filterwarnings("ignore", ".*MutableString",
  122. DeprecationWarning)
  123. test_support.run_unittest(UserStringTest, MutableStringTest)
  124. if __name__ == "__main__":
  125. test_main()