blob: 1d024678df6e4381d90163a8641542402c012fd2 [file] [log] [blame]
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001
Walter Dörwald0fd583c2003-02-21 12:53:50 +00002import unittest
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003import struct
4import 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.MixinStrUnicodeTest,
Walter Dörwald0fd583c2003-02-21 12:53:50 +000012 ):
13
Walter Dörwald4c271fe2007-06-07 13:52:37 +000014 type2test = str8
Walter Dörwald0fd583c2003-02-21 12:53:50 +000015
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)
Walter Dörwalddb290f02007-05-05 14:12:24 +000022 self.assertRaises(OverflowError, '%c'.__mod__, 0x12341234)
Walter Dörwald43440a62003-03-31 18:07:50 +000023
Guido van Rossum49d6b072006-08-17 21:11:47 +000024 def test_iterators(self):
25 # Make sure str objects have an __iter__ method
26 it = "abc".__iter__()
Georg Brandla18af4e2007-04-21 15:47:16 +000027 self.assertEqual(next(it), "a")
28 self.assertEqual(next(it), "b")
29 self.assertEqual(next(it), "c")
30 self.assertRaises(StopIteration, next, it)
Guido van Rossum49d6b072006-08-17 21:11:47 +000031
Brett Cannonc3647ac2005-04-26 03:45:26 +000032 def test_conversion(self):
33 # Make sure __str__() behaves properly
Brett Cannonc3647ac2005-04-26 03:45:26 +000034
35 class Foo1:
36 def __str__(self):
37 return "foo"
38
Guido van Rossumef87d6e2007-05-02 19:09:54 +000039 class Foo7(str):
Brett Cannonc3647ac2005-04-26 03:45:26 +000040 def __str__(self):
41 return "foos"
Brett Cannonc3647ac2005-04-26 03:45:26 +000042
43 class Foo8(str):
44 def __new__(cls, content=""):
45 return str.__new__(cls, 2*content)
46 def __str__(self):
47 return self
48
Brett Cannonc3647ac2005-04-26 03:45:26 +000049 self.assertEqual(str(Foo1()), "foo")
Brett Cannon40430012007-10-22 20:24:51 +000050 self.assertEqual(str(Foo7("bar")), "foos")
Brett Cannonc3647ac2005-04-26 03:45:26 +000051 self.assertEqual(str(Foo8("foo")), "foofoo")
Brett Cannonc3647ac2005-04-26 03:45:26 +000052
Guido van Rossumcd16bf62007-06-13 18:07:49 +000053 def test_expandtabs_overflows_gracefully(self):
54 # This test only affects 32-bit platforms because expandtabs can only take
55 # an int as the max value, not a 64-bit C long. If expandtabs is changed
56 # to take a 64-bit long, this test should apply to all platforms.
57 if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
58 return
59 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint)
60
61
Walter Dörwald0fd583c2003-02-21 12:53:50 +000062def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000063 test_support.run_unittest(StrTest)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000064
65if __name__ == "__main__":
66 test_main()