@@ -13,104 +13,6 @@ import SystemPackage
1313@testable
1414import SystemPackage
1515
16-
17- // Helper to organize some ad-hoc testing
18- //
19- // TODO: Currently a class so we can overwrite file/line, but that can be
20- // re-evaluated when we have source loc stacks.
21- private final class AdHocComponentsTest : TestCase {
22- // TODO (source-loc stack): Push fil/line from init onto stack
23-
24- // Record the top-most file/line info (`expect` overwrites these values)
25- //
26- // TODO: When we have source loc stacks, push the location from the init,
27- // and `expect` will be push/pops
28- var file : StaticString
29- var line : UInt
30-
31- var path : FilePath
32- var body : ( AdHocComponentsTest ) -> ( )
33-
34- init (
35- _ path: FilePath ,
36- _ file: StaticString = #file,
37- _ line: UInt = #line,
38- _ body: @escaping ( AdHocComponentsTest ) -> ( )
39- ) {
40- self . file = file
41- self . line = line
42- self . path = path
43- self . body = body
44- }
45-
46- func runAllTests( ) {
47- body ( self )
48- }
49- }
50-
51- private func adhocComponentsTest(
52- _ path: FilePath ,
53- _ file: StaticString = #file,
54- _ line: UInt = #line,
55- _ body: @escaping ( AdHocComponentsTest ) -> ( )
56- ) {
57- let test = AdHocComponentsTest ( path, file, line, body)
58- test. runAllTests ( )
59- }
60-
61- extension AdHocComponentsTest {
62- // Temporarily re-bind file/line
63- func withSourceLoc(
64- _ newFile: StaticString ,
65- _ newLine: UInt ,
66- _ body: ( ) -> ( )
67- ) {
68- let ( origFile, origLine) = ( self . file, self . line)
69- ( self . file, self . line) = ( newFile, newLine)
70- defer { ( self . file, self . line) = ( origFile, origLine) }
71- body ( )
72- }
73-
74- // Customize error report by adding our path and components to output
75- func failureMessage( _ reason: String ? ) -> String {
76- """
77-
78- Fail
79- path: \( path)
80- components: \( Array ( path. components) )
81- \( reason ?? " " )
82- """
83- }
84-
85- func expect(
86- _ expected: FilePath ,
87- file: StaticString = #file, line: UInt = #line
88- ) {
89-
90- withSourceLoc ( file, line) {
91- expectEqual ( expected, path, " expected: \( expected) " )
92- }
93- }
94-
95- func expectRelative(
96- file: StaticString = #file, line: UInt = #line
97- ) {
98- withSourceLoc ( file, line) {
99- expectTrue ( path. isRelative, " expected relative " )
100- }
101- }
102-
103- func expectAbsolute(
104- file: StaticString = #file, line: UInt = #line
105- ) {
106- withSourceLoc ( file, line) {
107- expectTrue ( path. isAbsolute, " expected absolute " )
108- }
109- }
110-
111- // TODO: Do we want to overload others like expectEqual[Sequence]?
112- }
113-
11416// @available(9999....)
11517struct TestPathComponents : TestCase {
11618 var path : FilePath
@@ -152,69 +54,54 @@ struct TestPathComponents: TestCase {
15254 }
15355
15456 func testBidi( ) {
155-
156-
57+ expectEqualSequence (
58+ expectedComponents. reversed ( ) , path. components. reversed ( ) , " reversed() " )
59+ expectEqualSequence (
60+ path. components, path. components. reversed ( ) . reversed ( ) ,
61+ " reversed().reversed() " )
62+ for i in 0 ..< path. components. count {
63+ expectEqualSequence (
64+ expectedComponents. dropLast ( i) , path. components. dropLast ( i) , " dropLast " )
65+ expectEqualSequence (
66+ expectedComponents. suffix ( i) , path. components. suffix ( i) , " suffix " )
67+ }
15768 }
15869
15970 func testRRC( ) {
160- // TODO: Convert tests into mutation tests
161- // // What generalized tests can we do, given how initial "/" is special?
162- // // E.g. absolute path inserted into itself can have only one root
163- //
164- // do {
165- // var path = self.path
166- // if path.isAbsolute {
167- // path.components.removeFirst()
168- // }
169- // expectTrue(path.isRelative)
170- //
171- // let componentsArray = Array(path.components)
172- // path.components.append(contentsOf: componentsArray)
173- // expectEqualSequence(componentsArray + componentsArray, path.components)
174- //
175- // // TODO: Other generalized tests which work on relative paths
176- // }
71+ // TODO: programmatic tests showing parity with Array<Component>
72+ }
73+
74+ func testModify( ) {
75+ if path. root == nil {
76+ let rootedPath = FilePath ( root: " / " , path. components)
77+ expectNotEqual ( rootedPath, path)
78+ var pathCopy = path
79+ expectEqual ( path, pathCopy)
80+ pathCopy. components = rootedPath. components
81+ expectNil ( pathCopy. root, " components.set doesn't assign root " )
82+ expectEqual ( path, pathCopy)
83+ } else {
84+ let rootlessPath = FilePath ( root: nil , path. components)
85+ var pathCopy = path
86+ expectEqual ( path, pathCopy)
87+ pathCopy. components = rootlessPath. components
88+ expectNotNil ( pathCopy. root, " components.set preserves root " )
89+ expectEqual ( path, pathCopy)
90+ }
17791 }
17892
17993 func runAllTests( ) {
18094 testComponents ( )
18195 testBidi ( )
18296 testRRC ( )
97+ testModify ( )
18398 }
18499}
185100
186101// TODO: Note that double-reversal will drop root if FilePath is constructed in between ...
187102
188103// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
189104final class FilePathComponentsTest : XCTestCase {
190-
191- let testPaths : Array < TestPathComponents > = [
192- TestPathComponents ( " " , root: nil , [ ] ) ,
193- TestPathComponents ( " / " , root: " / " , [ ] ) ,
194- TestPathComponents ( " foo " , root: nil , [ " foo " ] ) ,
195- TestPathComponents ( " foo/ " , root: nil , [ " foo " ] ) ,
196- TestPathComponents ( " /foo " , root: " / " , [ " foo " ] ) ,
197- TestPathComponents ( " foo/bar " , root: nil , [ " foo " , " bar " ] ) ,
198- TestPathComponents ( " foo/bar/ " , root: nil , [ " foo " , " bar " ] ) ,
199- TestPathComponents ( " /foo/bar " , root: " / " , [ " foo " , " bar " ] ) ,
200- TestPathComponents ( " ///foo// " , root: " / " , [ " foo " ] ) ,
201- TestPathComponents ( " /foo///bar " , root: " / " , [ " foo " , " bar " ] ) ,
202- TestPathComponents ( " foo/bar/ " , root: nil , [ " foo " , " bar " ] ) ,
203- TestPathComponents ( " foo///bar/baz/ " , root: nil , [ " foo " , " bar " , " baz " ] ) ,
204- TestPathComponents ( " //foo///bar/baz/ " , root: " / " , [ " foo " , " bar " , " baz " ] ) ,
205- TestPathComponents ( " ./ " , root: nil , [ " . " ] ) ,
206- TestPathComponents ( " ./.. " , root: nil , [ " . " , " .. " ] ) ,
207- TestPathComponents ( " /./..// " , root: " / " , [ " . " , " .. " ] ) ,
208- ]
209-
210- // TODO: generalize to a driver protocol that will inherit from XCTest, expose allTestCases
211- // based on an associated type, and provide the testCasees func, assuming XCTest supports
212- // that.
213- func testCases( ) {
214- testPaths. forEach { $0. runAllTests ( ) }
215- }
216-
217- // TODO: Convert these kinds of test cases into mutation API test cases.
218105 func testAdHocRRC( ) {
219106 var path : FilePath = " /usr/local/bin "
220107
@@ -343,21 +230,26 @@ final class FilePathComponentsTest: XCTestCase {
343230 }
344231 }
345232
346- func testConcatenation( ) {
347- // TODO: convert tests into mutation tests
348-
349- // for lhsTest in testPaths {
350- // let lhs = lhsTest.path
351- // for rhsTest in testPaths {
352- // let rhs = rhsTest.path
353- // adhocComponentsTest(lhs + rhs) { concatpath in
354- // // (lhs + rhs).components == (lhs.components + rhs.compontents)
355- // concatpath.expectEqualSequence(lhs.components + rhs.components, concatpath.components)
356- //
357- // // TODO: More tests around non-normalized separators
358- // }
359- // }
360- // }
233+ func testCases( ) {
234+ let testPaths : Array < TestPathComponents > = [
235+ TestPathComponents ( " " , root: nil , [ ] ) ,
236+ TestPathComponents ( " / " , root: " / " , [ ] ) ,
237+ TestPathComponents ( " foo " , root: nil , [ " foo " ] ) ,
238+ TestPathComponents ( " foo/ " , root: nil , [ " foo " ] ) ,
239+ TestPathComponents ( " /foo " , root: " / " , [ " foo " ] ) ,
240+ TestPathComponents ( " foo/bar " , root: nil , [ " foo " , " bar " ] ) ,
241+ TestPathComponents ( " foo/bar/ " , root: nil , [ " foo " , " bar " ] ) ,
242+ TestPathComponents ( " /foo/bar " , root: " / " , [ " foo " , " bar " ] ) ,
243+ TestPathComponents ( " ///foo// " , root: " / " , [ " foo " ] ) ,
244+ TestPathComponents ( " /foo///bar " , root: " / " , [ " foo " , " bar " ] ) ,
245+ TestPathComponents ( " foo/bar/ " , root: nil , [ " foo " , " bar " ] ) ,
246+ TestPathComponents ( " foo///bar/baz/ " , root: nil , [ " foo " , " bar " , " baz " ] ) ,
247+ TestPathComponents ( " //foo///bar/baz/ " , root: " / " , [ " foo " , " bar " , " baz " ] ) ,
248+ TestPathComponents ( " ./ " , root: nil , [ " . " ] ) ,
249+ TestPathComponents ( " ./.. " , root: nil , [ " . " , " .. " ] ) ,
250+ TestPathComponents ( " /./..// " , root: " / " , [ " . " , " .. " ] ) ,
251+ ]
252+ testPaths. forEach { $0. runAllTests ( ) }
361253 }
362254
363255 func testSeparatorNormalization( ) {
0 commit comments