Skip to content

Commit 335a0c2

Browse files
authored
Add tests for line start/end word boundary diffs (#616)
The `default` and `simple` word boundaries have different behaviors at the start and end of strings/lines. These tests validate that we have the correct behavior implemented. Related to issue #613.
1 parent 0b38ca9 commit 335a0c2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Tests/RegexTests/MatchTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,19 @@ extension RegexTests {
15171517
(" 123", "23"),
15181518
("123 456", "23"))
15191519

1520+
let defaultBoundaryRegex = try Regex(#"\b.{3}X.{3}\b"#)
1521+
// Default word boundaries match at the start/end of a string/line.
1522+
XCTAssertNotNil(try defaultBoundaryRegex.firstMatch(in: "---X---"))
1523+
XCTAssertNotNil(try defaultBoundaryRegex.firstMatch(in: "abc\n---X---\ndef"))
1524+
1525+
let simpleBoundaryRegex = defaultBoundaryRegex.wordBoundaryKind(.simple)
1526+
// Simple word boundaries match only when the adjacent position matches \w.
1527+
XCTAssertNil(try simpleBoundaryRegex.firstMatch(in: "---X---"))
1528+
XCTAssertNil(try simpleBoundaryRegex.firstMatch(in: "abc\n---X---\ndef"))
1529+
1530+
XCTAssertNotNil(try simpleBoundaryRegex.firstMatch(in: "x--X--x"))
1531+
XCTAssertNotNil(try simpleBoundaryRegex.firstMatch(in: "abc\nx--X--x\ndef"))
1532+
15201533
// \G and \K
15211534
let regex = try Regex(#"\Gab"#, as: Substring.self)
15221535
XCTAssertEqual("abab".matches(of: regex).map(\.output), ["ab", "ab"])

0 commit comments

Comments
 (0)