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 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 22 | def test_conversion(self): |
| 23 | # Make sure __str__() behaves properly |
| 24 | class Foo0: |
| 25 | def __unicode__(self): |
| 26 | return u"foo" |
| 27 | |
| 28 | class Foo1: |
| 29 | def __str__(self): |
| 30 | return "foo" |
| 31 | |
| 32 | class Foo2(object): |
| 33 | def __str__(self): |
| 34 | return "foo" |
| 35 | |
| 36 | class Foo3(object): |
| 37 | def __str__(self): |
| 38 | return u"foo" |
| 39 | |
| 40 | class Foo4(unicode): |
| 41 | def __str__(self): |
| 42 | return u"foo" |
| 43 | |
| 44 | class Foo5(str): |
| 45 | def __str__(self): |
| 46 | return u"foo" |
| 47 | |
| 48 | class Foo6(str): |
| 49 | def __str__(self): |
| 50 | return "foos" |
| 51 | |
| 52 | def __unicode__(self): |
| 53 | return u"foou" |
| 54 | |
| 55 | class Foo7(unicode): |
| 56 | def __str__(self): |
| 57 | return "foos" |
| 58 | def __unicode__(self): |
| 59 | return u"foou" |
| 60 | |
| 61 | class Foo8(str): |
| 62 | def __new__(cls, content=""): |
| 63 | return str.__new__(cls, 2*content) |
| 64 | def __str__(self): |
| 65 | return self |
| 66 | |
| 67 | class Foo9(str): |
| 68 | def __str__(self): |
| 69 | return "string" |
| 70 | def __unicode__(self): |
| 71 | return "not unicode" |
| 72 | |
| 73 | self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__ |
| 74 | self.assertEqual(str(Foo1()), "foo") |
| 75 | self.assertEqual(str(Foo2()), "foo") |
| 76 | self.assertEqual(str(Foo3()), "foo") |
| 77 | self.assertEqual(str(Foo4("bar")), "foo") |
| 78 | self.assertEqual(str(Foo5("bar")), "foo") |
| 79 | self.assertEqual(str(Foo6("bar")), "foos") |
| 80 | self.assertEqual(str(Foo7("bar")), "foos") |
| 81 | self.assertEqual(str(Foo8("foo")), "foofoo") |
| 82 | self.assertEqual(str(Foo9("foo")), "string") |
| 83 | self.assertEqual(unicode(Foo9("foo")), u"not unicode") |
| 84 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 85 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 86 | test_support.run_unittest(StrTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 87 | |
| 88 | if __name__ == "__main__": |
| 89 | test_main() |