diff options
author | Eric Smith <eric@trueblade.com> | 2010-11-04 17:06:58 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2010-11-04 17:06:58 (GMT) |
commit | 27bbca6f79690472c7beff8020ff331b14450b77 (patch) | |
tree | 3b6ea39069caf2f197656092c45e9cba0b384b85 /Lib/test/test_unicode.py | |
parent | 2397dd58b70986db898f689fe7a1597cdd51f29f (diff) | |
download | cpython-27bbca6f79690472c7beff8020ff331b14450b77.zip cpython-27bbca6f79690472c7beff8020ff331b14450b77.tar.gz cpython-27bbca6f79690472c7beff8020ff331b14450b77.tar.bz2 |
Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
Diffstat (limited to 'Lib/test/test_unicode.py')
-rw-r--r-- | Lib/test/test_unicode.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 7927834..cc891bd 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -695,6 +695,84 @@ class UnicodeTest(string_tests.CommonTest, self.assertRaises(ValueError, format, '', '#') self.assertRaises(ValueError, format, '', '#20') + def test_format_map(self): + self.assertEqual(''.format_map({}), '') + self.assertEqual('a'.format_map({}), 'a') + self.assertEqual('ab'.format_map({}), 'ab') + self.assertEqual('a{{'.format_map({}), 'a{') + self.assertEqual('a}}'.format_map({}), 'a}') + self.assertEqual('{{b'.format_map({}), '{b') + self.assertEqual('}}b'.format_map({}), '}b') + self.assertEqual('a{{b'.format_map({}), 'a{b') + + # using mappings + class Mapping(dict): + def __missing__(self, key): + return key + self.assertEqual('{hello}'.format_map(Mapping()), 'hello') + self.assertEqual('{a} {world}'.format_map(Mapping(a='hello')), 'hello world') + + class InternalMapping: + def __init__(self): + self.mapping = {'a': 'hello'} + def __getitem__(self, key): + return self.mapping[key] + self.assertEqual('{a}'.format_map(InternalMapping()), 'hello') + + + # classes we'll use for testing + class C: + def __init__(self, x=100): + self._x = x + def __format__(self, spec): + return spec + + class D: + def __init__(self, x): + self.x = x + def __format__(self, spec): + return str(self.x) + + # class with __str__, but no __format__ + class E: + def __init__(self, x): + self.x = x + def __str__(self): + return 'E(' + self.x + ')' + + # class with __repr__, but no __format__ or __str__ + class F: + def __init__(self, x): + self.x = x + def __repr__(self): + return 'F(' + self.x + ')' + + # class with __format__ that forwards to string, for some format_spec's + class G: + def __init__(self, x): + self.x = x + def __str__(self): + return "string is " + self.x + def __format__(self, format_spec): + if format_spec == 'd': + return 'G(' + self.x + ')' + return object.__format__(self, format_spec) + + # class that returns a bad type from __format__ + class H: + def __format__(self, format_spec): + return 1.0 + + self.assertEqual('{foo._x}'.format_map({'foo': C(20)}), '20') + + # test various errors + self.assertRaises(TypeError, '{'.format_map) + self.assertRaises(TypeError, '}'.format_map) + self.assertRaises(TypeError, 'a{'.format_map) + self.assertRaises(TypeError, 'a}'.format_map) + self.assertRaises(TypeError, '{a'.format_map) + self.assertRaises(TypeError, '}a'.format_map) + def test_format_auto_numbering(self): class C: def __init__(self, x=100): |