diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py index 6057e65fdca..b9a5b7f76e4 100755 --- a/Lib/test/test_userstring.py +++ b/Lib/test/test_userstring.py @@ -50,6 +50,68 @@ class MutableStringTest(UserStringTest): def test_hash(self): pass + def test_setitem(self): + s = self.type2test("foo") + self.assertRaises(IndexError, s.__setitem__, -1, "bar") + self.assertRaises(IndexError, s.__setitem__, 3, "bar") + s[0] = "bar" + self.assertEqual(s, "baroo") + s[4] = "foo" + self.assertEqual(s, "barofoo") + + def test_delitem(self): + s = self.type2test("foo") + self.assertRaises(IndexError, s.__setitem__, -1, "bar") + self.assertRaises(IndexError, s.__setitem__, 3, "bar") + del s[0] + self.assertEqual(s, "oo") + del s[1] + self.assertEqual(s, "o") + del s[0] + self.assertEqual(s, "") + + def test_setslice(self): + s = self.type2test("foo") + s[:] = "bar" + self.assertEqual(s, "bar") + s[1:2] = "foo" + self.assertEqual(s, "bfoor") + s[1:-1] = UserString("a") + self.assertEqual(s, "bar") + s[0:10] = 42 + self.assertEqual(s, "42") + + def test_delslice(self): + s = self.type2test("foobar") + del s[3:10] + self.assertEqual(s, "foo") + del s[-1:10] + self.assertEqual(s, "fo") + + def test_immutable(self): + s = self.type2test("foobar") + s2 = s.immutable() + self.assertEqual(s, s2) + self.assert_(isinstance(s2, UserString)) + + def test_iadd(self): + s = self.type2test("foo") + s += "bar" + self.assertEqual(s, "foobar") + s += UserString("baz") + self.assertEqual(s, "foobarbaz") + s += 42 + self.assertEqual(s, "foobarbaz42") + + def test_imul(self): + s = self.type2test("foo") + s *= 1 + self.assertEqual(s, "foo") + s *= 2 + self.assertEqual(s, "foofoo") + s *= -1 + self.assertEqual(s, "") + def test_main(): test_support.run_unittest(UserStringTest, MutableStringTest)