test_str.py revision ba965deea89299611454e43be005df357b75b11d
17dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz
20fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldimport unittest
3ba965deea89299611454e43be005df357b75b11dNeal Norwitzimport struct
47dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitzimport sys
50fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldfrom test import test_support, string_tests
60fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
70fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
80fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldclass StrTest(
90fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    string_tests.CommonTest,
100fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    string_tests.MixinStrUnicodeUserStringTest,
1157d88e5abd06cefcc8d52d62765c36214b1c1956Walter Dörwald    string_tests.MixinStrUserStringTest,
1257d88e5abd06cefcc8d52d62765c36214b1c1956Walter Dörwald    string_tests.MixinStrUnicodeTest,
130fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    ):
140fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
150fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    type2test = str
160fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
170fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    # We don't need to propagate to str
180fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    def fixtype(self, obj):
190fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald        return obj
200fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
2143440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald    def test_formatting(self):
2243440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald        string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
2343440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald        self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
2443440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald
25c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon    def test_conversion(self):
26c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        # Make sure __str__() behaves properly
27c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo0:
28c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __unicode__(self):
29c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foo"
30c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
31c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo1:
32c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
33c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "foo"
34c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
35c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo2(object):
36c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
37c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "foo"
38c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
39c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo3(object):
40c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
41c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foo"
42c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
43c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo4(unicode):
44c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
45c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foo"
46c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
47c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo5(str):
48c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
49c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foo"
50c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
51c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo6(str):
52c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
53c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "foos"
54c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
55c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __unicode__(self):
56c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foou"
57c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
58c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo7(unicode):
59c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
60c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "foos"
61c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __unicode__(self):
62c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return u"foou"
63c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
64c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo8(str):
65c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __new__(cls, content=""):
66c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return str.__new__(cls, 2*content)
67c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
68c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return self
69c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
70c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        class Foo9(str):
71c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __str__(self):
72c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "string"
73c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon            def __unicode__(self):
74c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon                return "not unicode"
75c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
76c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__
77c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo1()), "foo")
78c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo2()), "foo")
79c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo3()), "foo")
80c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo4("bar")), "foo")
81c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo5("bar")), "foo")
82c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo6("bar")), "foos")
83c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo7("bar")), "foos")
84c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo8("foo")), "foofoo")
85c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(str(Foo9("foo")), "string")
86c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon        self.assertEqual(unicode(Foo9("foo")), u"not unicode")
87c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon
887dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz    def test_expandtabs_overflows_gracefully(self):
897dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz        # This test only affects 32-bit platforms because expandtabs can only take
907dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz        # an int as the max value, not a 64-bit C long.  If expandtabs is changed
917dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz        # to take a 64-bit long, this test should apply to all platforms.
92ba965deea89299611454e43be005df357b75b11dNeal Norwitz        if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
937dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz            return
947dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz        self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint)
957dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz
967dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz
970fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwalddef test_main():
9821d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald    test_support.run_unittest(StrTest)
990fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald
1000fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldif __name__ == "__main__":
1010fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald    test_main()
102