|
| 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