Skip to content

Commit 52fe2af

Browse files
natecook1000hamishknight
authored andcommitted
Add tests for backreferences in lookaheads
1 parent a801234 commit 52fe2af

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Tests/RegexTests/MatchTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,37 @@ extension RegexTests {
11131113
(input: "23x23", match: "23x23"),
11141114
(input: "123x23", match: "23x23"),
11151115
xfail: true)
1116+
1117+
// Backreferences in lookaheads
1118+
firstMatchTests(
1119+
#"^(?=.*(.)(.)\2\1).+$"#,
1120+
(input: "abbba", match: nil),
1121+
(input: "ABBA", match: "ABBA"),
1122+
(input: "defABBAdef", match: "defABBAdef"))
1123+
firstMatchTests(
1124+
#"^(?=.*(.)(.)\2\1).+\2$"#,
1125+
(input: "abbba", match: nil),
1126+
(input: "ABBA", match: nil),
1127+
(input: "defABBAdef", match: nil))
1128+
// FIXME: Backreferences don't escape positive lookaheads
1129+
firstMatchTests(
1130+
#"^(?=.*(.)(.)\2\1).+\2$"#,
1131+
(input: "ABBAB", match: "ABBAB"),
1132+
(input: "defABBAdefB", match: "defABBAdefB"),
1133+
xfail: true)
1134+
1135+
firstMatchTests(
1136+
#"^(?!.*(.)(.)\2\1).+$"#,
1137+
(input: "abbba", match: "abbba"),
1138+
(input: "ABBA", match: nil),
1139+
(input: "defABBAdef", match: nil))
1140+
// Backreferences don't escape negative lookaheads;
1141+
// matching only proceeds when the lookahead fails
1142+
firstMatchTests(
1143+
#"^(?!.*(.)(.)\2\1).+\2$"#,
1144+
(input: "abbba", match: nil),
1145+
(input: "abbbab", match: nil),
1146+
(input: "ABBAB", match: nil))
11161147

11171148
// TODO: Test example where non-atomic is significant
11181149
firstMatchTest(

0 commit comments

Comments
 (0)