Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 1 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 2 | import unittest |
Neal Norwitz | ba965de | 2007-06-11 02:14:39 +0000 | [diff] [blame^] | 3 | import struct |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 4 | import sys |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 5 | from test import test_support, string_tests |
| 6 | |
| 7 | |
| 8 | class StrTest( |
| 9 | string_tests.CommonTest, |
| 10 | string_tests.MixinStrUnicodeUserStringTest, |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 11 | string_tests.MixinStrUserStringTest, |
| 12 | string_tests.MixinStrUnicodeTest, |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 13 | ): |
| 14 | |
| 15 | type2test = str |
| 16 | |
| 17 | # We don't need to propagate to str |
| 18 | def fixtype(self, obj): |
| 19 | return obj |
| 20 | |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 21 | def test_formatting(self): |
| 22 | string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
| 23 | self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) |
| 24 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 25 | def test_conversion(self): |
| 26 | # Make sure __str__() behaves properly |
| 27 | class Foo0: |
| 28 | def __unicode__(self): |
| 29 | return u"foo" |
| 30 | |
| 31 | class Foo1: |
| 32 | def __str__(self): |
| 33 | return "foo" |
| 34 | |
| 35 | class Foo2(object): |
| 36 | def __str__(self): |
| 37 | return "foo" |
| 38 | |
| 39 | class Foo3(object): |
| 40 | def __str__(self): |
| 41 | return u"foo" |
| 42 | |
| 43 | class Foo4(unicode): |
| 44 | def __str__(self): |
| 45 | return u"foo" |
| 46 | |
| 47 | class Foo5(str): |
| 48 | def __str__(self): |
| 49 | return u"foo" |
| 50 | |
| 51 | class Foo6(str): |
| 52 | def __str__(self): |
| 53 | return "foos" |
| 54 | |
| 55 | def __unicode__(self): |
| 56 | return u"foou" |
| 57 | |
| 58 | class Foo7(unicode): |
| 59 | def __str__(self): |
| 60 | return "foos" |
| 61 | def __unicode__(self): |
| 62 | return u"foou" |
| 63 | |
| 64 | class Foo8(str): |
| 65 | def __new__(cls, content=""): |
| 66 | return str.__new__(cls, 2*content) |
| 67 | def __str__(self): |
| 68 | return self |
| 69 | |
| 70 | class Foo9(str): |
| 71 | def __str__(self): |
| 72 | return "string" |
| 73 | def __unicode__(self): |
| 74 | return "not unicode" |
| 75 | |
| 76 | self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__ |
| 77 | self.assertEqual(str(Foo1()), "foo") |
| 78 | self.assertEqual(str(Foo2()), "foo") |
| 79 | self.assertEqual(str(Foo3()), "foo") |
| 80 | self.assertEqual(str(Foo4("bar")), "foo") |
| 81 | self.assertEqual(str(Foo5("bar")), "foo") |
| 82 | self.assertEqual(str(Foo6("bar")), "foos") |
| 83 | self.assertEqual(str(Foo7("bar")), "foos") |
| 84 | self.assertEqual(str(Foo8("foo")), "foofoo") |
| 85 | self.assertEqual(str(Foo9("foo")), "string") |
| 86 | self.assertEqual(unicode(Foo9("foo")), u"not unicode") |
| 87 | |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 88 | def test_expandtabs_overflows_gracefully(self): |
| 89 | # This test only affects 32-bit platforms because expandtabs can only take |
| 90 | # an int as the max value, not a 64-bit C long. If expandtabs is changed |
| 91 | # to take a 64-bit long, this test should apply to all platforms. |
Neal Norwitz | ba965de | 2007-06-11 02:14:39 +0000 | [diff] [blame^] | 92 | if sys.maxint > (1 << 32) or struct.calcsize('P') != 4: |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 93 | return |
| 94 | self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint) |
| 95 | |
| 96 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 97 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 98 | test_support.run_unittest(StrTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 99 | |
| 100 | if __name__ == "__main__": |
| 101 | test_main() |