blob: 3acb6d10db2cb332c2d555a1c64d5e15359f0278 [file] [log] [blame]
Neal Norwitz7dbd2a32007-06-09 03:36:34 +00001
Walter Dörwald0fd583c2003-02-21 12:53:50 +00002import unittest
Neal Norwitzba965de2007-06-11 02:14:39 +00003import struct
Neal Norwitz7dbd2a32007-06-09 03:36:34 +00004import sys
Walter Dörwald0fd583c2003-02-21 12:53:50 +00005from test import test_support, string_tests
6
7
8class StrTest(
9 string_tests.CommonTest,
10 string_tests.MixinStrUnicodeUserStringTest,
Walter Dörwald57d88e52004-08-26 16:53:04 +000011 string_tests.MixinStrUserStringTest,
12 string_tests.MixinStrUnicodeTest,
Walter Dörwald0fd583c2003-02-21 12:53:50 +000013 ):
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örwald43440a62003-03-31 18:07:50 +000021 def test_formatting(self):
22 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
23 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
24
Brett Cannonc3647ac2005-04-26 03:45:26 +000025 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 Norwitz7dbd2a32007-06-09 03:36:34 +000088 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 Norwitzba965de2007-06-11 02:14:39 +000092 if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
Neal Norwitz7dbd2a32007-06-09 03:36:34 +000093 return
94 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint)
95
96
Walter Dörwald0fd583c2003-02-21 12:53:50 +000097def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000098 test_support.run_unittest(StrTest)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000099
100if __name__ == "__main__":
101 test_main()