@@ -12,14 +12,20 @@ import Basics
12
12
import Foundation
13
13
import XCTest
14
14
15
+ #if os(Windows)
16
+ private var windows : Bool { true }
17
+ #else
18
+ private var windows : Bool { false }
19
+ #endif
20
+
15
21
class PathTests : XCTestCase {
16
22
func testBasics( ) {
17
- XCTAssertEqual ( AbsolutePath ( " / " ) . pathString, " / " )
18
- XCTAssertEqual ( AbsolutePath ( " /a " ) . pathString, " /a " )
19
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . pathString, " /a/b/c " )
23
+ XCTAssertEqual ( AbsolutePath ( " / " ) . pathString, windows ? #"\"# : " / " )
24
+ XCTAssertEqual ( AbsolutePath ( " /a " ) . pathString, windows ? #"\a"# : " /a " )
25
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . pathString, windows ? #"\a\b\c"# : " /a/b/c " )
20
26
XCTAssertEqual ( RelativePath ( " . " ) . pathString, " . " )
21
27
XCTAssertEqual ( RelativePath ( " a " ) . pathString, " a " )
22
- XCTAssertEqual ( RelativePath ( " a/b/c " ) . pathString, " a/b/c " )
28
+ XCTAssertEqual ( RelativePath ( " a/b/c " ) . pathString, windows ? #"a\b\c"# : " a/b/c " )
23
29
XCTAssertEqual ( RelativePath ( " ~ " ) . pathString, " ~ " ) // `~` is not special
24
30
}
25
31
@@ -41,25 +47,25 @@ class PathTests: XCTestCase {
41
47
42
48
func testStringLiteralInitialization( ) {
43
49
let abs = AbsolutePath ( " / " )
44
- XCTAssertEqual ( abs. pathString, " / " )
50
+ XCTAssertEqual ( abs. pathString, windows ? #"\"# : " / " )
45
51
let rel1 = RelativePath ( " . " )
46
52
XCTAssertEqual ( rel1. pathString, " . " )
47
53
let rel2 = RelativePath ( " ~ " )
48
54
XCTAssertEqual ( rel2. pathString, " ~ " ) // `~` is not special
49
55
}
50
56
51
57
func testRepeatedPathSeparators( ) {
52
- XCTAssertEqual ( AbsolutePath ( " /ab//cd//ef " ) . pathString, " /ab/cd/ef " )
53
- XCTAssertEqual ( AbsolutePath ( " /ab///cd//ef " ) . pathString, " /ab/cd/ef " )
54
- XCTAssertEqual ( RelativePath ( " ab//cd//ef " ) . pathString, " ab/cd/ef " )
55
- XCTAssertEqual ( RelativePath ( " ab//cd///ef " ) . pathString, " ab/cd/ef " )
58
+ XCTAssertEqual ( AbsolutePath ( " /ab//cd//ef " ) . pathString, windows ? #"\ab\cd\ef"# : " /ab/cd/ef " )
59
+ XCTAssertEqual ( AbsolutePath ( " /ab///cd//ef " ) . pathString, windows ? #"\ab\cd\ef"# : " /ab/cd/ef " )
60
+ XCTAssertEqual ( RelativePath ( " ab//cd//ef " ) . pathString, windows ? #"ab\cd\ef"# : " ab/cd/ef " )
61
+ XCTAssertEqual ( RelativePath ( " ab//cd///ef " ) . pathString, windows ? #"ab\cd\ef"# : " ab/cd/ef " )
56
62
}
57
63
58
64
func testTrailingPathSeparators( ) {
59
- XCTAssertEqual ( AbsolutePath ( " /ab/cd/ef/ " ) . pathString, " /ab/cd/ef " )
60
- XCTAssertEqual ( AbsolutePath ( " /ab/cd/ef// " ) . pathString, " /ab/cd/ef " )
61
- XCTAssertEqual ( RelativePath ( " ab/cd/ef/ " ) . pathString, " ab/cd/ef " )
62
- XCTAssertEqual ( RelativePath ( " ab/cd/ef// " ) . pathString, " ab/cd/ef " )
65
+ XCTAssertEqual ( AbsolutePath ( " /ab/cd/ef/ " ) . pathString, windows ? #"\ab\cd\ef"# : " /ab/cd/ef " )
66
+ XCTAssertEqual ( AbsolutePath ( " /ab/cd/ef// " ) . pathString, windows ? #"\ab\cd\ef"# : " /ab/cd/ef " )
67
+ XCTAssertEqual ( RelativePath ( " ab/cd/ef/ " ) . pathString, windows ? #"ab\cd\ef"# : " ab/cd/ef " )
68
+ XCTAssertEqual ( RelativePath ( " ab/cd/ef// " ) . pathString, windows ? #"ab\cd\ef"# : " ab/cd/ef " )
63
69
}
64
70
65
71
func testDotPathComponents( ) {
@@ -70,13 +76,13 @@ class PathTests: XCTestCase {
70
76
}
71
77
72
78
func testDotDotPathComponents( ) {
73
- XCTAssertEqual ( AbsolutePath ( " /.. " ) . pathString, " / " )
74
- XCTAssertEqual ( AbsolutePath ( " /../../../../.. " ) . pathString, " / " )
75
- XCTAssertEqual ( AbsolutePath ( " /abc/.. " ) . pathString, " / " )
76
- XCTAssertEqual ( AbsolutePath ( " /abc/../.. " ) . pathString, " / " )
77
- XCTAssertEqual ( AbsolutePath ( " /../abc " ) . pathString, " /abc " )
78
- XCTAssertEqual ( AbsolutePath ( " /../abc/.. " ) . pathString, " / " )
79
- XCTAssertEqual ( AbsolutePath ( " /../abc/../def " ) . pathString, " /def " )
79
+ XCTAssertEqual ( AbsolutePath ( " /.. " ) . pathString, windows ? #"\"# : " / " )
80
+ XCTAssertEqual ( AbsolutePath ( " /../../../../.. " ) . pathString, windows ? #"\"# : " / " )
81
+ XCTAssertEqual ( AbsolutePath ( " /abc/.. " ) . pathString, windows ? #"\"# : " / " )
82
+ XCTAssertEqual ( AbsolutePath ( " /abc/../.. " ) . pathString, windows ? #"\"# : " / " )
83
+ XCTAssertEqual ( AbsolutePath ( " /../abc " ) . pathString, windows ? #"\abc"# : " /abc " )
84
+ XCTAssertEqual ( AbsolutePath ( " /../abc/.. " ) . pathString, windows ? #"\"# : " / " )
85
+ XCTAssertEqual ( AbsolutePath ( " /../abc/../def " ) . pathString, windows ? #"\def"# : " /def " )
80
86
XCTAssertEqual ( RelativePath ( " .. " ) . pathString, " .. " )
81
87
XCTAssertEqual ( RelativePath ( " ../.. " ) . pathString, " ../.. " )
82
88
XCTAssertEqual ( RelativePath ( " .././.. " ) . pathString, " ../.. " )
@@ -86,8 +92,8 @@ class PathTests: XCTestCase {
86
92
}
87
93
88
94
func testCombinationsAndEdgeCases( ) {
89
- XCTAssertEqual ( AbsolutePath ( " /// " ) . pathString, " / " )
90
- XCTAssertEqual ( AbsolutePath ( " /./ " ) . pathString, " / " )
95
+ XCTAssertEqual ( AbsolutePath ( " /// " ) . pathString, windows ? #"\"# : " / " )
96
+ XCTAssertEqual ( AbsolutePath ( " /./ " ) . pathString, windows ? #"\"# : " / " )
91
97
XCTAssertEqual ( RelativePath ( " " ) . pathString, " . " )
92
98
XCTAssertEqual ( RelativePath ( " . " ) . pathString, " . " )
93
99
XCTAssertEqual ( RelativePath ( " ./abc " ) . pathString, " abc " )
@@ -108,19 +114,19 @@ class PathTests: XCTestCase {
108
114
XCTAssertEqual ( RelativePath ( " ./.. " ) . pathString, " .. " )
109
115
XCTAssertEqual ( RelativePath ( " ./../. " ) . pathString, " .. " )
110
116
XCTAssertEqual ( RelativePath ( " ./////../////.///// " ) . pathString, " .. " )
111
- XCTAssertEqual ( RelativePath ( " ../a " ) . pathString, " ../a " )
117
+ XCTAssertEqual ( RelativePath ( " ../a " ) . pathString, windows ? #"..\a"# : " ../a " )
112
118
XCTAssertEqual ( RelativePath ( " ../a/.. " ) . pathString, " .. " )
113
119
XCTAssertEqual ( RelativePath ( " a/.. " ) . pathString, " . " )
114
120
XCTAssertEqual ( RelativePath ( " a/../////../////.///// " ) . pathString, " .. " )
115
121
}
116
122
117
123
func testDirectoryNameExtraction( ) {
118
- XCTAssertEqual ( AbsolutePath ( " / " ) . dirname, " / " )
119
- XCTAssertEqual ( AbsolutePath ( " /a " ) . dirname, " / " )
120
- XCTAssertEqual ( AbsolutePath ( " /./a " ) . dirname, " / " )
121
- XCTAssertEqual ( AbsolutePath ( " /../.. " ) . dirname, " / " )
122
- XCTAssertEqual ( AbsolutePath ( " /ab/c//d/ " ) . dirname, " /ab/c " )
123
- XCTAssertEqual ( RelativePath ( " ab/c//d/ " ) . dirname, " ab/c " )
124
+ XCTAssertEqual ( AbsolutePath ( " / " ) . dirname, windows ? #"\"# : " / " )
125
+ XCTAssertEqual ( AbsolutePath ( " /a " ) . dirname, windows ? #"\"# : " / " )
126
+ XCTAssertEqual ( AbsolutePath ( " /./a " ) . dirname, windows ? #"\"# : " / " )
127
+ XCTAssertEqual ( AbsolutePath ( " /../.. " ) . dirname, windows ? #"\"# : " / " )
128
+ XCTAssertEqual ( AbsolutePath ( " /ab/c//d/ " ) . dirname, windows ? #"\ab\c"# : " /ab/c " )
129
+ XCTAssertEqual ( RelativePath ( " ab/c//d/ " ) . dirname, windows ? #"ab\c"# : " ab/c " )
124
130
XCTAssertEqual ( RelativePath ( " ../a " ) . dirname, " .. " )
125
131
XCTAssertEqual ( RelativePath ( " ../a/.. " ) . dirname, " . " )
126
132
XCTAssertEqual ( RelativePath ( " a/.. " ) . dirname, " . " )
@@ -132,7 +138,7 @@ class PathTests: XCTestCase {
132
138
}
133
139
134
140
func testBaseNameExtraction( ) {
135
- XCTAssertEqual ( AbsolutePath ( " / " ) . basename, " / " )
141
+ XCTAssertEqual ( AbsolutePath ( " / " ) . basename, windows ? #"\"# : " / " )
136
142
XCTAssertEqual ( AbsolutePath ( " /a " ) . basename, " a " )
137
143
XCTAssertEqual ( AbsolutePath ( " /./a " ) . basename, " a " )
138
144
XCTAssertEqual ( AbsolutePath ( " /../.. " ) . basename, " / " )
@@ -148,7 +154,7 @@ class PathTests: XCTestCase {
148
154
}
149
155
150
156
func testBaseNameWithoutExt( ) {
151
- XCTAssertEqual ( AbsolutePath ( " / " ) . basenameWithoutExt, " / " )
157
+ XCTAssertEqual ( AbsolutePath ( " / " ) . basenameWithoutExt, windows ? #"\"# : " / " )
152
158
XCTAssertEqual ( AbsolutePath ( " /a " ) . basenameWithoutExt, " a " )
153
159
XCTAssertEqual ( AbsolutePath ( " /./a " ) . basenameWithoutExt, " a " )
154
160
XCTAssertEqual ( AbsolutePath ( " /../.. " ) . basenameWithoutExt, " / " )
@@ -205,40 +211,40 @@ class PathTests: XCTestCase {
205
211
206
212
@available ( * , deprecated)
207
213
func testConcatenation( ) {
208
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " " ) ) . pathString, " / " )
209
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " . " ) ) . pathString, " / " )
210
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " .. " ) ) . pathString, " / " )
211
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " bar " ) ) . pathString, " /bar " )
212
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /foo/bar " ) , RelativePath ( " .. " ) ) . pathString, " /foo " )
213
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar " ) , RelativePath ( " ../foo " ) ) . pathString, " /foo " )
214
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar " ) , RelativePath ( " ../foo/..// " ) ) . pathString, " / " )
215
- XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar/../foo/..//yabba/ " ) , RelativePath ( " a/b " ) ) . pathString, " /yabba/a/b " )
216
-
217
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " " ) ) . pathString, " / " )
218
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " . " ) ) . pathString, " / " )
219
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " .. " ) ) . pathString, " / " )
220
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " bar " ) ) . pathString, " /bar " )
221
- XCTAssertEqual ( AbsolutePath ( " /foo/bar " ) . appending ( RelativePath ( " .. " ) ) . pathString, " /foo " )
222
- XCTAssertEqual ( AbsolutePath ( " /bar " ) . appending ( RelativePath ( " ../foo " ) ) . pathString, " /foo " )
223
- XCTAssertEqual ( AbsolutePath ( " /bar " ) . appending ( RelativePath ( " ../foo/..// " ) ) . pathString, " / " )
224
- XCTAssertEqual ( AbsolutePath ( " /bar/../foo/..//yabba/ " ) . appending ( RelativePath ( " a/b " ) ) . pathString, " /yabba/a/b " )
225
-
226
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( component: " a " ) . pathString, " /a " )
227
- XCTAssertEqual ( AbsolutePath ( " /a " ) . appending ( component: " b " ) . pathString, " /a/b " )
228
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " a " , " b " ) . pathString, " /a/b " )
229
- XCTAssertEqual ( AbsolutePath ( " /a " ) . appending ( components: " b " , " c " ) . pathString, " /a/b/c " )
230
-
231
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " " , " c " ) . pathString, " /a/b/c/c " )
232
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " " ) . pathString, " /a/b/c " )
233
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " . " ) . pathString, " /a/b/c " )
234
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " .. " ) . pathString, " /a/b " )
235
- XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " .. " , " d " ) . pathString, " /a/b/d " )
236
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " .. " ) . pathString, " / " )
237
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " . " ) . pathString, " / " )
238
- XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " .. " , " a " ) . pathString, " /a " )
239
-
240
- XCTAssertEqual ( RelativePath ( " hello " ) . appending ( components: " a " , " b " , " c " , " .. " ) . pathString, " hello/a/b " )
241
- XCTAssertEqual ( RelativePath ( " hello " ) . appending ( RelativePath ( " a/b/../c/d " ) ) . pathString, " hello/a/c/d " )
214
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " " ) ) . pathString, windows ? #"\"# : " / " )
215
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " . " ) ) . pathString, windows ? #"\"# : " / " )
216
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " .. " ) ) . pathString, windows ? #"\"# : " / " )
217
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " / " ) , RelativePath ( " bar " ) ) . pathString, windows ? #"\bar"# : " /bar " )
218
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /foo/bar " ) , RelativePath ( " .. " ) ) . pathString, windows ? #"\foo"# : " /foo " )
219
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar " ) , RelativePath ( " ../foo " ) ) . pathString, windows ? #"\foo"# : " /foo " )
220
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar " ) , RelativePath ( " ../foo/..// " ) ) . pathString, windows ? #"\"# : " / " )
221
+ XCTAssertEqual ( AbsolutePath ( AbsolutePath ( " /bar/../foo/..//yabba/ " ) , RelativePath ( " a/b " ) ) . pathString, windows ? #"\yabba\a\b"# : " /yabba/a/b " )
222
+
223
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " " ) ) . pathString, windows ? #"\"# : " / " )
224
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " . " ) ) . pathString, windows ? #"\"# : " / " )
225
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " .. " ) ) . pathString, windows ? #"\"# : " / " )
226
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( RelativePath ( " bar " ) ) . pathString, windows ? #"\bar"# : " /bar " )
227
+ XCTAssertEqual ( AbsolutePath ( " /foo/bar " ) . appending ( RelativePath ( " .. " ) ) . pathString, windows ? #"\foo"# : " /foo " )
228
+ XCTAssertEqual ( AbsolutePath ( " /bar " ) . appending ( RelativePath ( " ../foo " ) ) . pathString, windows ? #"\foo"# : " /foo " )
229
+ XCTAssertEqual ( AbsolutePath ( " /bar " ) . appending ( RelativePath ( " ../foo/..// " ) ) . pathString, windows ? #"\"# : " / " )
230
+ XCTAssertEqual ( AbsolutePath ( " /bar/../foo/..//yabba/ " ) . appending ( RelativePath ( " a/b " ) ) . pathString, windows ? #"\yabba\a\b"# : " /yabba/a/b " )
231
+
232
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( component: " a " ) . pathString, windows ? #"\a"# : " /a " )
233
+ XCTAssertEqual ( AbsolutePath ( " /a " ) . appending ( component: " b " ) . pathString, windows ? #"\a\b"# : " /a/b " )
234
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " a " , " b " ) . pathString, windows ? #"\a\b"# : " /a/b " )
235
+ XCTAssertEqual ( AbsolutePath ( " /a " ) . appending ( components: " b " , " c " ) . pathString, windows ? #"\a\b\c"# : " /a/b/c " )
236
+
237
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " " , " c " ) . pathString, windows ? #"\a\b\c\c"# : " /a/b/c/c " )
238
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " " ) . pathString, windows ? #"\a\b\c"# : " /a/b/c " )
239
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " . " ) . pathString, windows ? #"\a\b\c"# : " /a/b/c " )
240
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " .. " ) . pathString, windows ? #"\a\b"# : " /a/b " )
241
+ XCTAssertEqual ( AbsolutePath ( " /a/b/c " ) . appending ( components: " .. " , " d " ) . pathString, windows ? #"\a\b\d"# : " /a/b/d " )
242
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " .. " ) . pathString, windows ? #"\"# : " / " )
243
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " . " ) . pathString, windows ? #"\"# : " / " )
244
+ XCTAssertEqual ( AbsolutePath ( " / " ) . appending ( components: " .. " , " a " ) . pathString, windows ? #"\a"# : " /a " )
245
+
246
+ XCTAssertEqual ( RelativePath ( " hello " ) . appending ( components: " a " , " b " , " c " , " .. " ) . pathString, windows ? #"hello\a\b"# : " hello/a/b " )
247
+ XCTAssertEqual ( RelativePath ( " hello " ) . appending ( RelativePath ( " a/b/../c/d " ) ) . pathString, windows ? #"hello\a\c\d"# : " hello/a/c/d " )
242
248
}
243
249
244
250
func testPathComponents( ) {
@@ -360,8 +366,8 @@ class PathTests: XCTestCase {
360
366
let data = try JSONEncoder ( ) . encode ( foo)
361
367
let decodedFoo = try JSONDecoder ( ) . decode ( Foo . self, from: data)
362
368
XCTAssertEqual ( foo, decodedFoo)
363
- XCTAssertEqual ( foo. path. pathString, " /path/to/foo " )
364
- XCTAssertEqual ( decodedFoo. path. pathString, " /path/to/foo " )
369
+ XCTAssertEqual ( foo. path. pathString, windows ? #"\path\to\foo"# : " /path/to/foo " )
370
+ XCTAssertEqual ( decodedFoo. path. pathString, windows ? #"\path\to\foo"# : " /path/to/foo " )
365
371
}
366
372
367
373
do {
0 commit comments