Skip to content

Add tests for substring / anchor interaction #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,44 @@ extension RegexTests {
XCTAssertEqual(allRanges.count, 5)
}

func testSubstringAnchors() throws {
let string = "123abc456def789"
let trimmed = string.dropFirst(3).dropLast(3) // "abc456def"
let prefixLetters = try Regex(#"^[a-z]+"#, as: Substring.self)
let postfixLetters = try Regex(#"[a-z]+$"#, as: Substring.self)

// start anchor (^) should match beginning of substring
XCTExpectFailure {
XCTAssertEqual(trimmed.firstMatch(of: prefixLetters)?.output, "abc")
}
XCTExpectFailure {
XCTAssertEqual(trimmed.replacing(prefixLetters, with: ""), "456def")
}

// end anchor ($) should match end of substring
XCTExpectFailure {
XCTAssertEqual(trimmed.firstMatch(of: postfixLetters)?.output, "def")
}
XCTExpectFailure {
XCTAssertEqual(trimmed.replacing(postfixLetters, with: ""), "abc456")
}

// start anchor (^) should _not_ match beginning of subrange
XCTAssertEqual(
string.replacing(
prefixLetters,
with: "",
subrange: trimmed.startIndex..<trimmed.endIndex),
string)
// end anchor ($) should _not_ match beginning of subrange
XCTAssertEqual(
string.replacing(
postfixLetters,
with: "",
subrange: trimmed.startIndex..<trimmed.endIndex),
string)
}

func testMatchingOptionsScope() {
// `.` only matches newlines when the 's' option (single-line mode)
// is turned on. Standalone option-setting groups (e.g. `(?s)`) are
Expand Down