Skip to content

Commit 83d1a75

Browse files
committed
Test RegexMatchesCollection conformances
1 parent 30c540a commit 83d1a75

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Tests/RegexTests/AlgorithmsInternalsTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,39 @@ extension AlgorithmTests {
4444
XCTAssertEqual("x", "axb"._trimming(r))
4545
XCTAssertEqual("x", "axbb"._trimming(r))
4646
}
47+
48+
func testMatchesCollection() {
49+
let r = try! Regex("a|b+|c*", as: Substring.self)
50+
51+
let str = "zaabbbbbbcde"
52+
let matches = str._matches(of: r)
53+
let expected: [Substring] = [
54+
"", // before 'z'
55+
"a",
56+
"a",
57+
"bbbbbb",
58+
"c",
59+
"", // after 'c'
60+
"", // after 'd'
61+
"", // after 'e'
62+
]
63+
64+
// Make sure we're getting the right collection type
65+
let _: RegexMatchesCollection<Substring> = matches
66+
67+
XCTAssertEqual(matches.map(\.output), expected)
68+
69+
let i = matches.index(matches.startIndex, offsetBy: 3)
70+
XCTAssertEqual(matches[i].output, expected[3])
71+
let j = matches.index(i, offsetBy: 5)
72+
XCTAssertEqual(j, matches.endIndex)
73+
74+
var index = matches.startIndex
75+
while index < matches.endIndex {
76+
XCTAssertEqual(
77+
matches[index].output,
78+
expected[matches.distance(from: matches.startIndex, to: index)])
79+
matches.formIndex(after: &index)
80+
}
81+
}
4782
}

0 commit comments

Comments
 (0)