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 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 18 | def test_formatter(self): |
| 19 | fmt = string.Formatter() |
| 20 | self.assertEqual(fmt.format("foo"), "foo") |
| 21 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 22 | self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") |
| 23 | self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") |
| 24 | self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") |
| 25 | |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 26 | # override get_value ############################################ |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 27 | class NamespaceFormatter(string.Formatter): |
| 28 | def __init__(self, namespace={}): |
| 29 | string.Formatter.__init__(self) |
| 30 | self.namespace = namespace |
| 31 | |
| 32 | def get_value(self, key, args, kwds): |
| 33 | if isinstance(key, str): |
| 34 | try: |
| 35 | # Check explicitly passed arguments first |
| 36 | return kwds[key] |
| 37 | except KeyError: |
| 38 | return self.namespace[key] |
| 39 | else: |
| 40 | string.Formatter.get_value(key, args, kwds) |
| 41 | |
| 42 | fmt = NamespaceFormatter({'greeting':'hello'}) |
| 43 | self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 44 | |
| 45 | |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 46 | # override format_field ######################################### |
| 47 | class CallFormatter(string.Formatter): |
| 48 | def format_field(self, value, format_spec): |
| 49 | return format(value(), format_spec) |
| 50 | |
| 51 | fmt = CallFormatter() |
| 52 | self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') |
| 53 | |
| 54 | |
| 55 | # override convert_field ######################################## |
| 56 | class XFormatter(string.Formatter): |
| 57 | def convert_field(self, value, conversion): |
| 58 | if conversion == 'x': |
| 59 | return None |
| 60 | return super(XFormatter, self).convert_field(value, conversion) |
| 61 | |
| 62 | fmt = XFormatter() |
| 63 | self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") |
| 64 | |
| 65 | |
| 66 | # override parse ################################################ |
| 67 | class BarFormatter(string.Formatter): |
| 68 | # returns an iterable that contains tuples of the form: |
| 69 | # (literal_text, field_name, format_spec, conversion) |
| 70 | def parse(self, format_string): |
| 71 | for field in format_string.split('|'): |
| 72 | if field[0] == '+': |
| 73 | # it's markup |
| 74 | field_name, _, format_spec = field[1:].partition(':') |
| 75 | yield '', field_name, format_spec, None |
| 76 | else: |
| 77 | yield field, None, None, None |
| 78 | |
| 79 | fmt = BarFormatter() |
| 80 | self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *') |
| 81 | |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 82 | # test all parameters used |
| 83 | class CheckAllUsedFormatter(string.Formatter): |
| 84 | def check_unused_args(self, used_args, args, kwargs): |
| 85 | # Track which arguments actuallly got used |
| 86 | unused_args = set(kwargs.keys()) |
| 87 | unused_args.update(range(0, len(args))) |
| 88 | |
| 89 | for arg in used_args: |
| 90 | unused_args.remove(arg) |
| 91 | |
| 92 | if unused_args: |
| 93 | raise ValueError("unused arguments") |
| 94 | |
| 95 | fmt = CheckAllUsedFormatter() |
| 96 | self.assertEqual(fmt.format("{0}", 10), "10") |
| 97 | self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100") |
| 98 | self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020") |
| 99 | self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0) |
| 100 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20) |
| 101 | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) |
| 102 | self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) |
| 103 | |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 104 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 105 | def test_maketrans(self): |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 106 | transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 108 | self.assertEqual(string.maketrans(b'abc', b'xyz'), transtable) |
| 109 | self.assertRaises(ValueError, string.maketrans, b'abc', b'xyzq') |
| 110 | self.assertRaises(TypeError, string.maketrans, 'abc', 'def') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 111 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 112 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 113 | support.run_unittest(ModuleTest) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 114 | |
| 115 | if __name__ == "__main__": |
| 116 | test_main() |