blob: 18535173457d796ae1690e23c008ae0b897083bf [file] [log] [blame]
Neal Norwitz7dbd2a32007-06-09 03:36:34 +00001
Walter Dörwald0fd583c2003-02-21 12:53:50 +00002import unittest
Neal Norwitz7dbd2a32007-06-09 03:36:34 +00003import sys
Walter Dörwald0fd583c2003-02-21 12:53:50 +00004from test import test_support, string_tests
5
6
7class StrTest(
8 string_tests.CommonTest,
9 string_tests.MixinStrUnicodeUserStringTest,
Walter Dörwald57d88e52004-08-26 16:53:04 +000010 string_tests.MixinStrUserStringTest,
11 string_tests.MixinStrUnicodeTest,
Walter Dörwald0fd583c2003-02-21 12:53:50 +000012 ):
13
14 type2test = str
15
16 # We don't need to propagate to str
17 def fixtype(self, obj):
18 return obj
19
Walter Dörwald43440a62003-03-31 18:07:50 +000020 def test_formatting(self):
21 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
22 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
23
Brett Cannonc3647ac2005-04-26 03:45:26 +000024 def test_conversion(self):
25 # Make sure __str__() behaves properly
26 class Foo0:
27 def __unicode__(self):
28 return u"foo"
29
30 class Foo1:
31 def __str__(self):
32 return "foo"
33
34 class Foo2(object):
35 def __str__(self):
36 return "foo"
37
38 class Foo3(object):
39 def __str__(self):
40 return u"foo"
41
42 class Foo4(unicode):
43 def __str__(self):
44 return u"foo"
45
46 class Foo5(str):
47 def __str__(self):
48 return u"foo"
49
50 class Foo6(str):
51 def __str__(self):
52 return "foos"
53
54 def __unicode__(self):
55 return u"foou"
56
57 class Foo7(unicode):
58 def __str__(self):
59 return "foos"
60 def __unicode__(self):
61 return u"foou"
62
63 class Foo8(str):
64 def __new__(cls, content=""):
65 return str.__new__(cls, 2*content)
66 def __str__(self):
67 return self
68
69 class Foo9(str):
70 def __str__(self):
71 return "string"
72 def __unicode__(self):
73 return "not unicode"
74
75 self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__
76 self.assertEqual(str(Foo1()), "foo")
77 self.assertEqual(str(Foo2()), "foo")
78 self.assertEqual(str(Foo3()), "foo")
79 self.assertEqual(str(Foo4("bar")), "foo")
80 self.assertEqual(str(Foo5("bar")), "foo")
81 self.assertEqual(str(Foo6("bar")), "foos")
82 self.assertEqual(str(Foo7("bar")), "foos")
83 self.assertEqual(str(Foo8("foo")), "foofoo")
84 self.assertEqual(str(Foo9("foo")), "string")
85 self.assertEqual(unicode(Foo9("foo")), u"not unicode")
86
Neal Norwitz7dbd2a32007-06-09 03:36:34 +000087 def test_expandtabs_overflows_gracefully(self):
88 # This test only affects 32-bit platforms because expandtabs can only take
89 # an int as the max value, not a 64-bit C long. If expandtabs is changed
90 # to take a 64-bit long, this test should apply to all platforms.
91 if sys.maxint > (1 << 32):
92 return
93 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint)
94
95
Walter Dörwald0fd583c2003-02-21 12:53:50 +000096def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000097 test_support.run_unittest(StrTest)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000098
99if __name__ == "__main__":
100 test_main()