Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 6d66764

Browse files
committed
Split tests
1 parent 320e543 commit 6d66764

File tree

7 files changed

+651
-541
lines changed

7 files changed

+651
-541
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from rewrite.python import IntelliJ, SpacesVisitor
2+
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor
3+
4+
5+
def test_spaces_within_array_access_brackets():
6+
style = IntelliJ.spaces()
7+
style = style.with_within(
8+
style.within.with_brackets(False)
9+
).with_before_parentheses(
10+
style.before_parentheses.with_method_declaration(False)
11+
)
12+
rewrite_run(
13+
# language=python
14+
python(
15+
"""
16+
a [0]
17+
a [0 ]
18+
a[ 0 ]
19+
a[ 0][ 1]
20+
a [0 ][1 ]
21+
a[ 0 ][ 1 ]
22+
""",
23+
"""
24+
a[0]
25+
a[0]
26+
a[0]
27+
a[0][1]
28+
a[0][1]
29+
a[0][1]
30+
"""
31+
),
32+
spec=RecipeSpec()
33+
.with_recipe(from_visitor(SpacesVisitor(style)))
34+
)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import pytest
2+
3+
from rewrite.python import IntelliJ, SpacesVisitor
4+
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor
5+
6+
7+
def test_spaces_around_assignment():
8+
style = IntelliJ.spaces()
9+
style = style.with_around_operators(
10+
style.around_operators.with_assignment(True)
11+
)
12+
rewrite_run(
13+
# language=python
14+
python(
15+
"""
16+
a=1
17+
a= 1
18+
a =1
19+
def foo(x):
20+
x =1
21+
""",
22+
"""
23+
a = 1
24+
a = 1
25+
a = 1
26+
def foo(x):
27+
x = 1
28+
"""
29+
),
30+
spec=RecipeSpec()
31+
.with_recipe(from_visitor(SpacesVisitor(style)))
32+
)
33+
34+
35+
def test_spaces_around_chained_assignment():
36+
style = IntelliJ.spaces()
37+
style = style.with_around_operators(
38+
style.around_operators.with_assignment(True)
39+
)
40+
rewrite_run(
41+
# language=python
42+
python(
43+
"""
44+
a =b= 1 +2
45+
a=b=1 +2
46+
a=b =1 +2
47+
""",
48+
"""
49+
a = b = 1 + 2
50+
a = b = 1 + 2
51+
a = b = 1 + 2
52+
"""
53+
),
54+
spec=RecipeSpec()
55+
.with_recipe(from_visitor(SpacesVisitor(style)))
56+
)
57+
58+
59+
def test_spaces_around_assignment_op():
60+
style = IntelliJ.spaces()
61+
rewrite_run(
62+
# language=python
63+
python(
64+
"""
65+
a+=1
66+
a-= 1
67+
a +=1
68+
""",
69+
"""
70+
a += 1
71+
a -= 1
72+
a += 1
73+
"""
74+
),
75+
spec=RecipeSpec()
76+
.with_recipe(from_visitor(SpacesVisitor(style)))
77+
)
78+
79+
80+
def test_spaces_member_reference():
81+
style = IntelliJ.spaces()
82+
rewrite_run(
83+
# language=python
84+
python(
85+
"""
86+
class A:
87+
def __init__(self):
88+
self.a= 1
89+
inst : A = A()
90+
inst.a =1
91+
""",
92+
"""
93+
class A:
94+
def __init__(self):
95+
self.a = 1
96+
inst : A = A()
97+
inst.a = 1
98+
"""
99+
),
100+
spec=RecipeSpec()
101+
.with_recipe(from_visitor(SpacesVisitor(style)))
102+
)
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import pytest
2+
3+
from rewrite.python import IntelliJ, SpacesVisitor
4+
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor
5+
6+
7+
def test_spaces_around_assignment():
8+
style = IntelliJ.spaces()
9+
style = style.with_around_operators(
10+
style.around_operators.with_assignment(True)
11+
)
12+
rewrite_run(
13+
# language=python
14+
python(
15+
"""
16+
a=1
17+
a= 1
18+
a =1
19+
def foo(x):
20+
x =1
21+
""",
22+
"""
23+
a = 1
24+
a = 1
25+
a = 1
26+
def foo(x):
27+
x = 1
28+
"""
29+
),
30+
spec=RecipeSpec()
31+
.with_recipe(from_visitor(SpacesVisitor(style)))
32+
)
33+
34+
35+
def test_spaces_around_chained_assignment():
36+
style = IntelliJ.spaces()
37+
style = style.with_around_operators(
38+
style.around_operators.with_assignment(True)
39+
)
40+
rewrite_run(
41+
# language=python
42+
python(
43+
"""
44+
a =b= 1 +2
45+
a=b=1 +2
46+
a=b =1 +2
47+
""",
48+
"""
49+
a = b = 1 + 2
50+
a = b = 1 + 2
51+
a = b = 1 + 2
52+
"""
53+
),
54+
spec=RecipeSpec()
55+
.with_recipe(from_visitor(SpacesVisitor(style)))
56+
)
57+
58+
59+
def test_spaces_around_assignment_op():
60+
style = IntelliJ.spaces()
61+
rewrite_run(
62+
# language=python
63+
python(
64+
"""
65+
a+=1
66+
a-= 1
67+
a +=1
68+
""",
69+
"""
70+
a += 1
71+
a -= 1
72+
a += 1
73+
"""
74+
),
75+
spec=RecipeSpec()
76+
.with_recipe(from_visitor(SpacesVisitor(style)))
77+
)
78+
79+
80+
def test_spaces_member_reference():
81+
style = IntelliJ.spaces()
82+
rewrite_run(
83+
# language=python
84+
python(
85+
"""
86+
class A:
87+
def __init__(self):
88+
self.a= 1
89+
inst : A = A()
90+
inst.a =1
91+
""",
92+
"""
93+
class A:
94+
def __init__(self):
95+
self.a = 1
96+
inst : A = A()
97+
inst.a = 1
98+
"""
99+
),
100+
spec=RecipeSpec()
101+
.with_recipe(from_visitor(SpacesVisitor(style)))
102+
)
103+
104+
105+
@pytest.mark.parametrize("binary_op", [
106+
"+", "-", "*", "/", "%", "<", ">", "<=", ">=", "==", "!=", "&", "|", "^", "<<", ">>"
107+
])
108+
def test_spaces_binary_operators(binary_op):
109+
style = IntelliJ.spaces()
110+
rewrite_run(
111+
python(
112+
f"""
113+
1{binary_op}1
114+
1{binary_op} 1
115+
1 {binary_op}1
116+
1 {binary_op} 1
117+
a{binary_op} b
118+
""",
119+
f"""
120+
1 {binary_op} 1
121+
1 {binary_op} 1
122+
1 {binary_op} 1
123+
1 {binary_op} 1
124+
a {binary_op} b
125+
"""
126+
),
127+
spec=RecipeSpec()
128+
.with_recipe(from_visitor(SpacesVisitor(style)))
129+
)
130+
131+
132+
@pytest.mark.parametrize('python_binary_op', ["in", "is", "is not", "not in", "and", "or"])
133+
@pytest.mark.parametrize('left_space', ['', ' '])
134+
@pytest.mark.parametrize('right_space', ['', ' '])
135+
def test_spaces_python_binary_operators_on_lists(python_binary_op, left_space, right_space):
136+
style = IntelliJ.spaces()
137+
rewrite_run(
138+
python(
139+
f"""
140+
[3]{left_space}{python_binary_op}{right_space}[1, 2, 3, 4]
141+
[3]{left_space}{python_binary_op}{left_space}[a]
142+
""",
143+
f"""
144+
[3] {python_binary_op} [1, 2, 3, 4]
145+
[3] {python_binary_op} [a]
146+
"""
147+
),
148+
spec=RecipeSpec()
149+
.with_recipe(from_visitor(SpacesVisitor(style)))
150+
)
151+
152+
153+
@pytest.mark.parametrize('python_binary_op', ["//", "@", "**", "+"])
154+
@pytest.mark.parametrize('left_space', ['', ' '])
155+
@pytest.mark.parametrize('right_space', ['', ' '])
156+
def test_spaces_python_binary_operators_numbers(python_binary_op, left_space, right_space):
157+
style = IntelliJ.spaces()
158+
rewrite_run(
159+
python(
160+
f"""
161+
a{left_space}{python_binary_op}{right_space}b
162+
""",
163+
f"""
164+
a {python_binary_op} b
165+
"""
166+
),
167+
spec=RecipeSpec()
168+
.with_recipe(from_visitor(SpacesVisitor(style)))
169+
)
170+
171+
172+
@pytest.mark.parametrize('left_space', ['', ' '])
173+
@pytest.mark.parametrize('right_space', ['', ' '])
174+
def test_spaces_python_binary_operators_string(left_space, right_space):
175+
style = IntelliJ.spaces()
176+
rewrite_run(
177+
python(
178+
f"""
179+
"Hello"{left_space}+{right_space}"World!"
180+
""",
181+
"""
182+
"Hello" + "World!"
183+
"""
184+
),
185+
spec=RecipeSpec()
186+
.with_recipe(from_visitor(SpacesVisitor(style)))
187+
)

0 commit comments

Comments
 (0)