Fix f-string tests

This commit is contained in:
David Strobach 2020-08-10 16:25:58 +02:00
parent dcedf4ac64
commit 12c8990e53
2 changed files with 42 additions and 14 deletions

View file

@ -17,6 +17,8 @@ from xonsh.built_ins import (
enter_macro,
path_literal,
_BuiltIns,
fstring_fields,
eval_fstring_field,
)
from xonsh.execer import Execer
from xonsh.jobs import tasks
@ -132,6 +134,8 @@ def xonsh_builtins(monkeypatch, xonsh_events):
builtins.__xonsh__.list_of_list_of_strs_outer_product = (
list_of_list_of_strs_outer_product
)
builtins.__xonsh__.fstring_fields = fstring_fields
builtins.__xonsh__.eval_fstring_field = eval_fstring_field
builtins.__xonsh__.history = DummyHistory()
builtins.__xonsh__.subproc_captured_stdout = sp
builtins.__xonsh__.subproc_captured_inject = sp
@ -160,5 +164,5 @@ def xonsh_builtins(monkeypatch, xonsh_events):
def pytest_configure(config):
"""Abort test run if --flake8 requested, since it would hang on parser_test.py"""
if config.getoption('--flake8', ''):
if config.getoption("--flake8", ""):
pytest.exit("pytest-flake8 no longer supported, use flake8 instead.")

View file

@ -14,6 +14,7 @@ from xonsh.parsers.base import eval_fstr_fields
from tools import nodes_equal, skip_if_no_walrus
@pytest.fixture(autouse=True)
def xonsh_builtins_autouse(xonsh_builtins):
return xonsh_builtins
@ -124,27 +125,50 @@ def test_f_env_var():
@pytest.mark.parametrize(
"inp, exp",
"inp, exp, exp_fields",
[
('f"{}"', 'f"{}"'),
('f"$HOME"', 'f"$HOME"'),
('f"{0} - {1}"', 'f"{0} - {1}"'),
('f"$HOME"', "$HOME", 0),
('f"{0} - {1}"', "0 - 1", 0),
('f"{$HOME}"', "/foo/bar", 1),
('f"{ $HOME }"', "/foo/bar", 1),
("f\"{'$HOME'}\"", "$HOME", 0),
("f\"{${'HOME'}}\"", "/foo/bar", 1),
("f'{${$FOO+$BAR}}'", "/foo/bar", 1),
("f\"${$FOO}{$BAR}={f'{$HOME}'}\"", "$HOME=/foo/bar", 3),
(
'f"{$HOME}"',
"f\"{__xonsh__.execer.eval(r'$HOME', glbs=globals(), locs=locals())}\"",
'''f"""foo
{f"_{$HOME}_"}
bar"""''',
"foo\n_/foo/bar_\nbar",
1,
),
(
'f"{ $HOME }"',
"f\"{__xonsh__.execer.eval(r'$HOME ', glbs=globals(), locs=locals())}\"",
'''f"""foo
{f"_{${'HOME'}}_"}
bar"""''',
"foo\n_/foo/bar_\nbar",
1,
),
(
"f\"{'$HOME'}\"",
"f\"{__xonsh__.execer.eval(r'\\'$HOME\\'', glbs=globals(), locs=locals())}\"",
'''f"""foo
{f"_{${ $FOO + $BAR }}_"}
bar"""''',
"foo\n_/foo/bar_\nbar",
1,
),
],
)
def test_eval_fstr_fields(inp, exp):
obs = eval_fstr_fields(inp, 'f"')
def test_eval_fstr_fields(inp, exp, exp_fields):
builtins.__xonsh__.fstring_fields.clear()
joined_str_node = eval_fstr_fields(inp, "f").body[0].value
assert isinstance(joined_str_node, ast.JoinedStr)
node = ast.Expression(body=joined_str_node)
code = compile(node, "<test_eval_fstr_fields>", mode="eval")
fields = len(builtins.__xonsh__.fstring_fields)
assert exp_fields == fields
builtins.__xonsh__.env = {"HOME": "/foo/bar", "FOO": "HO", "BAR": "ME"}
obs = eval(code)
assert len(builtins.__xonsh__.fstring_fields) == 0
assert exp == obs
@ -308,7 +332,7 @@ def test_in():
def test_is():
check_ast("int is float") # avoid PY3.8 SyntaxWarning "is" with a literal
check_ast("int is float") # avoid PY3.8 SyntaxWarning "is" with a literal
def test_not_in():