test_userstring.py 4.7 KB

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