Skip to content

Commit 11dbb21

Browse files
authored
Add Spacer support (#380)
* Add Spacer implementation * Add Spacer UI Tests
1 parent 4702415 commit 11dbb21

File tree

5 files changed

+748
-124
lines changed

5 files changed

+748
-124
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
//
2+
// SpacerUITests.swift
3+
// OpenSwiftUIUITests
4+
5+
import Testing
6+
import SnapshotTesting
7+
8+
@MainActor
9+
@Suite(.snapshots(record: .never, diffTool: diffTool))
10+
struct SpacerUITests {
11+
12+
// MARK: - Basic Spacer in HStack
13+
14+
@Test
15+
func spacerInHStack() {
16+
struct ContentView: View {
17+
var body: some View {
18+
HStack {
19+
Color.red
20+
.frame(width: 50, height: 50)
21+
Spacer()
22+
Color.blue
23+
.frame(width: 50, height: 50)
24+
}
25+
.frame(width: 200, height: 80)
26+
.background(Color.gray.opacity(0.3))
27+
}
28+
}
29+
openSwiftUIAssertSnapshot(of: ContentView())
30+
}
31+
32+
@Test
33+
func spacerInVStack() {
34+
struct ContentView: View {
35+
var body: some View {
36+
VStack {
37+
Color.red
38+
.frame(width: 50, height: 50)
39+
Spacer()
40+
Color.blue
41+
.frame(width: 50, height: 50)
42+
}
43+
.frame(width: 80, height: 200)
44+
.background(Color.gray.opacity(0.3))
45+
}
46+
}
47+
openSwiftUIAssertSnapshot(of: ContentView())
48+
}
49+
50+
// MARK: - Multiple Spacers
51+
52+
@Test
53+
func multipleSpacersInHStack() {
54+
struct ContentView: View {
55+
var body: some View {
56+
HStack {
57+
Color.red
58+
.frame(width: 40, height: 50)
59+
Spacer()
60+
Color.blue
61+
.frame(width: 40, height: 50)
62+
Spacer()
63+
Color.green
64+
.frame(width: 40, height: 50)
65+
}
66+
.frame(width: 250, height: 80)
67+
.background(Color.gray.opacity(0.3))
68+
}
69+
}
70+
openSwiftUIAssertSnapshot(of: ContentView())
71+
}
72+
73+
@Test
74+
func multipleSpacersInVStack() {
75+
struct ContentView: View {
76+
var body: some View {
77+
VStack {
78+
Color.red
79+
.frame(width: 50, height: 40)
80+
Spacer()
81+
Color.blue
82+
.frame(width: 50, height: 40)
83+
Spacer()
84+
Color.green
85+
.frame(width: 50, height: 40)
86+
}
87+
.frame(width: 80, height: 250)
88+
.background(Color.gray.opacity(0.3))
89+
}
90+
}
91+
openSwiftUIAssertSnapshot(of: ContentView())
92+
}
93+
94+
// MARK: - Spacer with MinLength
95+
96+
@Test
97+
func spacerWithMinLengthInHStack() {
98+
struct ContentView: View {
99+
var body: some View {
100+
HStack {
101+
Color.red
102+
.frame(width: 50, height: 50)
103+
Spacer(minLength: 50)
104+
Color.blue
105+
.frame(width: 50, height: 50)
106+
}
107+
.frame(width: 180, height: 80)
108+
.background(Color.gray.opacity(0.3))
109+
}
110+
}
111+
openSwiftUIAssertSnapshot(of: ContentView())
112+
}
113+
114+
@Test
115+
func spacerWithMinLengthInVStack() {
116+
struct ContentView: View {
117+
var body: some View {
118+
VStack {
119+
Color.red
120+
.frame(width: 50, height: 50)
121+
Spacer(minLength: 30)
122+
Color.blue
123+
.frame(width: 50, height: 50)
124+
}
125+
.frame(width: 80, height: 160)
126+
.background(Color.gray.opacity(0.3))
127+
}
128+
}
129+
openSwiftUIAssertSnapshot(of: ContentView())
130+
}
131+
132+
// MARK: - Spacer at Different Positions
133+
134+
@Test
135+
func spacerAtStartInHStack() {
136+
struct ContentView: View {
137+
var body: some View {
138+
HStack {
139+
Spacer()
140+
Color.red
141+
.frame(width: 50, height: 50)
142+
Color.blue
143+
.frame(width: 50, height: 50)
144+
}
145+
.frame(width: 200, height: 80)
146+
.background(Color.gray.opacity(0.3))
147+
}
148+
}
149+
openSwiftUIAssertSnapshot(of: ContentView())
150+
}
151+
152+
@Test
153+
func spacerAtEndInHStack() {
154+
struct ContentView: View {
155+
var body: some View {
156+
HStack {
157+
Color.red
158+
.frame(width: 50, height: 50)
159+
Color.blue
160+
.frame(width: 50, height: 50)
161+
Spacer()
162+
}
163+
.frame(width: 200, height: 80)
164+
.background(Color.gray.opacity(0.3))
165+
}
166+
}
167+
openSwiftUIAssertSnapshot(of: ContentView())
168+
}
169+
170+
@Test
171+
func spacersAtBothEndsInHStack() {
172+
struct ContentView: View {
173+
var body: some View {
174+
HStack {
175+
Spacer()
176+
Color.red
177+
.frame(width: 50, height: 50)
178+
Color.blue
179+
.frame(width: 50, height: 50)
180+
Spacer()
181+
}
182+
.frame(width: 200, height: 80)
183+
.background(Color.gray.opacity(0.3))
184+
}
185+
}
186+
openSwiftUIAssertSnapshot(of: ContentView())
187+
}
188+
189+
// MARK: - Nested Stacks with Spacers
190+
191+
@Test
192+
func nestedStacksWithSpacers() {
193+
struct ContentView: View {
194+
var body: some View {
195+
VStack {
196+
HStack {
197+
Color.red
198+
.frame(width: 40, height: 40)
199+
Spacer()
200+
Color.blue
201+
.frame(width: 40, height: 40)
202+
}
203+
204+
Spacer()
205+
206+
HStack {
207+
Spacer()
208+
Color.green
209+
.frame(width: 40, height: 40)
210+
Spacer()
211+
Color.yellow
212+
.frame(width: 40, height: 40)
213+
Spacer()
214+
}
215+
}
216+
.frame(width: 200, height: 200)
217+
.background(Color.gray.opacity(0.3))
218+
}
219+
}
220+
openSwiftUIAssertSnapshot(of: ContentView())
221+
}
222+
223+
// MARK: - Spacer with Zero MinLength
224+
225+
@Test
226+
func spacerWithZeroMinLength() {
227+
struct ContentView: View {
228+
var body: some View {
229+
HStack {
230+
Color.red
231+
.frame(width: 80, height: 50)
232+
Spacer(minLength: 0)
233+
Color.blue
234+
.frame(width: 80, height: 50)
235+
}
236+
.frame(width: 160, height: 80)
237+
.background(Color.gray.opacity(0.3))
238+
}
239+
}
240+
openSwiftUIAssertSnapshot(of: ContentView())
241+
}
242+
243+
// MARK: - Spacer with Large MinLength
244+
245+
@Test
246+
func spacerWithLargeMinLength() {
247+
struct ContentView: View {
248+
var body: some View {
249+
HStack {
250+
Color.red
251+
.frame(width: 30, height: 50)
252+
Spacer(minLength: 100)
253+
Color.blue
254+
.frame(width: 30, height: 50)
255+
}
256+
.frame(width: 200, height: 80)
257+
.background(Color.gray.opacity(0.3))
258+
}
259+
}
260+
openSwiftUIAssertSnapshot(of: ContentView())
261+
}
262+
263+
// MARK: - Stack Without Frame Constraint
264+
265+
@Test
266+
func spacerInHStackWithoutFrame() {
267+
struct ContentView: View {
268+
var body: some View {
269+
HStack {
270+
Color.red
271+
.frame(width: 50, height: 50)
272+
Spacer()
273+
Color.blue
274+
.frame(width: 50, height: 50)
275+
}
276+
.background(Color.gray.opacity(0.3))
277+
}
278+
}
279+
openSwiftUIAssertSnapshot(of: ContentView())
280+
}
281+
282+
@Test
283+
func spacerInVStackWithoutFrame() {
284+
struct ContentView: View {
285+
var body: some View {
286+
VStack {
287+
Color.red
288+
.frame(width: 50, height: 50)
289+
Spacer()
290+
Color.blue
291+
.frame(width: 50, height: 50)
292+
}
293+
.background(Color.gray.opacity(0.3))
294+
}
295+
}
296+
openSwiftUIAssertSnapshot(of: ContentView())
297+
}
298+
299+
// MARK: - Complex Layout
300+
301+
@Test
302+
func complexLayoutWithMultipleSpacers() {
303+
struct ContentView: View {
304+
var body: some View {
305+
VStack(spacing: 20) {
306+
HStack {
307+
Color.red
308+
.frame(width: 30, height: 30)
309+
Spacer(minLength: 20)
310+
Color.blue
311+
.frame(width: 30, height: 30)
312+
Spacer(minLength: 10)
313+
Color.green
314+
.frame(width: 30, height: 30)
315+
}
316+
317+
HStack {
318+
Spacer()
319+
Color.yellow
320+
.frame(width: 60, height: 30)
321+
Spacer()
322+
}
323+
324+
HStack {
325+
Color.orange
326+
.frame(width: 40, height: 30)
327+
Spacer()
328+
Color.purple
329+
.frame(width: 40, height: 30)
330+
}
331+
}
332+
.frame(width: 200, height: 180)
333+
.background(Color.gray.opacity(0.3))
334+
}
335+
}
336+
openSwiftUIAssertSnapshot(of: ContentView())
337+
}
338+
}

0 commit comments

Comments
 (0)