11import ast
2+ import sys
3+
24import pytest
35
46from python_minifier import add_namespace
@@ -12,6 +14,14 @@ def fold_constants(source):
1214 FoldConstants ()(module )
1315 return module
1416
17+ def run_test (source , expected ):
18+ try :
19+ expected_ast = ast .parse (expected )
20+ except SyntaxError :
21+ pytest .skip ('Syntax not supported in this version of python' )
22+
23+ actual_ast = fold_constants (source )
24+ compare_ast (expected_ast , actual_ast )
1525
1626@pytest .mark .parametrize ('source,expected' , [
1727 ('True | True' , 'True' ),
@@ -65,11 +75,11 @@ def test_bool(source, expected):
6575
6676 This is mainly testing we fold the constants correctly
6777 """
68- expected_ast = ast . parse ( expected )
69- actual_ast = fold_constants (
70- source
71- )
72- compare_ast ( expected_ast , actual_ast )
78+
79+ if sys . version_info < ( 3 , 4 ):
80+ pytest . skip ( 'NameConstant not in python < 3.4' )
81+
82+ run_test ( source , expected )
7383
7484@pytest .mark .parametrize ('source,expected' , [
7585 ('10 + 10' , '20' ),
@@ -93,11 +103,8 @@ def test_int(source, expected):
93103 """
94104 Test BinOp with integer operands we can fold
95105 """
96- expected_ast = ast .parse (expected )
97- actual_ast = fold_constants (
98- source
99- )
100- compare_ast (expected_ast , actual_ast )
106+
107+ run_test (source , expected )
101108
102109@pytest .mark .parametrize ('source,expected' , [
103110 ('10/10' , '10/10' ),
@@ -111,26 +118,18 @@ def test_int_not_eval(source, expected):
111118 """
112119 Test BinOp with operations we don't want to fold
113120 """
114- expected_ast = ast .parse (expected )
115- actual_ast = fold_constants (
116- source
117- )
118- compare_ast (expected_ast , actual_ast )
121+
122+ run_test (source , expected )
119123
120124@pytest .mark .parametrize ('source,expected' , [
121125 ('"Hello" + "World"' , '"Hello" + "World"' ),
122126 ('"Hello" * 5' , '"Hello" * 5' ),
123127 ('b"Hello" + b"World"' , 'b"Hello" + b"World"' ),
124128 ('b"Hello" * 5' , 'b"Hello" * 5' ),
125-
126129])
127130def test_not_eval (source , expected ):
128131 """
129132 Test BinOps we don't want to fold
130133 """
131- expected_ast = ast .parse (expected )
132- actual_ast = fold_constants (
133- source
134- )
135- compare_ast (expected_ast , actual_ast )
136134
135+ run_test (source , expected )
0 commit comments