def test_eval_1_is_fast(): """``eval `` should be very quick.""" # This should ideally be below 0.1s. with helpers.Timer(max=0.6): output = helpers.run(["eval", "1"]).decode() assert output == "1\n"
def test_meta_pip_command(): """``meta pip`` command accepts pip subcommands and options.""" result = helpers.run(["meta", "pip", "install", "--help"]).decode() expected = ( "pip install [options] <requirement specifier> [package-index-options] ..." ) assert expected in result
def test_render_short_help(tmp_path, tmp_env): """CLI renders ``short_help`` if ``help`` is missing.""" result = helpers.run(["map", "--help"]).decode() text = """ [[command]] name = "my-command" short_help = "This is the short help." [[command.stages]] command = "eval" params = {code='1'} """ (tmp_path / "config.toml").write_text(text) result = helpers.run(["my-command", "--help"], env=tmp_env).decode() assert "This is the short help." in result
def test_exec_before(): exec_before = textwrap.dedent("""\ import csv def func(line): return next(csv.reader([line])) """) assert (helpers.run(["--exec-before", exec_before, "map", "func"], input=b"a,b\n").decode() == "['a', 'b']\n")
def test_config_file(tmp_path): config_body = """ exec_before = "from collections import Counter as C" """ config_file_path = tmp_path / "config.toml" config_file_path.write_text(config_body) args = ["apply", "C(x)"] stdin = "1\n2\n".encode() env = dict(os.environ) env.update( {f"{utils.NAME}_CONFIG_DIR".upper().encode(): str(tmp_path).encode()}) output = helpers.run(args, input=stdin, env=env).decode() assert output.startswith("Counter")
def test_autocall(): output = helpers.run(["map", "len"], input=b"a\nbb\n").decode() assert output == "1\n2\n"
def test_no_autocall(): output = helpers.run(["map", "--no-autocall", "1"], input=b"a\nbb\n").decode() assert output == "1\n1\n"
from tests import errors from tests import helpers as h from tests import paths class Case: def __init__(self, name: str, expression: str): self.name = name self.expression = expression def __str__(self): return f'{self.name}:{self.expression}' def cases_integers(): yield Case('integer-integer', '0 1') @h.cases(cases_integers()) def test(_: str, case: Case): argv = [paths.ACALC] pr = h.run(argv, input=case.expression.encode(), stdout=h.PIPE) assert pr.returncode == errors.INVALID_SYNTAX
def test_help(): """CLI help is formatted as plain text not rst and includes a demo.""" result = helpers.run(["map", "--help"]).decode() assert "$ mario map" in result assert ".. code-block" not in result
def test_eval_cli(): assert helpers.run(["eval", "1+1"]).decode() == "2\n"
def test_stage_exec_before(): assert helpers.run(["eval", "--exec-before", "a=1", "a"]).decode() == "1\n"
def test_cli_version(runner): args = ["--version"] result = helpers.run(args).decode() assert result == f"mario, version {mario.__version__}\n"
def test_chain(): expected = "[1, 2]\n" result = helpers.run(["eval", "[[1, 2]]", "chain"]).decode() assert result == expected, (result, expected)
def test_autocall_in_eval(): helpers.run(["eval", "datetime.datetime.now().isoformat()"]).decode()
import typing from tests import helpers as h from tests import paths class Case: def __init__(self, name: str, argv: typing.Iterable): self.name = name self.argv = tuple(argv) def cases(): yield Case('too much arguments', ['']) @h.cases(cases()) def test(_, case: Case): argv = [paths.ACALC] argv.extend(case.argv) pr = h.run(argv, stdout=h.PIPE) assert pr.returncode == 2
def test_autocall_requires_symbol(): output = helpers.run(["map", "pathlib.Path(x).name"], input=b"a\nbb\n").decode() assert output == "a\nbb\n"
def test_chain(): expected = "1\n1\n1\n1\n" result = helpers.run(["stack", "x", "chain", "map", "len"], input=b"abc\n").decode() assert result == expected, (result, expected)