Skip to content

Commit

Permalink
add support for testing lambdas with types=
Browse files Browse the repository at this point in the history
  • Loading branch information
rudymatela committed Nov 12, 2024
1 parent 1df60da commit 16aa276
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ TODO for leancheck.py
later
-----

* `check`: add `types=` mechanism in order to support lambdas

* handle exceptions nicely somehow (`prop_minimum`) for sort

* simplify code
Expand Down
24 changes: 17 additions & 7 deletions src/leancheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import typing


def check(prop, max_tests=360, verbose=True, silent=False):
def check(prop, max_tests=360, verbose=True, silent=False, types=[]):
"""
Checks a property for several enumerated argument values.
Properties must have type hints
Expand Down Expand Up @@ -114,15 +114,25 @@ def check(prop, max_tests=360, verbose=True, silent=False):
>>> check(prop_sorted_twice, silent=True)
True
Lambdas do not allow type annotations,
for them one can use the `types=` option
to list argument types.
>>> check(lambda xs: sorted(sorted(xs)) == sorted(xs), types=[list[int]])
+++ OK, passed 360 tests: <lambda>
True
"""
verbose = verbose and not silent
clear, red, green, blue, yellow = _colour_escapes()
sig = inspect.signature(prop)
ret = sig.return_annotation
# print(f"Property's signature: {sig}")
if ret != bool and not silent:
print(f"{yellow}Warning{clear}: property's return value is {ret} and not {bool}")
es = [Enumerator[par.annotation] for par in sig.parameters.values()]
if not types:
sig = inspect.signature(prop)
# print(f"Property's signature: {sig}")
ret = sig.return_annotation
if ret != bool and not silent:
print(f"{yellow}Warning{clear}: property's return value is {ret} and not {bool}")
types = [par.annotation for par in sig.parameters.values()]
es = [Enumerator[t] for t in types]
for i, args in enumerate(itertools.islice(Enumerator.product(*es), max_tests)):
if not prop(*args):
if not silent:
Expand Down

0 comments on commit 16aa276

Please sign in to comment.