Skip to content
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

Test all string extension branches that were missing #5

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion Sources/Evergreen/Evergreen+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ extension String {
/// - Returns: The new string with the first match, if applicable, replaced

func replaceFirst(matching: NSRegularExpression, with template: String, in range: NSRange? = nil, options: NSRegularExpression.MatchingOptions = []) -> String {
let matchedRange = range ?? fullRange()
var matchedRange: NSRange!

if let r = range {
matchedRange = r
} else {
guard let match = matching.firstMatch(in: self, options: [], range: fullRange()) else {
return self
}
matchedRange = match.range
}

return matching.stringByReplacingMatches(in: self, options: options, range: matchedRange, withTemplate: template)
}
Expand Down
80 changes: 80 additions & 0 deletions Tests/EvergreenTests/StringExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Foundation
import XCTest
@testable import Evergreen

final class StringExtensionTests: XCTestCase {
func testReplaceAllMethod() {
let str = "replace replace none replace"
let match = try! NSRegularExpression(pattern: "replace", options: [])

let replaced = str.replaceAll(matching: match, with: "_")

XCTAssertEqual(replaced, "_ _ none _")
}

func testReplaceFirstMethod() {
let str = "replace replace none replace"
let match = try! NSRegularExpression(pattern: "replace", options: [])

let replaced = str.replaceFirst(matching: match, with: "_")

XCTAssertEqual(replaced, "_ replace none replace")
}

func testReplaceFirstMethodWithMissingRegex() {
let str = "replace replace none replace"
let match = try! NSRegularExpression(pattern: "missing", options: [])

let replaced = str.replaceFirst(matching: match, with: "_")

XCTAssertEqual(replaced, "replace replace none replace")
}

func testReplaceFirstWithPassedRange() {
let str = "replace replace none replace"
let match = try! NSRegularExpression(pattern: "replace", options: [])

let range = match.firstMatch(in: str, options: [], range: str.fullRange())?.range

let replaced = str.replaceFirst(matching: match, with: "_", in: range)

XCTAssertEqual(replaced, "_ replace none replace")
}

func testStringFromMatchWithPassedRange() {
let str = "replace replace none replace"

let match = try! NSRegularExpression(pattern: "replace", options: [])

let range = match.firstMatch(in: str, options: [], range: str.fullRange())?.range

let badMatch = try! NSRegularExpression(pattern: "none", options: [])

let result = str.stringFromMatch(badMatch, in: range)

XCTAssertEqual(result, "")
}

func testReplaceRangeWithPassedRange() {
var str = "replace replace none replace"

let match = try! NSRegularExpression(pattern: "replace", options: [])

let range = match.firstMatch(in: str, options: [], range: str.fullRange())?.range

let badMatch = try! NSRegularExpression(pattern: "none", options: [])

str.replaceRange(matching: badMatch, with: "nothing", options: [], range: range)

XCTAssertEqual(str, "replace replace none replace")
}

static var allTests = [
"testReplaceAll",
"testReplaceFirstMethod",
"testReplaceFirstMethodWithMissingRegex",
"testReplaceFirstWithPassedRange",
"testStringFromMatchWithPassedRange",
"testReplaceRangeWithPassedRange"
]
}