Skip to content

Commit b972772

Browse files
authored
Added more directory result builder functions (#2)
1 parent 6052ed5 commit b972772

File tree

2 files changed

+139
-8
lines changed

2 files changed

+139
-8
lines changed

Sources/FileManagerKitTesting/FileManagerPlayground.swift

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,46 @@ public struct FileManagerPlayground {
133133

134134
@resultBuilder
135135
public enum DirectoryBuilder {
136+
public static func buildBlock(_ components: [Item]...) -> [Item] {
137+
components.flatMap { $0 }
138+
}
139+
140+
public static func buildExpression(_ expression: File) -> [Item] {
141+
[.file(expression)]
142+
}
143+
144+
public static func buildExpression(_ expression: Directory) -> [Item] {
145+
[.directory(expression)]
146+
}
147+
148+
public static func buildExpression(_ expression: SymbolicLink) -> [Item]
149+
{
150+
[.symbolicLink(expression)]
151+
}
152+
153+
// Optionally allow string literals to be treated as files:
154+
public static func buildExpression(_ expression: String) -> [Item] {
155+
[.file(File(expression, contents: nil))]
156+
}
157+
158+
public static func buildExpression(_ expression: [Item]) -> [Item] {
159+
expression
160+
}
136161

137-
public static func buildBlock(_ components: Item...) -> [Item] {
138-
components
162+
public static func buildOptional(_ component: [Item]?) -> [Item] {
163+
component ?? []
139164
}
140165

141-
public static func buildExpression(_ expression: File) -> Item {
142-
.file(expression)
166+
public static func buildEither(first component: [Item]) -> [Item] {
167+
component
143168
}
144169

145-
public static func buildExpression(_ expression: Directory) -> Item {
146-
.directory(expression)
170+
public static func buildEither(second component: [Item]) -> [Item] {
171+
component
147172
}
148173

149-
public static func buildExpression(_ expression: SymbolicLink) -> Item {
150-
.symbolicLink(expression)
174+
public static func buildArray(_ components: [[Item]]) -> [Item] {
175+
components.flatMap { $0 }
151176
}
152177
}
153178

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// File.swift
3+
// file-manager-kit
4+
//
5+
// Created by Viasz-Kádi Ferenc on 2025. 04. 01..
6+
//
7+
8+
import Foundation
9+
import Testing
10+
11+
@testable import FileManagerKitTesting
12+
13+
struct DirectoryBuilderTests {
14+
15+
@Test
16+
func builder_AllFeatures() throws {
17+
let includeOptional = true
18+
let useFirst = false
19+
let useThird = true
20+
let dynamicFiles = (1...2)
21+
.map { i in
22+
File("dynamic\(i).txt", contents: nil)
23+
}
24+
let injected: [FileManagerPlayground.Item] = [
25+
.file(File("injected1.txt", contents: nil)),
26+
.file(File("injected2.txt", contents: nil)),
27+
]
28+
29+
try FileManagerPlayground {
30+
Directory("root") {
31+
File("static.md")
32+
33+
if includeOptional {
34+
File("optional.txt")
35+
}
36+
37+
if useFirst {
38+
File("first-choice.txt")
39+
}
40+
else {
41+
File("second-choice.txt")
42+
}
43+
44+
if useThird {
45+
File("third-choice.txt")
46+
}
47+
else {
48+
File("forth-choice.txt")
49+
}
50+
51+
Directory("looped") {
52+
for file in dynamicFiles {
53+
file
54+
}
55+
}
56+
57+
Directory("empty") {}
58+
59+
Directory("nested") {
60+
Directory("deeper") {
61+
File("deep.txt")
62+
}
63+
}
64+
65+
SymbolicLink("link", destination: "static.md")
66+
67+
injected
68+
69+
// Multiple arrays in one block
70+
[
71+
.file(File("array1.txt")),
72+
.file(File("array2.txt")),
73+
]
74+
[
75+
.file(File("array3.txt"))
76+
]
77+
}
78+
}
79+
.test { fileManager, rootUrl in
80+
let checkPaths = [
81+
"root/static.md",
82+
"root/optional.txt",
83+
"root/second-choice.txt",
84+
"root/third-choice.txt",
85+
"root/looped/dynamic1.txt",
86+
"root/looped/dynamic2.txt",
87+
"root/empty",
88+
"root/nested/deeper/deep.txt",
89+
"root/link",
90+
"root/injected1.txt",
91+
"root/injected2.txt",
92+
"root/array1.txt",
93+
"root/array2.txt",
94+
"root/array3.txt",
95+
]
96+
97+
for path in checkPaths {
98+
let url = rootUrl.appending(path: path)
99+
#expect(
100+
fileManager.exists(at: url),
101+
"Expected file or directory at: \(path)"
102+
)
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)