Skip to content

Commit 63911c4

Browse files
authored
Add test for backreference before group (swiftlang#172)
A literal can specify a backreference that references a group before the group is opened. This most frequently shows up with a repeated alternation - the first part of an alternation can include a reference to a group in a different alternation, and simply doesn't match anything until the group has matched and captured a portion of the input.
1 parent 3f2468f commit 63911c4

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Tests/RegexTests/MatchTests.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func flatCaptureTest(
6161
syntax: SyntaxOptions = .traditional,
6262
enableTracing: Bool = false,
6363
dumpAST: Bool = false,
64-
xfail: Bool = false
64+
xfail: Bool = false,
65+
file: StaticString = #file,
66+
line: UInt = #line
6567
) {
6668
for (test, expect) in tests {
6769
do {
@@ -78,7 +80,7 @@ func flatCaptureTest(
7880
}
7981
}
8082
guard let expect = expect else {
81-
throw "Match succeeded where failure expected"
83+
throw "Match of \(test) succeeded where failure expected in \(regex)"
8284
}
8385
let capStrs = caps.map { $0 == nil ? nil : String($0!) }
8486
guard expect.count == capStrs.count else {
@@ -98,7 +100,7 @@ func flatCaptureTest(
98100
}
99101
} catch {
100102
if !xfail {
101-
XCTFail("\(error)")
103+
XCTFail("\(error)", file: file, line: line)
102104
}
103105
}
104106
}
@@ -1011,6 +1013,13 @@ extension RegexTests {
10111013
("abbbe", nil),
10121014
("accce", nil),
10131015
("abcbbd", nil))
1016+
flatCaptureTest(
1017+
#"(?:\w\1|:(\w):)+"#,
1018+
(":a:bacada", ["a"]),
1019+
(":a:baca:o:boco", ["o"]),
1020+
("bacada", nil),
1021+
(":a:boco", ["a"]) // this matches only the ':a:' prefix
1022+
)
10141023
}
10151024

10161025
func testMatchReferences() {

0 commit comments

Comments
 (0)