@@ -93,6 +93,7 @@ class TestNSString : XCTestCase {
9393 ( " test_PrefixSuffix " , test_PrefixSuffix) ,
9494 ( " test_utf16StringRangeCount " , test_StringUTF16ViewIndexStrideableRange) ,
9595 ( " test_reflection " , { _ in test_reflection } ) ,
96+ ( " test_replacingOccurrences " , test_replacingOccurrences) ,
9697 ]
9798 }
9899
@@ -1229,3 +1230,42 @@ extension TestNSString {
12291230
12301231func test_reflection( ) {
12311232}
1233+
1234+ extension TestNSString {
1235+ func test_replacingOccurrences( ) {
1236+ let testPrefix = " ab "
1237+ let testSuffix = " cd "
1238+ let testEmoji = " \u{1F468} \u{200D} \u{1F469} \u{200D} \u{1F467} \u{200D} \u{1F466} "
1239+ let testString = testPrefix + testEmoji + testSuffix
1240+
1241+ let testReplacement = " xyz "
1242+ let testReplacementEmoji = " \u{01F468} \u{200D} \u{002764} \u{00FE0F} \u{200D} \u{01F48B} \u{200D} \u{01F468} "
1243+
1244+ let noChange = testString. replacingOccurrences ( of: testReplacement, with: " " )
1245+ XCTAssertEqual ( noChange, testString)
1246+
1247+ let removePrefix = testString. replacingOccurrences ( of: testPrefix, with: " " )
1248+ XCTAssertEqual ( removePrefix, testEmoji + testSuffix)
1249+ let replacePrefix = testString. replacingOccurrences ( of: testPrefix, with: testReplacement)
1250+ XCTAssertEqual ( replacePrefix, testReplacement + testEmoji + testSuffix)
1251+
1252+ let removeSuffix = testString. replacingOccurrences ( of: testSuffix, with: " " )
1253+ XCTAssertEqual ( removeSuffix, testPrefix + testEmoji)
1254+ let replaceSuffix = testString. replacingOccurrences ( of: testSuffix, with: testReplacement)
1255+ XCTAssertEqual ( replaceSuffix, testPrefix + testEmoji + testReplacement)
1256+
1257+ let removeMultibyte = testString. replacingOccurrences ( of: testEmoji, with: " " )
1258+ XCTAssertEqual ( removeMultibyte, testPrefix + testSuffix)
1259+ let replaceMultibyte = testString. replacingOccurrences ( of: testEmoji, with: testReplacement)
1260+ XCTAssertEqual ( replaceMultibyte, testPrefix + testReplacement + testSuffix)
1261+
1262+ let replaceMultibyteWithMultibyte = testString. replacingOccurrences ( of: testEmoji, with: testReplacementEmoji)
1263+ XCTAssertEqual ( replaceMultibyteWithMultibyte, testPrefix + testReplacementEmoji + testSuffix)
1264+
1265+ let replacePrefixWithMultibyte = testString. replacingOccurrences ( of: testPrefix, with: testReplacementEmoji)
1266+ XCTAssertEqual ( replacePrefixWithMultibyte, testReplacementEmoji + testEmoji + testSuffix)
1267+
1268+ let replaceSuffixWithMultibyte = testString. replacingOccurrences ( of: testSuffix, with: testReplacementEmoji)
1269+ XCTAssertEqual ( replaceSuffixWithMultibyte, testPrefix + testEmoji + testReplacementEmoji)
1270+ }
1271+ }
0 commit comments