Skip to content

Commit ca5ed59

Browse files
committed
Update property based tests
Add tests for constant folding
1 parent 8f002ef commit ca5ed59

File tree

5 files changed

+121
-58
lines changed

5 files changed

+121
-58
lines changed

hypo_test/expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ def arguments(draw, for_lambda=False) -> ast.arguments:
240240
allow_annotation = False if for_lambda else True
241241

242242
args = draw(lists(arg(allow_annotation), max_size=2))
243+
posonlyargs = draw(lists(arg(allow_annotation), max_size=2))
243244
kwonlyargs = draw(lists(arg(allow_annotation), max_size=2))
244245
vararg = draw(none() | arg(allow_annotation))
245246
kwarg = draw(none() | arg(allow_annotation))
246247
defaults=[]
247248
kw_defaults=draw(lists(none() | expression(), max_size=len(kwonlyargs), min_size=len(kwonlyargs)))
248249
return ast.arguments(
250+
posonlyargs=posonlyargs,
249251
args=args,
250252
vararg=vararg,
251253
kwonlyargs=kwonlyargs,

hypo_test/folding.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from hypothesis import assume
2+
from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries, booleans, SearchStrategy, text, composite, one_of, floats, complex_numbers, characters, none
3+
import ast
4+
5+
from expressions import NameConstant, Num
6+
7+
leaves = NameConstant() | Num()
8+
9+
@composite
10+
def BinOp(draw, expression) -> ast.BinOp:
11+
op = draw(sampled_from([
12+
ast.Add(),
13+
ast.Sub(),
14+
ast.Mult(),
15+
ast.Div(),
16+
ast.FloorDiv(),
17+
ast.Mod(),
18+
ast.Pow(),
19+
ast.LShift(),
20+
ast.RShift(),
21+
ast.BitOr(),
22+
ast.BitXor(),
23+
ast.BitAnd(),
24+
ast.MatMult()
25+
]))
26+
27+
le = draw(lists(expression, min_size=2, max_size=2))
28+
29+
return ast.BinOp(le[0], op, le[1])
30+
31+
32+
def expression() -> SearchStrategy:
33+
return recursive(
34+
leaves,
35+
lambda expression:
36+
BinOp(expression),
37+
max_leaves=150
38+
)
39+
40+
@composite
41+
def FoldableExpression(draw) -> ast.Expression:
42+
""" An eval expression """
43+
e = draw(expression())
44+
return ast.Expression(e)

hypo_test/module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,4 @@ def suite() -> SearchStrategy:
269269
@composite
270270
def Module(draw) -> ast.Module:
271271
b = draw(lists(suite(), min_size=1, max_size=10))
272-
return ast.Module(body=b)
272+
return ast.Module(body=b, type_ignores=[])

hypo_test/test.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

hypo_test/test_it.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import ast
2+
3+
from datetime import timedelta
4+
from hypothesis import given, settings, Verbosity, note, HealthCheck
5+
6+
from folding import FoldableExpression
7+
from patterns import Pattern
8+
from python_minifier import ModulePrinter
9+
from python_minifier.ast_compare import compare_ast
10+
from python_minifier.ast_printer import print_ast
11+
from python_minifier.expression_printer import ExpressionPrinter
12+
from expressions import Expression
13+
from module import Module
14+
from python_minifier.rename.mapper import add_parent
15+
from python_minifier.transforms.constant_folding import FoldConstants
16+
17+
18+
@given(node=Expression())
19+
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
20+
def test_expression(node):
21+
assert isinstance(node, ast.AST)
22+
23+
note(ast.dump(node))
24+
printer = ExpressionPrinter()
25+
code = printer(node)
26+
note(code)
27+
compare_ast(node, ast.parse(code, 'test_expression', 'eval'))
28+
29+
30+
@given(node=Module())
31+
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
32+
def test_module(node):
33+
assert isinstance(node, ast.Module)
34+
35+
note(ast.dump(node))
36+
printer = ModulePrinter()
37+
code = printer(node)
38+
note(code)
39+
compare_ast(node, ast.parse(code, 'test_module'))
40+
41+
42+
@given(node=Pattern())
43+
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=100, verbosity=Verbosity.verbose)
44+
def test_pattern(node):
45+
46+
module = ast.Module(
47+
body=[ast.Match(subject=ast.Constant(value=None),
48+
cases=[
49+
ast.match_case(
50+
pattern=node,
51+
guard=None,
52+
body=[ast.Pass()]
53+
)
54+
])],
55+
type_ignores=[]
56+
)
57+
58+
printer = ModulePrinter()
59+
code = printer(module)
60+
note(code)
61+
compare_ast(module, ast.parse(code, 'test_pattern'))
62+
63+
@given(node=FoldableExpression())
64+
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100000, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
65+
def test_folding(node):
66+
assert isinstance(node, ast.AST)
67+
note(print_ast(node))
68+
69+
add_parent(node)
70+
71+
constant_folder = FoldConstants()
72+
73+
# The constant folder asserts the value is correct
74+
constant_folder(node)

0 commit comments

Comments
 (0)