Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1 | import unittest, string |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 3 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 4 | class ModuleTest(unittest.TestCase): |
| 5 | |
| 6 | def test_attrs(self): |
R David Murray | 0a8f43e | 2015-04-13 20:04:29 -0400 | [diff] [blame^] | 7 | # While the exact order of the items in these attributes is not |
| 8 | # technically part of the "language spec", in practice there is almost |
| 9 | # certainly user code that depends on the order, so de-facto it *is* |
| 10 | # part of the spec. |
| 11 | self.assertEqual(string.whitespace, ' \t\n\r\x0b\x0c') |
| 12 | self.assertEqual(string.ascii_lowercase, 'abcdefghijklmnopqrstuvwxyz') |
| 13 | self.assertEqual(string.ascii_uppercase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
| 14 | self.assertEqual(string.ascii_letters, string.ascii_lowercase + string.ascii_uppercase) |
| 15 | self.assertEqual(string.digits, '0123456789') |
| 16 | self.assertEqual(string.hexdigits, string.digits + 'abcdefABCDEF') |
| 17 | self.assertEqual(string.octdigits, '01234567') |
| 18 | self.assertEqual(string.punctuation, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') |
| 19 | self.assertEqual(string.printable, string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.whitespace) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 20 | |
Ezio Melotti | 2c6a949 | 2009-09-26 12:19:30 +0000 | [diff] [blame] | 21 | def test_capwords(self): |
| 22 | self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi') |
| 23 | self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi') |
| 24 | self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi') |
| 25 | self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') |
| 26 | self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') |
| 27 | self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi') |
Ezio Melotti | a40bdda | 2009-09-26 12:33:22 +0000 | [diff] [blame] | 28 | self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def') |
| 29 | self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def') |
| 30 | self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t') |
Ezio Melotti | 2c6a949 | 2009-09-26 12:19:30 +0000 | [diff] [blame] | 31 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 32 | def test_basic_formatter(self): |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 33 | fmt = string.Formatter() |
| 34 | self.assertEqual(fmt.format("foo"), "foo") |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 35 | self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") |
| 36 | self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") |
Serhiy Storchaka | 8ffe917 | 2015-03-24 22:28:43 +0200 | [diff] [blame] | 37 | self.assertRaises(TypeError, fmt.format) |
| 38 | self.assertRaises(TypeError, string.Formatter.format) |
| 39 | |
| 40 | def test_format_keyword_arguments(self): |
| 41 | fmt = string.Formatter() |
| 42 | self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-') |
| 43 | self.assertRaises(KeyError, fmt.format, "-{arg}-") |
| 44 | self.assertEqual(fmt.format("-{self}-", self='test'), '-test-') |
| 45 | self.assertRaises(KeyError, fmt.format, "-{self}-") |
| 46 | self.assertEqual(fmt.format("-{format_string}-", format_string='test'), |
| 47 | '-test-') |
| 48 | self.assertRaises(KeyError, fmt.format, "-{format_string}-") |
Serhiy Storchaka | b876df4 | 2015-03-24 22:30:46 +0200 | [diff] [blame] | 49 | with self.assertWarnsRegex(DeprecationWarning, "format_string"): |
| 50 | self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"), |
| 51 | '-test-') |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 52 | |
Eric V. Smith | 7ce9074 | 2014-04-14 16:43:50 -0400 | [diff] [blame] | 53 | def test_auto_numbering(self): |
| 54 | fmt = string.Formatter() |
| 55 | self.assertEqual(fmt.format('foo{}{}', 'bar', 6), |
| 56 | 'foo{}{}'.format('bar', 6)) |
| 57 | self.assertEqual(fmt.format('foo{1}{num}{1}', None, 'bar', num=6), |
| 58 | 'foo{1}{num}{1}'.format(None, 'bar', num=6)) |
| 59 | self.assertEqual(fmt.format('{:^{}}', 'bar', 6), |
| 60 | '{:^{}}'.format('bar', 6)) |
| 61 | self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6), |
| 62 | '{:^{pad}}{}'.format('foo', 'bar', pad=6)) |
| 63 | |
| 64 | with self.assertRaises(ValueError): |
| 65 | fmt.format('foo{1}{}', 'bar', 6) |
| 66 | |
| 67 | with self.assertRaises(ValueError): |
| 68 | fmt.format('foo{}{1}', 'bar', 6) |
| 69 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 70 | def test_conversion_specifiers(self): |
| 71 | fmt = string.Formatter() |
| 72 | self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") |
| 73 | self.assertEqual(fmt.format("{0!s}", 'test'), 'test') |
| 74 | self.assertRaises(ValueError, fmt.format, "{0!h}", 'test') |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 75 | # issue13579 |
| 76 | self.assertEqual(fmt.format("{0!a}", 42), '42') |
| 77 | self.assertEqual(fmt.format("{0!a}", string.ascii_letters), |
| 78 | "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'") |
| 79 | self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'") |
| 80 | self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'") |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 81 | |
| 82 | def test_name_lookup(self): |
| 83 | fmt = string.Formatter() |
| 84 | class AnyAttr: |
| 85 | def __getattr__(self, attr): |
| 86 | return attr |
| 87 | x = AnyAttr() |
| 88 | self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack') |
| 89 | with self.assertRaises(AttributeError): |
| 90 | fmt.format("{0.lumber}{0.jack}", '') |
| 91 | |
| 92 | def test_index_lookup(self): |
| 93 | fmt = string.Formatter() |
| 94 | lookup = ["eggs", "and", "spam"] |
| 95 | self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') |
| 96 | with self.assertRaises(IndexError): |
| 97 | fmt.format("{0[2]}{0[0]}", []) |
| 98 | with self.assertRaises(KeyError): |
| 99 | fmt.format("{0[2]}{0[0]}", {}) |
| 100 | |
| 101 | def test_override_get_value(self): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 102 | class NamespaceFormatter(string.Formatter): |
| 103 | def __init__(self, namespace={}): |
| 104 | string.Formatter.__init__(self) |
| 105 | self.namespace = namespace |
| 106 | |
| 107 | def get_value(self, key, args, kwds): |
| 108 | if isinstance(key, str): |
| 109 | try: |
| 110 | # Check explicitly passed arguments first |
| 111 | return kwds[key] |
| 112 | except KeyError: |
| 113 | return self.namespace[key] |
| 114 | else: |
| 115 | string.Formatter.get_value(key, args, kwds) |
| 116 | |
| 117 | fmt = NamespaceFormatter({'greeting':'hello'}) |
| 118 | self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 119 | |
| 120 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 121 | def test_override_format_field(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 122 | class CallFormatter(string.Formatter): |
| 123 | def format_field(self, value, format_spec): |
| 124 | return format(value(), format_spec) |
| 125 | |
| 126 | fmt = CallFormatter() |
| 127 | self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') |
| 128 | |
| 129 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 130 | def test_override_convert_field(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 131 | class XFormatter(string.Formatter): |
| 132 | def convert_field(self, value, conversion): |
| 133 | if conversion == 'x': |
| 134 | return None |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 135 | return super().convert_field(value, conversion) |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 136 | |
| 137 | fmt = XFormatter() |
| 138 | self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") |
| 139 | |
| 140 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 141 | def test_override_parse(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 142 | class BarFormatter(string.Formatter): |
| 143 | # returns an iterable that contains tuples of the form: |
| 144 | # (literal_text, field_name, format_spec, conversion) |
| 145 | def parse(self, format_string): |
| 146 | for field in format_string.split('|'): |
| 147 | if field[0] == '+': |
| 148 | # it's markup |
| 149 | field_name, _, format_spec = field[1:].partition(':') |
| 150 | yield '', field_name, format_spec, None |
| 151 | else: |
| 152 | yield field, None, None, None |
| 153 | |
| 154 | fmt = BarFormatter() |
| 155 | self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *') |
| 156 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 157 | def test_check_unused_args(self): |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 158 | class CheckAllUsedFormatter(string.Formatter): |
| 159 | def check_unused_args(self, used_args, args, kwargs): |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 160 | # Track which arguments actually got used |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 161 | unused_args = set(kwargs.keys()) |
| 162 | unused_args.update(range(0, len(args))) |
| 163 | |
| 164 | for arg in used_args: |
| 165 | unused_args.remove(arg) |
| 166 | |
| 167 | if unused_args: |
| 168 | raise ValueError("unused arguments") |
| 169 | |
| 170 | fmt = CheckAllUsedFormatter() |
| 171 | self.assertEqual(fmt.format("{0}", 10), "10") |
| 172 | self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100") |
| 173 | self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020") |
| 174 | self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0) |
| 175 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20) |
| 176 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) |
| 177 | self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) |
| 178 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 179 | def test_vformat_recursion_limit(self): |
| 180 | fmt = string.Formatter() |
| 181 | args = () |
| 182 | kwargs = dict(i=100) |
| 183 | with self.assertRaises(ValueError) as err: |
| 184 | fmt._vformat("{i}", args, kwargs, set(), -1) |
| 185 | self.assertIn("recursion", str(err.exception)) |
Nick Coghlan | d25fd4d | 2011-03-15 08:54:37 +1000 | [diff] [blame] | 186 | |
| 187 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 188 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 189 | unittest.main() |