blob: 226a16809ecd76644e728ce27fbc5cf73c1090cc [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001import unittest
2from test import test_support, string_tests
3
4
5class StrTest(
6 string_tests.CommonTest,
7 string_tests.MixinStrUnicodeUserStringTest,
Walter Dörwald57d88e52004-08-26 16:53:04 +00008 string_tests.MixinStrUserStringTest,
9 string_tests.MixinStrUnicodeTest,
Walter Dörwald0fd583c2003-02-21 12:53:50 +000010 ):
11
12 type2test = str
13
14 # We don't need to propagate to str
15 def fixtype(self, obj):
16 return obj
17
Walter Dörwald43440a62003-03-31 18:07:50 +000018 def test_formatting(self):
19 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
20 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
21
Guido van Rossum49d6b072006-08-17 21:11:47 +000022 def test_iterators(self):
23 # Make sure str objects have an __iter__ method
24 it = "abc".__iter__()
25 self.assertEqual(it.next(), "a")
26 self.assertEqual(it.next(), "b")
27 self.assertEqual(it.next(), "c")
28 self.assertRaises(StopIteration, it.next)
29
Brett Cannonc3647ac2005-04-26 03:45:26 +000030 def test_conversion(self):
31 # Make sure __str__() behaves properly
32 class Foo0:
33 def __unicode__(self):
34 return u"foo"
35
36 class Foo1:
37 def __str__(self):
38 return "foo"
39
40 class Foo2(object):
41 def __str__(self):
42 return "foo"
43
44 class Foo3(object):
45 def __str__(self):
46 return u"foo"
47
48 class Foo4(unicode):
49 def __str__(self):
50 return u"foo"
51
52 class Foo5(str):
53 def __str__(self):
54 return u"foo"
55
56 class Foo6(str):
57 def __str__(self):
58 return "foos"
59
60 def __unicode__(self):
61 return u"foou"
62
63 class Foo7(unicode):
64 def __str__(self):
65 return "foos"
66 def __unicode__(self):
67 return u"foou"
68
69 class Foo8(str):
70 def __new__(cls, content=""):
71 return str.__new__(cls, 2*content)
72 def __str__(self):
73 return self
74
75 class Foo9(str):
76 def __str__(self):
77 return "string"
78 def __unicode__(self):
79 return "not unicode"
80
81 self.assert_(str(Foo0()).startswith("<")) # this is different from __unicode__
82 self.assertEqual(str(Foo1()), "foo")
83 self.assertEqual(str(Foo2()), "foo")
84 self.assertEqual(str(Foo3()), "foo")
85 self.assertEqual(str(Foo4("bar")), "foo")
86 self.assertEqual(str(Foo5("bar")), "foo")
87 self.assertEqual(str(Foo6("bar")), "foos")
88 self.assertEqual(str(Foo7("bar")), "foos")
89 self.assertEqual(str(Foo8("foo")), "foofoo")
90 self.assertEqual(str(Foo9("foo")), "string")
91 self.assertEqual(unicode(Foo9("foo")), u"not unicode")
92
Walter Dörwald0fd583c2003-02-21 12:53:50 +000093def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000094 test_support.run_unittest(StrTest)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000095
96if __name__ == "__main__":
97 test_main()