mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
78836b9959
Add preferSwiftStringAPI rule (#2292) Replaces the Objective-C bridged `replacingOccurrences(of:with:)` with Swift-native `replacing(_:with:)`. The ObjC method has known Unicode bugs (e.g. corrupting emoji flag sequences); the Swift equivalent is safer, shorter, and available from Swift 5.7+. Only the two-argument form is transformed — calls with `options:` or `range:` are left unchanged as they have no direct Swift equivalent.
103 lines
2.9 KiB
Swift
103 lines
2.9 KiB
Swift
//
|
|
// PreferSwiftStringAPITests.swift
|
|
// SwiftFormatTests
|
|
//
|
|
// Created by Sutheesh Sukumaran on 05/04/2026.
|
|
// Copyright © 2026 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import XCTest
|
|
@testable import SwiftFormat
|
|
|
|
final class PreferSwiftStringAPITests: XCTestCase {
|
|
func testReplacingOccurrences() {
|
|
let input = """
|
|
str.replacingOccurrences(of: "foo", with: "bar")
|
|
"""
|
|
|
|
let output = """
|
|
str.replacing("foo", with: "bar")
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, output, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesOnOptionalChain() {
|
|
let input = """
|
|
str?.replacingOccurrences(of: "foo", with: "bar")
|
|
"""
|
|
|
|
let output = """
|
|
str?.replacing("foo", with: "bar")
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, output, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesMultiline() {
|
|
let input = """
|
|
str.replacingOccurrences(
|
|
of: "foo",
|
|
with: "bar"
|
|
)
|
|
"""
|
|
|
|
let output = """
|
|
str.replacing(
|
|
"foo",
|
|
with: "bar"
|
|
)
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, output, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesNotTransformedBeforeSwift5_7() {
|
|
let input = """
|
|
str.replacingOccurrences(of: "foo", with: "bar")
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.6")
|
|
testFormatting(for: input, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesWithOptionsNotTransformed() {
|
|
let input = """
|
|
str.replacingOccurrences(of: "foo", with: "bar", options: [.caseInsensitive])
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesWithRangeNotTransformed() {
|
|
let input = """
|
|
str.replacingOccurrences(of: "foo", with: "bar", options: [], range: str.startIndex...)
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
|
|
func testReplacingOccurrencesNotTransformedWhenNoVersionSet() {
|
|
let input = """
|
|
str.replacingOccurrences(of: "foo", with: "bar")
|
|
"""
|
|
|
|
testFormatting(for: input, rule: .preferSwiftStringAPI)
|
|
}
|
|
|
|
func testFreestandingReplacingOccurrencesNotTransformed() {
|
|
let input = """
|
|
replacingOccurrences(of: "foo", with: "bar")
|
|
"""
|
|
|
|
let options = FormatOptions(swiftVersion: "5.7")
|
|
testFormatting(for: input, rule: .preferSwiftStringAPI, options: options)
|
|
}
|
|
}
|