@@ -95,6 +95,7 @@ class TestNSString : XCTestCase {
95
95
( " test_reflection " , { _ in test_reflection } ) ,
96
96
( " test_replacingOccurrences " , test_replacingOccurrences) ,
97
97
( " test_getLineStart " , test_getLineStart) ,
98
+ ( " test_substringWithRange " , test_substringWithRange) ,
98
99
]
99
100
}
100
101
@@ -1056,6 +1057,49 @@ class TestNSString : XCTestCase {
1056
1057
XCTAssertEqual ( outContentsEndIndex, twoLines. index ( twoLines. startIndex, offsetBy: 11 ) )
1057
1058
XCTAssertEqual ( outEndIndex, twoLines. index ( twoLines. startIndex, offsetBy: 12 ) )
1058
1059
}
1060
+
1061
+ func test_emptyStringPrefixAndSuffix( ) {
1062
+ let testString = " hello "
1063
+ XCTAssertTrue ( testString. hasPrefix ( " " ) )
1064
+ XCTAssertTrue ( testString. hasSuffix ( " " ) )
1065
+ }
1066
+
1067
+ func test_substringWithRange( ) {
1068
+ let trivial = NSString ( string: " swift.org " )
1069
+ XCTAssertEqual ( trivial. substring ( with: NSMakeRange ( 0 , 5 ) ) , " swift " )
1070
+
1071
+ let surrogatePairSuffix = NSString ( string: " Hurray🎉 " )
1072
+ XCTAssertEqual ( surrogatePairSuffix. substring ( with: NSMakeRange ( 0 , 7 ) ) , " Hurray� " )
1073
+
1074
+ let surrogatePairPrefix = NSString ( string: " 🐱Cat " )
1075
+ XCTAssertEqual ( surrogatePairPrefix. substring ( with: NSMakeRange ( 1 , 4 ) ) , " �Cat " )
1076
+
1077
+ let singleChar = NSString ( string: " 😹 " )
1078
+ XCTAssertEqual ( singleChar. substring ( with: NSMakeRange ( 0 , 1 ) ) , " � " )
1079
+
1080
+ let crlf = NSString ( string: " \r \n " )
1081
+ XCTAssertEqual ( crlf. substring ( with: NSMakeRange ( 0 , 1 ) ) , " \r " )
1082
+ XCTAssertEqual ( crlf. substring ( with: NSMakeRange ( 1 , 1 ) ) , " \n " )
1083
+ XCTAssertEqual ( crlf. substring ( with: NSMakeRange ( 1 , 0 ) ) , " " )
1084
+
1085
+ let bothEnds1 = NSString ( string: " 😺😺 " )
1086
+ XCTAssertEqual ( bothEnds1. substring ( with: NSMakeRange ( 1 , 2 ) ) , " �� " )
1087
+
1088
+ let s1 = NSString ( string: " 😺 \r \n " )
1089
+ XCTAssertEqual ( s1. substring ( with: NSMakeRange ( 1 , 2 ) ) , " � \r " )
1090
+
1091
+ let s2 = NSString ( string: " \r \n 😺 " )
1092
+ XCTAssertEqual ( s2. substring ( with: NSMakeRange ( 1 , 2 ) ) , " \n � " )
1093
+
1094
+ let s3 = NSString ( string: " 😺cats😺 " )
1095
+ XCTAssertEqual ( s3. substring ( with: NSMakeRange ( 1 , 6 ) ) , " �cats� " )
1096
+
1097
+ let s4 = NSString ( string: " 😺cats \r \n " )
1098
+ XCTAssertEqual ( s4. substring ( with: NSMakeRange ( 1 , 6 ) ) , " �cats \r " )
1099
+
1100
+ let s5 = NSString ( string: " \r \n cats😺 " )
1101
+ XCTAssertEqual ( s5. substring ( with: NSMakeRange ( 1 , 6 ) ) , " \n cats� " )
1102
+ }
1059
1103
}
1060
1104
1061
1105
struct ComparisonTest {
0 commit comments