[formatter] allow multiple "special" format specifiers (#595)
It is now, for example, possible to specify multiple replacement
operations per format replacement field: {name:Ra/b/Rc/d/}
This commit is contained in:
@@ -267,7 +267,7 @@ class TestFormatter(util.Formatter):
|
||||
return ""
|
||||
|
||||
def _apply_simple(self, key, fmt):
|
||||
if key == "extension" or "._format_optional." in repr(fmt):
|
||||
if key == "extension" or "._parse_optional." in repr(fmt):
|
||||
return self._noop
|
||||
|
||||
def wrap(obj):
|
||||
@@ -275,7 +275,7 @@ class TestFormatter(util.Formatter):
|
||||
return wrap
|
||||
|
||||
def _apply(self, key, funcs, fmt):
|
||||
if key == "extension" or "._format_optional." in repr(fmt):
|
||||
if key == "extension" or "._parse_optional." in repr(fmt):
|
||||
return self._noop
|
||||
|
||||
def wrap(obj):
|
||||
|
||||
@@ -390,6 +390,18 @@ class TestFormatter(unittest.TestCase):
|
||||
self._run_test("{a!l:Rl//}" , "heo word")
|
||||
self._run_test("{name:Rame/othing/}", "Nothing")
|
||||
|
||||
def test_chain_special(self):
|
||||
# multiple replacements
|
||||
self._run_test("{a:Rh/C/RE/e/RL/l/}", "Cello wOrld")
|
||||
self._run_test("{d[b]!s:R1/Q/R2/A/R0/Y/}", "Y")
|
||||
|
||||
# join-and-replace
|
||||
self._run_test("{l:J-/Rb/E/}", "a-E-c")
|
||||
|
||||
# optional-and-maxlen
|
||||
self._run_test("{d[a]:?</>/L1/too long/}", "<too long>")
|
||||
self._run_test("{d[c]:?</>/L5/too long/}", "")
|
||||
|
||||
def _run_test(self, format_string, result, default=None):
|
||||
formatter = util.Formatter(format_string, default)
|
||||
output = formatter.format_map(self.kwdict)
|
||||
|
||||
Reference in New Issue
Block a user