Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support, string_tests |
| 3 | |
| 4 | |
| 5 | class StrTest( |
| 6 | string_tests.CommonTest, |
| 7 | string_tests.MixinStrUnicodeUserStringTest, |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 8 | string_tests.MixinStrUserStringTest, |
| 9 | string_tests.MixinStrUnicodeTest, |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 10 | ): |
| 11 | |
| 12 | type2test = str |
| 13 | |
| 14 | # We don't need to propagate to str |
| 15 | def fixtype(self, obj): |
| 16 | return obj |
| 17 | |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 18 | def test_formatting(self): |
| 19 | string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
| 20 | self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) |
| 21 | |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 22 | def test_iterators(self): |
| 23 | # Make sure str objects have an __iter__ method |
| 24 | it = "abc".__iter__() |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 25 | self.assertEqual(next(it), "a") |
| 26 | self.assertEqual(next(it), "b") |
| 27 | self.assertEqual(next(it), "c") |
| 28 | self.assertRaises(StopIteration, next, it) |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 29 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 30 | def test_conversion(self): |
| 31 | # Make sure __str__() behaves properly |
| 32 | class Foo0: |
| 33 | def __unicode__(self): |
| 34 | return u"foo" |
| 35 | |
| 36 | class Foo1: |
| 37 | def __str__(self): |
| 38 | return "foo" |
| 39 | |
| 40 | class Foo2(object): |
| 41 | def __str__(self): |
| 42 | return "foo" |
| 43 | |
| 44 | class Foo3(object): |
| 45 | def __str__(self): |
| 46 | return u"foo" |
| 47 | |
| 48 | class Foo4(unicode): |
| 49 | def __str__(self): |
| 50 | return u"foo" |
| 51 | |
| 52 | class Foo5(str): |
| 53 | def __str__(self): |
| 54 | return u"foo" |
| 55 | |
| 56 | class Foo6(str): |
| 57 | def __str__(self): |
| 58 | return "foos" |
| 59 | |
| 60 | def __unicode__(self): |
| 61 | return u"foou" |
| 62 | |
| 63 | class Foo7(unicode): |
| 64 | def __str__(self): |
| 65 | return "foos" |
| 66 | def __unicode__(self): |
| 67 | return u"foou" |
| 68 | |
| 69 | class Foo8(str): |
| 70 | def __new__(cls, content=""): |
| 71 | return str.__new__(cls, 2*content) |
| 72 | def __str__(self): |
| 73 | return self |
| 74 | |
| 75 | class Foo9(str): |
| 76 | def __str__(self): |
| 77 | return "string" |
| 78 | def __unicode__(self): |
| 79 | return "not unicode" |
| 80 | |
| 81 | self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__ |
| 82 | self.assertEqual(str(Foo1()), "foo") |
| 83 | self.assertEqual(str(Foo2()), "foo") |
| 84 | self.assertEqual(str(Foo3()), "foo") |
| 85 | self.assertEqual(str(Foo4("bar")), "foo") |
| 86 | self.assertEqual(str(Foo5("bar")), "foo") |
| 87 | self.assertEqual(str(Foo6("bar")), "foos") |
| 88 | self.assertEqual(str(Foo7("bar")), "foos") |
| 89 | self.assertEqual(str(Foo8("foo")), "foofoo") |
| 90 | self.assertEqual(str(Foo9("foo")), "string") |
| 91 | self.assertEqual(unicode(Foo9("foo")), u"not unicode") |
| 92 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 93 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 94 | test_support.run_unittest(StrTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 95 | |
| 96 | if __name__ == "__main__": |
| 97 | test_main() |