2015-05-18 03:20:13 +00:00
|
|
|
import re
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from boltons.formatutils import (get_format_args,
|
|
|
|
split_format_str,
|
|
|
|
tokenize_format_str,
|
2017-09-05 20:18:29 +00:00
|
|
|
infer_positional_format_args,
|
|
|
|
DeferredValue as DV)
|
2015-05-18 03:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
PFAT = namedtuple("PositionalFormatArgTest", "fstr arg_vals res")
|
|
|
|
|
|
|
|
_PFATS = [PFAT('{} {} {}', ('hi', 'hello', 'bye'), "hi hello bye"),
|
|
|
|
PFAT('{:d} {}', (1, 2), "1 2"),
|
|
|
|
PFAT('{!s} {!r}', ('str', 'repr'), "str 'repr'"),
|
|
|
|
PFAT('{[hi]}, {.__name__!r}', ({'hi': 'hi'}, re), "hi, 're'"),
|
|
|
|
PFAT('{{joek}} ({} {})', ('so', 'funny'), "{joek} (so funny)")]
|
|
|
|
|
|
|
|
|
|
|
|
def test_pos_infer():
|
|
|
|
for i, (tmpl, args, res) in enumerate(_PFATS):
|
|
|
|
converted = infer_positional_format_args(tmpl)
|
|
|
|
assert converted.format(*args) == res
|
|
|
|
|
|
|
|
|
|
|
|
_TEST_TMPLS = ["example 1: {hello}",
|
|
|
|
"example 2: {hello:*10}",
|
|
|
|
"example 3: {hello:*{width}}",
|
|
|
|
"example 4: {hello!r:{fchar}{width}}, {width}, yes",
|
2022-12-08 09:28:02 +00:00
|
|
|
"example 5: {0}, {1:d}, {2:f}, {1}",
|
|
|
|
"example 6: {}, {}, {}, {1}"]
|
2015-05-18 03:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_fstr_args():
|
|
|
|
results = []
|
|
|
|
for t in _TEST_TMPLS:
|
|
|
|
inferred_t = infer_positional_format_args(t)
|
|
|
|
res = get_format_args(inferred_t)
|
2022-12-08 09:28:02 +00:00
|
|
|
assert res
|
2015-05-18 03:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_split_fstr():
|
|
|
|
results = []
|
|
|
|
for t in _TEST_TMPLS:
|
|
|
|
res = split_format_str(t)
|
|
|
|
results.append(res)
|
2022-12-08 09:28:02 +00:00
|
|
|
assert res
|
2015-05-18 03:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tokenize_format_str():
|
|
|
|
results = []
|
|
|
|
for t in _TEST_TMPLS:
|
|
|
|
res = tokenize_format_str(t)
|
|
|
|
results.append(res)
|
2022-12-08 09:28:02 +00:00
|
|
|
assert res
|
2017-09-05 20:18:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_deferredvalue():
|
|
|
|
def myfunc():
|
|
|
|
myfunc.called += 1
|
|
|
|
return 123
|
|
|
|
|
|
|
|
myfunc.called = 0
|
|
|
|
|
|
|
|
dv = DV(myfunc)
|
|
|
|
assert str(dv) == '123'
|
|
|
|
assert myfunc.called == 1
|
|
|
|
assert str(dv) == '123'
|
|
|
|
assert myfunc.called == 1
|
|
|
|
dv.cache_value = False
|
|
|
|
assert str(dv) == '123'
|
|
|
|
assert myfunc.called == 2
|
|
|
|
assert str(dv) == '123'
|
|
|
|
assert myfunc.called == 3
|