Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_str.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 3ceeccbc2c3b |
| children | 21fb1767e185 |
| rev | line source |
|---|---|
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
1 |
|
42023
dde7c37f4536
Prevent these tests from running on Win64 since they don\'t apply there either
Neal Norwitz <nnorwitz@gmail.com>
parents:
42004
diff
changeset
|
2 import struct |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
3 import sys |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
4 from test import test_support, string_tests |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
5 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
6 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
7 class StrTest( |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
8 string_tests.CommonTest, |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
9 string_tests.MixinStrUnicodeUserStringTest, |
|
33298
a37ae7dba814
Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
10 string_tests.MixinStrUserStringTest, |
|
a37ae7dba814
Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
11 string_tests.MixinStrUnicodeTest, |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
12 ): |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
13 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
14 type2test = str |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
15 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
16 # We don't need to propagate to str |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
17 def fixtype(self, obj): |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
18 return obj |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
19 |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
20 def test_basic_creation(self): |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
21 self.assertEqual(str(''), '') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
22 self.assertEqual(str(0), '0') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
23 self.assertEqual(str(0L), '0') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
24 self.assertEqual(str(()), '()') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
25 self.assertEqual(str([]), '[]') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
26 self.assertEqual(str({}), '{}') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
27 a = [] |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
28 a.append(a) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
29 self.assertEqual(str(a), '[[...]]') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
30 a = {} |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
31 a[0] = a |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
32 self.assertEqual(str(a), '{0: {...}}') |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
33 |
|
28478
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
28009
diff
changeset
|
34 def test_formatting(self): |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
28009
diff
changeset
|
35 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
28009
diff
changeset
|
36 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
28009
diff
changeset
|
37 |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
38 def test_conversion(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
39 # Make sure __str__() behaves properly |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
40 class Foo0: |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
41 def __unicode__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
42 return u"foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
43 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
44 class Foo1: |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
45 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
46 return "foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
47 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
48 class Foo2(object): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
49 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
50 return "foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
51 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
52 class Foo3(object): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
53 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
54 return u"foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
55 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
56 class Foo4(unicode): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
57 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
58 return u"foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
59 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
60 class Foo5(str): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
61 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
62 return u"foo" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
63 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
64 class Foo6(str): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
65 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
66 return "foos" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
67 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
68 def __unicode__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
69 return u"foou" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
70 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
71 class Foo7(unicode): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
72 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
73 return "foos" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
74 def __unicode__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
75 return u"foou" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
76 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
77 class Foo8(str): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
78 def __new__(cls, content=""): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
79 return str.__new__(cls, 2*content) |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
80 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
81 return self |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
82 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
83 class Foo9(str): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
84 def __str__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
85 return "string" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
86 def __unicode__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
87 return "not unicode" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
88 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54267
diff
changeset
|
89 self.assertTrue(str(Foo0()).startswith("<")) # this is different from __unicode__ |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
90 self.assertEqual(str(Foo1()), "foo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
91 self.assertEqual(str(Foo2()), "foo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
92 self.assertEqual(str(Foo3()), "foo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
93 self.assertEqual(str(Foo4("bar")), "foo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
94 self.assertEqual(str(Foo5("bar")), "foo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
95 self.assertEqual(str(Foo6("bar")), "foos") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
96 self.assertEqual(str(Foo7("bar")), "foos") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
97 self.assertEqual(str(Foo8("foo")), "foofoo") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
98 self.assertEqual(str(Foo9("foo")), "string") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
99 self.assertEqual(unicode(Foo9("foo")), u"not unicode") |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33298
diff
changeset
|
100 |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
101 def test_expandtabs_overflows_gracefully(self): |
|
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
102 # This test only affects 32-bit platforms because expandtabs can only take |
|
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
103 # an int as the max value, not a 64-bit C long. If expandtabs is changed |
|
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
104 # to take a 64-bit long, this test should apply to all platforms. |
|
42023
dde7c37f4536
Prevent these tests from running on Win64 since they don\'t apply there either
Neal Norwitz <nnorwitz@gmail.com>
parents:
42004
diff
changeset
|
105 if sys.maxint > (1 << 32) or struct.calcsize('P') != 4: |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
106 return |
|
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
107 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint) |
|
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
108 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
109 def test__format__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
110 def test(value, format, expected): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
111 # test both with and without the trailing 's' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
112 self.assertEqual(value.__format__(format), expected) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
113 self.assertEqual(value.__format__(format + 's'), expected) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
114 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
115 test('', '', '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
116 test('abc', '', 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
117 test('abc', '.3', 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
118 test('ab', '.3', 'ab') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
119 test('abcdef', '.3', 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
120 test('abcdef', '.0', '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
121 test('abc', '3.3', 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
122 test('abc', '2.3', 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
123 test('abc', '2.2', 'ab') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
124 test('abc', '3.2', 'ab ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
125 test('result', 'x<0', 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
126 test('result', 'x<5', 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
127 test('result', 'x<6', 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
128 test('result', 'x<7', 'resultx') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
129 test('result', 'x<8', 'resultxx') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
130 test('result', ' <7', 'result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
131 test('result', '<7', 'result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
132 test('result', '>7', ' result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
133 test('result', '>8', ' result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
134 test('result', '^8', ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
135 test('result', '^9', ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
136 test('result', '^10', ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
137 test('a', '10000', 'a' + ' ' * 9999) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
138 test('', '10000', ' ' * 10000) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
139 test('', '10000000', ' ' * 10000000) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
140 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
141 def test_format(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
142 self.assertEqual(''.format(), '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
143 self.assertEqual('a'.format(), 'a') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
144 self.assertEqual('ab'.format(), 'ab') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
145 self.assertEqual('a{{'.format(), 'a{') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
146 self.assertEqual('a}}'.format(), 'a}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
147 self.assertEqual('{{b'.format(), '{b') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
148 self.assertEqual('}}b'.format(), '}b') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
149 self.assertEqual('a{{b'.format(), 'a{b') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
150 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
151 # examples from the PEP: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
152 import datetime |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
153 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
154 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')), |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
155 "My name is Fred") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
156 self.assertEqual("My name is {0} :-{{}}".format('Fred'), |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
157 "My name is Fred :-{}") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
158 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
159 d = datetime.date(2007, 8, 18) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
160 self.assertEqual("The year is {0.year}".format(d), |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
161 "The year is 2007") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
162 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
163 # classes we'll use for testing |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
164 class C: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
165 def __init__(self, x=100): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
166 self._x = x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
167 def __format__(self, spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
168 return spec |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
169 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
170 class D: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
171 def __init__(self, x): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
172 self.x = x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
173 def __format__(self, spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
174 return str(self.x) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
175 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
176 # class with __str__, but no __format__ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
177 class E: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
178 def __init__(self, x): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
179 self.x = x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
180 def __str__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
181 return 'E(' + self.x + ')' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
182 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
183 # class with __repr__, but no __format__ or __str__ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
184 class F: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
185 def __init__(self, x): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
186 self.x = x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
187 def __repr__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
188 return 'F(' + self.x + ')' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
189 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
190 # class with __format__ that forwards to string, for some format_spec's |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
191 class G: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
192 def __init__(self, x): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
193 self.x = x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
194 def __str__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
195 return "string is " + self.x |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
196 def __format__(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
197 if format_spec == 'd': |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
198 return 'G(' + self.x + ')' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
199 return object.__format__(self, format_spec) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
200 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
201 # class that returns a bad type from __format__ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
202 class H: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
203 def __format__(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
204 return 1.0 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
205 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
206 class I(datetime.date): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
207 def __format__(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
208 return self.strftime(format_spec) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
209 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
210 class J(int): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
211 def __format__(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
212 return int.__format__(self * 2, format_spec) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
213 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
214 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
215 self.assertEqual(''.format(), '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
216 self.assertEqual('abc'.format(), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
217 self.assertEqual('{0}'.format('abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
218 self.assertEqual('{0:}'.format('abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
219 self.assertEqual('X{0}'.format('abc'), 'Xabc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
220 self.assertEqual('{0}X'.format('abc'), 'abcX') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
221 self.assertEqual('X{0}Y'.format('abc'), 'XabcY') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
222 self.assertEqual('{1}'.format(1, 'abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
223 self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
224 self.assertEqual('{1}X'.format(1, 'abc'), 'abcX') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
225 self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
226 self.assertEqual('{0}'.format(-15), '-15') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
227 self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
228 self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
229 self.assertEqual('{{'.format(), '{') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
230 self.assertEqual('}}'.format(), '}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
231 self.assertEqual('{{}}'.format(), '{}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
232 self.assertEqual('{{x}}'.format(), '{x}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
233 self.assertEqual('{{{0}}}'.format(123), '{123}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
234 self.assertEqual('{{{{0}}}}'.format(), '{{0}}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
235 self.assertEqual('}}{{'.format(), '}{') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
236 self.assertEqual('}}x{{'.format(), '}x{') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
237 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
238 # weird field names |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
239 self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
240 self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
241 self.assertEqual("{0[ ]}".format({' ':3}), '3') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
242 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
243 self.assertEqual('{foo._x}'.format(foo=C(20)), '20') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
244 self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
245 self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
246 self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
247 self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
248 self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
249 self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
250 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
251 # strings |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
252 self.assertEqual('{0:.3s}'.format('abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
253 self.assertEqual('{0:.3s}'.format('ab'), 'ab') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
254 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
255 self.assertEqual('{0:.0s}'.format('abcdef'), '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
256 self.assertEqual('{0:3.3s}'.format('abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
257 self.assertEqual('{0:2.3s}'.format('abc'), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
258 self.assertEqual('{0:2.2s}'.format('abc'), 'ab') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
259 self.assertEqual('{0:3.2s}'.format('abc'), 'ab ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
260 self.assertEqual('{0:x<0s}'.format('result'), 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
261 self.assertEqual('{0:x<5s}'.format('result'), 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
262 self.assertEqual('{0:x<6s}'.format('result'), 'result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
263 self.assertEqual('{0:x<7s}'.format('result'), 'resultx') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
264 self.assertEqual('{0:x<8s}'.format('result'), 'resultxx') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
265 self.assertEqual('{0: <7s}'.format('result'), 'result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
266 self.assertEqual('{0:<7s}'.format('result'), 'result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
267 self.assertEqual('{0:>7s}'.format('result'), ' result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
268 self.assertEqual('{0:>8s}'.format('result'), ' result') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
269 self.assertEqual('{0:^8s}'.format('result'), ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
270 self.assertEqual('{0:^9s}'.format('result'), ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
271 self.assertEqual('{0:^10s}'.format('result'), ' result ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
272 self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
273 self.assertEqual('{0:10000}'.format(''), ' ' * 10000) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
274 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
275 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
276 # format specifiers for user defined type |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
277 self.assertEqual('{0:abc}'.format(C()), 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
278 |
|
64707
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
279 # !r and !s coercions |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
280 self.assertEqual('{0!s}'.format('Hello'), 'Hello') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
281 self.assertEqual('{0!s:}'.format('Hello'), 'Hello') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
282 self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
283 self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
284 self.assertEqual('{0!r}'.format('Hello'), "'Hello'") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
285 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
286 self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
287 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
288 # test fallback to object.__format__ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
289 self.assertEqual('{0}'.format({}), '{}') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
290 self.assertEqual('{0}'.format([]), '[]') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
291 self.assertEqual('{0}'.format([1]), '[1]') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
292 self.assertEqual('{0}'.format(E('data')), 'E(data)') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
293 self.assertEqual('{0:d}'.format(G('data')), 'G(data)') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
294 self.assertEqual('{0!s}'.format(G('data')), 'string is data') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
295 |
|
64707
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
296 msg = 'object.__format__ with a non-empty format string is deprecated' |
|
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
297 with test_support.check_warnings((msg, PendingDeprecationWarning)): |
|
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
298 self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') |
|
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
299 self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') |
|
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
300 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') |
|
274a0bf46871
Check PendingDeprecationWarning after issue #7994.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56093
diff
changeset
|
301 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
302 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
303 month=8, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
304 day=27)), |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
305 "date: 2007-08-27") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
306 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
307 # test deriving from a builtin type and overriding __format__ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
308 self.assertEqual("{0}".format(J(10)), "20") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
309 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
310 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
311 # string format specifiers |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
312 self.assertEqual('{0:}'.format('a'), 'a') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
313 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
314 # computed format specifiers |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
315 self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
316 self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
317 self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
318 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
319 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
320 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
321 # test various errors |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
322 self.assertRaises(ValueError, '{'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
323 self.assertRaises(ValueError, '}'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
324 self.assertRaises(ValueError, 'a{'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
325 self.assertRaises(ValueError, 'a}'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
326 self.assertRaises(ValueError, '{a'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
327 self.assertRaises(ValueError, '}a'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
328 self.assertRaises(IndexError, '{0}'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
329 self.assertRaises(IndexError, '{1}'.format, 'abc') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
330 self.assertRaises(KeyError, '{x}'.format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
331 self.assertRaises(ValueError, "}{".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
332 self.assertRaises(ValueError, "{".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
333 self.assertRaises(ValueError, "}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
334 self.assertRaises(ValueError, "abc{0:{}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
335 self.assertRaises(ValueError, "{0".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
336 self.assertRaises(IndexError, "{0.}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
337 self.assertRaises(ValueError, "{0.}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
338 self.assertRaises(IndexError, "{0[}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
339 self.assertRaises(ValueError, "{0[}".format, []) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
340 self.assertRaises(KeyError, "{0]}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
341 self.assertRaises(ValueError, "{0.[]}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
342 self.assertRaises(ValueError, "{0..foo}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
343 self.assertRaises(ValueError, "{0[0}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
344 self.assertRaises(ValueError, "{0[0:foo}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
345 self.assertRaises(KeyError, "{c]}".format) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
346 self.assertRaises(ValueError, "{{ {{{0}}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
347 self.assertRaises(ValueError, "{0}}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
348 self.assertRaises(KeyError, "{foo}".format, bar=3) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
349 self.assertRaises(ValueError, "{0!x}".format, 3) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
350 self.assertRaises(ValueError, "{0!}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
351 self.assertRaises(ValueError, "{0!rs}".format, 0) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
352 self.assertRaises(ValueError, "{!}".format) |
|
52157
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
353 self.assertRaises(IndexError, "{:}".format) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
354 self.assertRaises(IndexError, "{:s}".format) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
355 self.assertRaises(IndexError, "{}".format) |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
356 |
|
54268
d2d028f65c7c
Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
357 # issue 6089 |
|
d2d028f65c7c
Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
358 self.assertRaises(ValueError, "{0[0]x}".format, [None]) |
|
d2d028f65c7c
Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
359 self.assertRaises(ValueError, "{0[0](10)}".format, [None]) |
|
d2d028f65c7c
Merged revisions 72848 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
360 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
361 # can't have a replacement on the field name portion |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
362 self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
363 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
364 # exceed maximum recursion depth |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
365 self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
366 self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
367 0, 1, 2, 3, 4, 5, 6, 7) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
368 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
369 # string format spec errors |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
370 self.assertRaises(ValueError, "{0:-s}".format, '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
371 self.assertRaises(ValueError, format, "", "-") |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
372 self.assertRaises(ValueError, "{0:=s}".format, '') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42023
diff
changeset
|
373 |
|
52157
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
374 def test_format_auto_numbering(self): |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
375 class C: |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
376 def __init__(self, x=100): |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
377 self._x = x |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
378 def __format__(self, spec): |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
379 return spec |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
380 |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
381 self.assertEqual('{}'.format(10), '10') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
382 self.assertEqual('{:5}'.format('s'), 's ') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
383 self.assertEqual('{!r}'.format('s'), "'s'") |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
384 self.assertEqual('{._x}'.format(C(10)), '10') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
385 self.assertEqual('{[1]}'.format([1, 2]), '2') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
386 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
387 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
388 |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
389 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
390 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
391 |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
392 # can't mix and match numbering and auto-numbering |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
393 self.assertRaises(ValueError, '{}{1}'.format, 1, 2) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
394 self.assertRaises(ValueError, '{1}{}'.format, 1, 2) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
395 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
396 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2) |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
397 |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
398 # can mix and match auto-numbering and named |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
399 self.assertEqual('{f}{}'.format(4, f='test'), 'test4') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
400 self.assertEqual('{}{f}'.format(4, f='test'), '4test') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
401 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
402 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') |
|
3f18d609ac1b
Issue 5237, Allow auto-numbered replacement fields in str.format() strings.
Eric Smith <eric@trueblade.com>
parents:
48139
diff
changeset
|
403 |
|
48139
97607fcb7b26
Preemptively backport the relevant parts of r65420
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
404 def test_buffer_is_readonly(self): |
|
97607fcb7b26
Preemptively backport the relevant parts of r65420
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
405 self.assertRaises(TypeError, sys.stdin.readinto, b"") |
|
97607fcb7b26
Preemptively backport the relevant parts of r65420
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
406 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
407 def test_encode_and_decode_kwargs(self): |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
408 self.assertEqual('abcde'.encode('ascii', 'replace'), |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
409 'abcde'.encode('ascii', errors='replace')) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
410 self.assertEqual('abcde'.encode('ascii', 'ignore'), |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
411 'abcde'.encode(encoding='ascii', errors='ignore')) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
412 self.assertEqual('Andr\202 x'.decode('ascii', 'ignore'), |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
413 'Andr\202 x'.decode('ascii', errors='ignore')) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
414 self.assertEqual('Andr\202 x'.decode('ascii', 'replace'), |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
415 'Andr\202 x'.decode(encoding='ascii', errors='replace')) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
416 |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
417 def test_startswith_endswith_errors(self): |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
418 with self.assertRaises(UnicodeDecodeError): |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
419 '\xff'.startswith(u'x') |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
420 with self.assertRaises(UnicodeDecodeError): |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
421 '\xff'.endswith(u'x') |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
422 for meth in ('foo'.startswith, 'foo'.endswith): |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
423 with self.assertRaises(TypeError) as cm: |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
424 meth(['f']) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
425 exc = str(cm.exception) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
426 self.assertIn('unicode', exc) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
427 self.assertIn('str', exc) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
68256
diff
changeset
|
428 self.assertIn('tuple', exc) |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
34931
diff
changeset
|
429 |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
430 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28478
diff
changeset
|
431 test_support.run_unittest(StrTest) |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
432 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
433 if __name__ == "__main__": |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
434 test_main() |
