Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1 | import unittest, string |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 3 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 5 | class ModuleTest(unittest.TestCase): |
| 6 | |
| 7 | def test_attrs(self): |
| 8 | string.whitespace |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 9 | string.ascii_lowercase |
| 10 | string.ascii_uppercase |
| 11 | string.ascii_letters |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 12 | string.digits |
| 13 | string.hexdigits |
| 14 | string.octdigits |
| 15 | string.punctuation |
| 16 | string.printable |
| 17 | |
Ezio Melotti | 2c6a949 | 2009-09-26 12:19:30 +0000 | [diff] [blame] | 18 | def test_capwords(self): |
| 19 | self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi') |
| 20 | self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi') |
| 21 | self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi') |
| 22 | self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') |
| 23 | self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') |
| 24 | 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] | 25 | self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def') |
| 26 | self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def') |
| 27 | self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t') |
Ezio Melotti | 2c6a949 | 2009-09-26 12:19:30 +0000 | [diff] [blame] | 28 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 29 | def test_basic_formatter(self): |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 30 | fmt = string.Formatter() |
| 31 | self.assertEqual(fmt.format("foo"), "foo") |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 32 | self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") |
| 33 | self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 34 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 35 | def test_conversion_specifiers(self): |
| 36 | fmt = string.Formatter() |
| 37 | self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") |
| 38 | self.assertEqual(fmt.format("{0!s}", 'test'), 'test') |
| 39 | self.assertRaises(ValueError, fmt.format, "{0!h}", 'test') |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 40 | # issue13579 |
| 41 | self.assertEqual(fmt.format("{0!a}", 42), '42') |
| 42 | self.assertEqual(fmt.format("{0!a}", string.ascii_letters), |
| 43 | "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'") |
| 44 | self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'") |
| 45 | self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'") |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 46 | |
| 47 | def test_name_lookup(self): |
| 48 | fmt = string.Formatter() |
| 49 | class AnyAttr: |
| 50 | def __getattr__(self, attr): |
| 51 | return attr |
| 52 | x = AnyAttr() |
| 53 | self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack') |
| 54 | with self.assertRaises(AttributeError): |
| 55 | fmt.format("{0.lumber}{0.jack}", '') |
| 56 | |
| 57 | def test_index_lookup(self): |
| 58 | fmt = string.Formatter() |
| 59 | lookup = ["eggs", "and", "spam"] |
| 60 | self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') |
| 61 | with self.assertRaises(IndexError): |
| 62 | fmt.format("{0[2]}{0[0]}", []) |
| 63 | with self.assertRaises(KeyError): |
| 64 | fmt.format("{0[2]}{0[0]}", {}) |
| 65 | |
| 66 | def test_override_get_value(self): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 67 | class NamespaceFormatter(string.Formatter): |
| 68 | def __init__(self, namespace={}): |
| 69 | string.Formatter.__init__(self) |
| 70 | self.namespace = namespace |
| 71 | |
| 72 | def get_value(self, key, args, kwds): |
| 73 | if isinstance(key, str): |
| 74 | try: |
| 75 | # Check explicitly passed arguments first |
| 76 | return kwds[key] |
| 77 | except KeyError: |
| 78 | return self.namespace[key] |
| 79 | else: |
| 80 | string.Formatter.get_value(key, args, kwds) |
| 81 | |
| 82 | fmt = NamespaceFormatter({'greeting':'hello'}) |
| 83 | self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 84 | |
| 85 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 86 | def test_override_format_field(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 87 | class CallFormatter(string.Formatter): |
| 88 | def format_field(self, value, format_spec): |
| 89 | return format(value(), format_spec) |
| 90 | |
| 91 | fmt = CallFormatter() |
| 92 | self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') |
| 93 | |
| 94 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 95 | def test_override_convert_field(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 96 | class XFormatter(string.Formatter): |
| 97 | def convert_field(self, value, conversion): |
| 98 | if conversion == 'x': |
| 99 | return None |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 100 | return super().convert_field(value, conversion) |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 101 | |
| 102 | fmt = XFormatter() |
| 103 | self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") |
| 104 | |
| 105 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 106 | def test_override_parse(self): |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 107 | class BarFormatter(string.Formatter): |
| 108 | # returns an iterable that contains tuples of the form: |
| 109 | # (literal_text, field_name, format_spec, conversion) |
| 110 | def parse(self, format_string): |
| 111 | for field in format_string.split('|'): |
| 112 | if field[0] == '+': |
| 113 | # it's markup |
| 114 | field_name, _, format_spec = field[1:].partition(':') |
| 115 | yield '', field_name, format_spec, None |
| 116 | else: |
| 117 | yield field, None, None, None |
| 118 | |
| 119 | fmt = BarFormatter() |
| 120 | self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *') |
| 121 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 122 | def test_check_unused_args(self): |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 123 | class CheckAllUsedFormatter(string.Formatter): |
| 124 | def check_unused_args(self, used_args, args, kwargs): |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 125 | # Track which arguments actually got used |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 126 | unused_args = set(kwargs.keys()) |
| 127 | unused_args.update(range(0, len(args))) |
| 128 | |
| 129 | for arg in used_args: |
| 130 | unused_args.remove(arg) |
| 131 | |
| 132 | if unused_args: |
| 133 | raise ValueError("unused arguments") |
| 134 | |
| 135 | fmt = CheckAllUsedFormatter() |
| 136 | self.assertEqual(fmt.format("{0}", 10), "10") |
| 137 | self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100") |
| 138 | self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020") |
| 139 | self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0) |
| 140 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20) |
| 141 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) |
| 142 | self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) |
| 143 | |
Nick Coghlan | 62ecb6a | 2011-05-31 19:40:11 +1000 | [diff] [blame] | 144 | def test_vformat_recursion_limit(self): |
| 145 | fmt = string.Formatter() |
| 146 | args = () |
| 147 | kwargs = dict(i=100) |
| 148 | with self.assertRaises(ValueError) as err: |
| 149 | fmt._vformat("{i}", args, kwargs, set(), -1) |
| 150 | self.assertIn("recursion", str(err.exception)) |
Nick Coghlan | d25fd4d | 2011-03-15 08:54:37 +1000 | [diff] [blame] | 151 | |
| 152 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 153 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 154 | support.run_unittest(ModuleTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 155 | |
| 156 | if __name__ == "__main__": |
| 157 | test_main() |