| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import ast |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis.vendor import pretty |
| 16 |
|
| 17 |
|
| 18 |
def _try_inline_lambda(func_name, args=(), kwargs=None): |
| 19 |
"""Helper: run _try_inline_lambda and return the printer output, or None.""" |
| 20 |
if kwargs is None: |
| 21 |
kwargs = {} |
| 22 |
p = pretty.RepresentationPrinter() |
| 23 |
if not pretty._try_inline_lambda(func_name, args, kwargs, p): |
| 24 |
return None |
| 25 |
return p.getvalue() |
| 26 |
|
| 27 |
|
| 28 |
class BadRepr: |
| 29 |
def __repr__(self): |
| 30 |
return "not valid(python syntax" |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
"func_name, args, kwargs, expected", |
| 35 |
[ |
| 36 |
pytest.param("lambda: 42", (), {}, "42", id="no_args"), |
| 37 |
pytest.param("lambda x: x + 1", (0,), {}, "0 + 1", id="single_positional"), |
| 38 |
pytest.param("lambda x: x + 1", (), {"x": 0}, "0 + 1", id="single_kwarg"), |
| 39 |
pytest.param( |
| 40 |
"lambda a, b: (b, a)", |
| 41 |
("hello", "world"), |
| 42 |
{}, |
| 43 |
"('world', 'hello')", |
| 44 |
id="multi_positional", |
| 45 |
), |
| 46 |
pytest.param( |
| 47 |
"lambda a, b: a + b", (), {"a": 1, "b": 2}, "1 + 2", id="multi_kwargs" |
| 48 |
), |
| 49 |
pytest.param( |
| 50 |
"lambda a, b: a + b", |
| 51 |
(1,), |
| 52 |
{"b": 2}, |
| 53 |
"1 + 2", |
| 54 |
id="mixed_positional_and_kwargs", |
| 55 |
), |
| 56 |
pytest.param("lambda x: 42", (99,), {}, "42", id="unused_param"), |
| 57 |
pytest.param( |
| 58 |
"lambda a, b, c: 42", (1, 2, 3), {}, "42", id="unused_params_multi" |
| 59 |
), |
| 60 |
pytest.param( |
| 61 |
"lambda s: s.upper()", ("hi",), {}, "'hi'.upper()", id="method_call" |
| 62 |
), |
| 63 |
pytest.param("lambda b: f(b).g()", (0,), {}, "f(0).g()", id="nested_call"), |
| 64 |
pytest.param( |
| 65 |
"lambda a, b=10: a + 1", |
| 66 |
(), |
| 67 |
{"a": 5}, |
| 68 |
"5 + 1", |
| 69 |
id="default_not_passed", |
| 70 |
), |
| 71 |
pytest.param( |
| 72 |
"lambda a, b=10: a + b", |
| 73 |
(), |
| 74 |
{"a": 5, "b": 20}, |
| 75 |
"5 + 20", |
| 76 |
id="default_passed", |
| 77 |
), |
| 78 |
pytest.param("lambda a, b=1, c=2: a", (99,), {}, "99", id="multiple_defaults"), |
| 79 |
], |
| 80 |
) |
| 81 |
def test_inline_success(func_name, args, kwargs, expected): |
| 82 |
assert _try_inline_lambda(func_name, args, kwargs) == expected |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
"func_name, args, kwargs", |
| 87 |
[ |
| 88 |
pytest.param("not a lambda at all!!!", (), {}, id="syntax_error"), |
| 89 |
pytest.param("foo", (), {}, id="not_a_lambda"), |
| 90 |
pytest.param("lambda *args: args", ((1, 2),), {}, id="vararg"), |
| 91 |
pytest.param("lambda **kw: kw", (), {"a": 1}, id="kwarg_star"), |
| 92 |
pytest.param("lambda *, x: x", (), {"x": 1}, id="kwonly_args"), |
| 93 |
pytest.param("lambda x: (x, x)", (1,), {}, id="param_used_twice"), |
| 94 |
pytest.param("lambda x: x + x", (1,), {}, id="param_used_twice_different"), |
| 95 |
pytest.param("lambda a, b: a + b", (1,), {}, id="too_few_args_no_default"), |
| 96 |
pytest.param("lambda x: x", (), {"y": 1}, id="wrong_kwarg_name"), |
| 97 |
pytest.param("lambda: 42", (1, 2), {}, id="more_args_than_params"), |
| 98 |
pytest.param("lambda x: x", (BadRepr(),), {}, id="invalid_repr"), |
| 99 |
], |
| 100 |
) |
| 101 |
def test_inline_bail_out(func_name, args, kwargs): |
| 102 |
assert _try_inline_lambda(func_name, args, kwargs) is None |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.parametrize( |
| 106 |
"func_name, args, kwargs, expected", |
| 107 |
[ |
| 108 |
pytest.param("lambda: 0", (), {}, "0", id="no_arg_lambda"), |
| 109 |
pytest.param("lambda x: x", (1,), {}, "1", id="identity"), |
| 110 |
pytest.param("lambda a, b: a - b", (10, 3), {}, "10 - 3", id="subtraction"), |
| 111 |
pytest.param( |
| 112 |
"lambda x: (x, x)", (1,), {}, "(lambda x: (x, x))(1)", id="multi_use" |
| 113 |
), |
| 114 |
pytest.param("lambda x: [x]", (1,), {}, "[1]", id="list_wrap"), |
| 115 |
pytest.param( |
| 116 |
"lambda s: s.strip()", (" hi ",), {}, "' hi '.strip()", id="strip" |
| 117 |
), |
| 118 |
pytest.param("lambda: None", (), {}, "None", id="none"), |
| 119 |
pytest.param("lambda x: f(x)", (42,), {}, "f(42)", id="inlines_single_use"), |
| 120 |
pytest.param("lambda: 42", (), {}, "42", id="inlines_no_arg"), |
| 121 |
pytest.param( |
| 122 |
"lambda *args: args", |
| 123 |
((1, 2),), |
| 124 |
{}, |
| 125 |
"(lambda *args: args)((1, 2))", |
| 126 |
id="wraps_vararg", |
| 127 |
), |
| 128 |
pytest.param("foo", (1, 2), {}, "foo(1, 2)", id="non_lambda_unchanged"), |
| 129 |
], |
| 130 |
) |
| 131 |
def test_repr_call_parametrized(func_name, args, kwargs, expected): |
| 132 |
p = pretty.RepresentationPrinter() |
| 133 |
p.repr_call(func_name, args, kwargs) |
| 134 |
assert p.getvalue() == expected |
| 135 |
|
| 136 |
|
| 137 |
def test_unparse_failure_returns_none(monkeypatch): |
| 138 |
real_unparse = ast.unparse |
| 139 |
called = False |
| 140 |
|
| 141 |
def bad_unparse(node): |
| 142 |
nonlocal called |
| 143 |
if not called: |
| 144 |
called = True |
| 145 |
raise ValueError("boom") |
| 146 |
return real_unparse(node) |
| 147 |
|
| 148 |
monkeypatch.setattr(ast, "unparse", bad_unparse) |
| 149 |
assert _try_inline_lambda("lambda x: x", args=(1,)) is None |
| 150 |
|
| 151 |
|
| 152 |
def test_repr_call_skips_inlining_when_comments_present(): |
| 153 |
p = pretty.RepresentationPrinter() |
| 154 |
# Simulate explain-mode comments on slice (0, 5) |
| 155 |
p.slice_comments[(0, 5)] = "or any other generated value" |
| 156 |
p.repr_call( |
| 157 |
"lambda x: f(x)", |
| 158 |
args=(42,), |
| 159 |
kwargs={}, |
| 160 |
arg_slices={"arg[0]": (0, 5)}, |
| 161 |
) |
| 162 |
result = p.getvalue() |
| 163 |
# Should NOT inline — must keep call style for the comment |
| 164 |
assert result.startswith("(lambda x: f(x))") |
| 165 |
assert "# or any other generated value" in result |
| 166 |
|
| 167 |
|
| 168 |
def test_repr_call_inlines_when_arg_slices_but_no_comments(): |
| 169 |
p = pretty.RepresentationPrinter() |
| 170 |
# arg_slices present but no matching slice_comments |
| 171 |
p.repr_call( |
| 172 |
"lambda x: f(x)", |
| 173 |
args=(42,), |
| 174 |
kwargs={}, |
| 175 |
arg_slices={"arg[0]": (0, 5)}, |
| 176 |
) |
| 177 |
assert p.getvalue() == "f(42)" |