Skip to content

Commit 94efea7

Browse files
committed
Add unit tests
covering new changes: - bool folding - SuiteTransformer descending into more blocks - RemoveDebug changes - RemoveDeadBranches
1 parent 45a4880 commit 94efea7

5 files changed

Lines changed: 900 additions & 5 deletions

File tree

‎test/test_folding.py‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,111 @@ def test_mixed_numeric_bool_folded(source, expected):
558558
pytest.skip('NameConstant not in python < 3.4')
559559

560560
run_test(source, expected)
561+
562+
563+
@pytest.mark.parametrize(
564+
('source', 'expected'), [
565+
('1 or 2', '1'),
566+
('0 or 2', '2'),
567+
('1 and 2', '2'),
568+
('0 and 2', '0'),
569+
]
570+
)
571+
def test_boolop_numeric_folded(source, expected):
572+
"""
573+
Test that and/or expressions with numeric operands are folded.
574+
575+
BoolOp returns an operand value, not necessarily a bool.
576+
"""
577+
run_test(source, expected)
578+
579+
580+
@pytest.mark.parametrize(
581+
('source', 'expected'), [
582+
('True and False', 'False'),
583+
('False or True', 'True'),
584+
('None or False', 'False'),
585+
('True and True and True', 'True'),
586+
]
587+
)
588+
def test_boolop_folded(source, expected):
589+
"""
590+
Test that and/or expressions with constant operands are folded.
591+
"""
592+
if sys.version_info < (3, 4):
593+
pytest.skip('NameConstant not in python < 3.4')
594+
595+
run_test(source, expected)
596+
597+
598+
@pytest.mark.parametrize(
599+
('source', 'expected'), [
600+
# Partially constant expressions must not fold, even where
601+
# short circuit evaluation makes the result predictable
602+
('True and x', 'True and x'),
603+
('True or x', 'True or x'),
604+
('x and False', 'x and False'),
605+
606+
# fold() doesn't construct None constants, only bool and numbers
607+
('False or None', 'False or None'),
608+
]
609+
)
610+
def test_boolop_not_folded(source, expected):
611+
"""
612+
Test that and/or expressions with non-constant operands are not folded.
613+
"""
614+
if sys.version_info < (3, 4):
615+
pytest.skip('NameConstant not in python < 3.4')
616+
617+
run_test(source, expected)
618+
619+
620+
@pytest.mark.parametrize(
621+
('source', 'expected'), [
622+
('True is True', 'True'),
623+
('True is not False', 'True'),
624+
('None is None', 'True'),
625+
('True == True', 'True'),
626+
('1 < 2 < 3', 'True'),
627+
('3 < 2 < 1 < 0', 'False'),
628+
('1 == 1.0', 'True'),
629+
]
630+
)
631+
def test_compare_folded(source, expected):
632+
"""
633+
Test that comparisons of constants are folded.
634+
"""
635+
if sys.version_info < (3, 4):
636+
pytest.skip('NameConstant not in python < 3.4')
637+
638+
run_test(source, expected)
639+
640+
641+
@pytest.mark.parametrize(
642+
('source', 'expected'), [
643+
# Not smaller than the folded result
644+
('1 < 2', '1 < 2'),
645+
('1 == 1', '1 == 1'),
646+
647+
# Identity of non-singletons is implementation defined
648+
('1000 is 1000', '1000 is 1000'),
649+
('1000 is not 1000', '1000 is not 1000'),
650+
('1 < 2 is 2', '1 < 2 is 2'),
651+
652+
# Raises at runtime, must be preserved
653+
('1 in 2', '1 in 2'),
654+
('1 < None', '1 < None'),
655+
656+
# Not constant
657+
('x < 2', 'x < 2'),
658+
('True is x', 'True is x'),
659+
]
660+
)
661+
def test_compare_not_folded(source, expected):
662+
"""
663+
Test comparisons that must not be folded.
664+
"""
665+
if sys.version_info < (3, 4):
666+
pytest.skip('NameConstant not in python < 3.4')
667+
668+
run_test(source, expected)

‎test/test_remove_assert.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import ast
2+
import sys
3+
4+
import pytest
25

36
from python_minifier.ast_annotation import add_parent
47
from python_minifier.ast_compare import compare_ast
@@ -116,3 +119,56 @@ def b(): 0'''
116119
expected_ast = ast.parse(expected)
117120
actual_ast = remove_asserts(source)
118121
compare_ast(expected_ast, actual_ast)
122+
123+
124+
def test_remove_from_except_handler():
125+
source = '''try:
126+
a()
127+
except:
128+
assert x
129+
'''
130+
expected = '''try:
131+
a()
132+
except:
133+
0
134+
'''
135+
136+
expected_ast = ast.parse(expected)
137+
actual_ast = remove_asserts(source)
138+
compare_ast(expected_ast, actual_ast)
139+
140+
141+
def test_remove_from_finally():
142+
source = '''try:
143+
a()
144+
finally:
145+
assert x
146+
b()
147+
'''
148+
expected = '''try:
149+
a()
150+
finally:
151+
b()
152+
'''
153+
154+
expected_ast = ast.parse(expected)
155+
actual_ast = remove_asserts(source)
156+
compare_ast(expected_ast, actual_ast)
157+
158+
159+
def test_remove_from_match_case():
160+
if sys.version_info < (3, 10):
161+
pytest.skip('Match statement not in python < 3.10')
162+
163+
source = '''match x:
164+
case 1:
165+
assert y
166+
'''
167+
expected = '''match x:
168+
case 1:
169+
0
170+
'''
171+
172+
expected_ast = ast.parse(expected)
173+
actual_ast = remove_asserts(source)
174+
compare_ast(expected_ast, actual_ast)

0 commit comments

Comments
 (0)