Skip to content

Commit dde9d4d

Browse files
committed
Add folding unit tests
1 parent 1ecf7e6 commit dde9d4d

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

test/test_folding.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import ast
2+
import pytest
3+
4+
from python_minifier import add_namespace
5+
from python_minifier.transforms.constant_folding import FoldConstants
6+
from python_minifier.ast_compare import compare_ast
7+
8+
9+
def fold_constants(source):
10+
module = ast.parse(source)
11+
add_namespace(module)
12+
FoldConstants()(module)
13+
return module
14+
15+
16+
@pytest.mark.parametrize('source,expected', [
17+
('True | True', 'True'),
18+
('True | False', 'True'),
19+
('False | True', 'True'),
20+
('False | False', 'False'),
21+
('True & True', 'True'),
22+
('True & False', 'False'),
23+
('False & True', 'False'),
24+
('False & False', 'False'),
25+
('True ^ True', 'False'),
26+
('True ^ False', 'True'),
27+
('False ^ True', 'True'),
28+
('False ^ False', 'False'),
29+
('(True | True) | True', 'True'),
30+
('(True | True) | False', 'True'),
31+
('(True | False) | True', 'True'),
32+
('(True | False) | False', 'True'),
33+
('(False | True) | True', 'True'),
34+
('(False | True) | False', 'True'),
35+
('(False | False) | True', 'True'),
36+
('(False | False) | False', 'False'),
37+
('(True | True) & True', 'True'),
38+
('(True | True) & False', 'False'),
39+
('(True | False) & True', 'True'),
40+
('(True | False) & False', 'False'),
41+
('(False | True) & True', 'True'),
42+
('(False | True) & False', 'False'),
43+
('(False | False) & True', 'False'),
44+
('(False | False) & False', 'False'),
45+
('(True | True) ^ True', 'False'),
46+
('(True | True) ^ False', 'True'),
47+
('(True | False) ^ True', 'False'),
48+
('(True | False) ^ False', 'True'),
49+
('(False | True) ^ True', 'False'),
50+
('(False | True) ^ False', 'True'),
51+
('(False | False) ^ True', 'True'),
52+
('(False | False) ^ False', 'False'),
53+
('True | (True | True)', 'True'),
54+
('True | (True | False)', 'True'),
55+
('True | (False | True)', 'True'),
56+
('True | (False | False)', 'True'),
57+
('False | (True | True)', 'True'),
58+
('False | (True | False)', 'True'),
59+
('False | (False | True)', 'True'),
60+
('False | (False | False)', 'False'),
61+
])
62+
def test_bool(source, expected):
63+
"""
64+
Test BinOp with bool operands
65+
66+
This is mainly testing we fold the constants correctly
67+
"""
68+
expected_ast = ast.parse(expected)
69+
actual_ast = fold_constants(
70+
source
71+
)
72+
compare_ast(expected_ast, actual_ast)
73+
74+
@pytest.mark.parametrize('source,expected', [
75+
('10 + 10', '20'),
76+
('10 + 0', '10'),
77+
('0 + 10', '10'),
78+
('10 + 10 + 5', '25'),
79+
('10 - 5 + 5', '10'),
80+
('10 * 10', '100'),
81+
('10 * 10 * 10', '1000'),
82+
('(10 * 10) // 10', '10'),
83+
('(2 * 10) // (2+2)', '5'),
84+
('8>>2', '2'),
85+
('8<<2', '32'),
86+
('0xff^0x0f', '0xf0'),
87+
('0xf0&0xff', '0xf0'),
88+
('0xf0|0x0f', '0xff'),
89+
('10%2', '0'),
90+
('10%3', '1'),
91+
])
92+
def test_int(source, expected):
93+
"""
94+
Test BinOp with integer operands we can fold
95+
"""
96+
expected_ast = ast.parse(expected)
97+
actual_ast = fold_constants(
98+
source
99+
)
100+
compare_ast(expected_ast, actual_ast)
101+
102+
@pytest.mark.parametrize('source,expected', [
103+
('10/10', '10/10'),
104+
('5+5/10', '5+5/10'),
105+
('2*5/10', '10/10'),
106+
('2/5*10', '2/5*10'),
107+
('2**5', '2**5'),
108+
('5@6', '5@6'),
109+
])
110+
def test_int_not_eval(source, expected):
111+
"""
112+
Test BinOp with operations we don't want to fold
113+
"""
114+
expected_ast = ast.parse(expected)
115+
actual_ast = fold_constants(
116+
source
117+
)
118+
compare_ast(expected_ast, actual_ast)
119+
120+
@pytest.mark.parametrize('source,expected', [
121+
('"Hello" + "World"', '"Hello" + "World"'),
122+
('"Hello" * 5', '"Hello" * 5'),
123+
('b"Hello" + b"World"', 'b"Hello" + b"World"'),
124+
('b"Hello" * 5', 'b"Hello" * 5'),
125+
126+
])
127+
def test_not_eval(source, expected):
128+
"""
129+
Test BinOps we don't want to fold
130+
"""
131+
expected_ast = ast.parse(expected)
132+
actual_ast = fold_constants(
133+
source
134+
)
135+
compare_ast(expected_ast, actual_ast)
136+

0 commit comments

Comments
 (0)