|
1 |
|
2 import unittest |
|
3 import struct |
|
4 import sys |
|
5 from test import test_support, string_tests |
|
6 |
|
7 |
|
8 class StrTest( |
|
9 string_tests.CommonTest, |
|
10 string_tests.MixinStrUnicodeUserStringTest, |
|
11 string_tests.MixinStrUserStringTest, |
|
12 string_tests.MixinStrUnicodeTest, |
|
13 ): |
|
14 |
|
15 type2test = str |
|
16 |
|
17 # We don't need to propagate to str |
|
18 def fixtype(self, obj): |
|
19 return obj |
|
20 |
|
21 def test_formatting(self): |
|
22 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
|
23 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) |
|
24 |
|
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 |
|
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. |
|
92 if sys.maxint > (1 << 32) or struct.calcsize('P') != 4: |
|
93 return |
|
94 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint) |
|
95 |
|
96 |
|
97 def test_main(): |
|
98 test_support.run_unittest(StrTest) |
|
99 |
|
100 if __name__ == "__main__": |
|
101 test_main() |