Skip to content

Commit 1489c40

Browse files
committed
Add a handful of tests for Selection support <#297>.
1 parent 0abec59 commit 1489c40

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import SwiftFormat
2+
import XCTest
3+
4+
final class Selections: PrettyPrintTestCase {
5+
func testSelectAll() {
6+
let input =
7+
"""
8+
➡️func foo() {
9+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
10+
// do stuff
11+
}
12+
}⬅️
13+
"""
14+
15+
let expected =
16+
"""
17+
func foo() {
18+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
19+
// do stuff
20+
}
21+
}
22+
"""
23+
24+
// The line length ends on the last paren of .Stuff()
25+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
26+
}
27+
28+
func testSelectComment() {
29+
let input =
30+
"""
31+
func foo() {
32+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
33+
➡️// do stuff⬅️
34+
}
35+
}
36+
"""
37+
38+
let expected =
39+
"""
40+
func foo() {
41+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
42+
// do stuff
43+
}
44+
}
45+
"""
46+
47+
// The line length ends on the last paren of .Stuff()
48+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
49+
}
50+
51+
func testInsertionPointBeforeComment() {
52+
let input =
53+
"""
54+
func foo() {
55+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
56+
➡️⬅️// do stuff
57+
}
58+
}
59+
"""
60+
61+
let expected =
62+
"""
63+
func foo() {
64+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
65+
// do stuff
66+
}
67+
}
68+
"""
69+
70+
// The line length ends on the last paren of .Stuff()
71+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
72+
}
73+
74+
func testSpacesInline() {
75+
let input =
76+
"""
77+
func foo() {
78+
if let SomeReallyLongVar ➡️ = ⬅️Some.More.Stuff(), let a = myfunc() {
79+
// do stuff
80+
}
81+
}
82+
"""
83+
84+
let expected =
85+
"""
86+
func foo() {
87+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
88+
// do stuff
89+
}
90+
}
91+
"""
92+
93+
// The line length ends on the last paren of .Stuff()
94+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
95+
}
96+
97+
func testSpacesFullLine() {
98+
// FIXME: we should be able to set the end range to _after_ the opening brace.
99+
// Currently, this also indents the comment line. Also, it would fail idempotency (since the
100+
// tests don't update the selection, and it would no longer end after the brace)
101+
let input =
102+
"""
103+
func foo() {
104+
➡️if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() ⬅️{
105+
// do stuff
106+
}
107+
}
108+
"""
109+
110+
let expected =
111+
"""
112+
func foo() {
113+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
114+
// do stuff
115+
}
116+
}
117+
"""
118+
119+
// The line length ends on the last paren of .Stuff()
120+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
121+
}
122+
123+
func testWrapInline() {
124+
let input =
125+
"""
126+
func foo() {
127+
if let SomeReallyLongVar = ➡️Some.More.Stuff(), let a = myfunc()⬅️ {
128+
// do stuff
129+
}
130+
}
131+
"""
132+
133+
let expected =
134+
"""
135+
func foo() {
136+
if let SomeReallyLongVar = Some.More
137+
.Stuff(), let a = myfunc() {
138+
// do stuff
139+
}
140+
}
141+
"""
142+
143+
// The line length ends on the last paren of .Stuff()
144+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 44)
145+
}
146+
147+
func testCommentsOnly() {
148+
let input =
149+
"""
150+
func foo() {
151+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
152+
➡️// do stuff
153+
// do more stuff⬅️
154+
var i = 0
155+
}
156+
}
157+
"""
158+
159+
let expected =
160+
"""
161+
func foo() {
162+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
163+
// do stuff
164+
// do more stuff
165+
var i = 0
166+
}
167+
}
168+
"""
169+
170+
// The line length ends on the last paren of .Stuff()
171+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
172+
}
173+
174+
func testVarOnly() {
175+
let input =
176+
"""
177+
func foo() {
178+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
179+
// do stuff
180+
// do more stuff
181+
➡️⬅️var i = 0
182+
}
183+
}
184+
"""
185+
186+
let expected =
187+
"""
188+
func foo() {
189+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
190+
// do stuff
191+
// do more stuff
192+
var i = 0
193+
}
194+
}
195+
"""
196+
197+
// The line length ends on the last paren of .Stuff()
198+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
199+
}
200+
201+
#if false
202+
func testFirstCommentAndVar() {
203+
let input =
204+
"""
205+
func foo() {
206+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
207+
➡️⬅️// do stuff
208+
// do more stuff
209+
➡️⬅️var i = 0
210+
}
211+
}
212+
"""
213+
214+
// FIXME: this shouldn't indent the second comment
215+
// Also not idempotent since the selection doesn't get updated.
216+
let expected =
217+
"""
218+
func foo() {
219+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
220+
// do stuff
221+
// do more stuff
222+
var i = 0
223+
}
224+
}
225+
"""
226+
227+
// The line length ends on the last paren of .Stuff()
228+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
229+
}
230+
#endif
231+
}

0 commit comments

Comments
 (0)