@@ -22,7 +22,7 @@ class TestNSString : XCTestCase {
22
22
var allTests : [ ( String , ( ) -> ( ) ) ] {
23
23
return [
24
24
( " test_BridgeConstruction " , test_BridgeConstruction ) ,
25
- ( " test_isEqualToStringWithStringLiteral " , test_isEqualToStringWithStringLiteral ) ,
25
+ ( " test_isEqualToStringWithSwiftString " , test_isEqualToStringWithSwiftString ) ,
26
26
( " test_FromASCIIData " , test_FromASCIIData ) ,
27
27
( " test_FromUTF8Data " , test_FromUTF8Data ) ,
28
28
( " test_FromMalformedUTF8Data " , test_FromMalformedUTF8Data ) ,
@@ -53,70 +53,77 @@ class TestNSString : XCTestCase {
53
53
XCTAssertEqual ( cluster. length, 3 )
54
54
}
55
55
56
- func test_isEqualToStringWithStringLiteral ( ) {
56
+ func test_isEqualToStringWithSwiftString ( ) {
57
57
let string : NSString = " literal "
58
- XCTAssertTrue ( string. isEqualToString ( " literal " ) )
58
+ let swiftString = " literal "
59
+ XCTAssertTrue ( string. isEqualToString ( swiftString) )
59
60
}
60
61
62
+ internal let mockASCIIStringBytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 ]
63
+ internal let mockASCIIString = " Hello Swift! "
64
+ internal let mockUTF8StringBytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ]
65
+ internal let mockUTF8String = " I ❤️ Swift "
66
+ internal let mockMalformedUTF8StringBytes : [ UInt8 ] = [ 0xFF ]
67
+
61
68
func test_FromASCIIData( ) {
62
- let bytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 ] // "Hello Swift!"
69
+ let bytes = mockASCIIStringBytes
63
70
let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSASCIIStringEncoding)
64
71
XCTAssertNotNil ( string)
65
- XCTAssertTrue ( string! . isEqualToString ( " Hello Swift! " ) )
72
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString ) ?? false )
66
73
}
67
74
68
75
func test_FromUTF8Data( ) {
69
- let bytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ] // "I ❤️ Swift"
76
+ let bytes = mockUTF8StringBytes
70
77
let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSUTF8StringEncoding)
71
78
XCTAssertNotNil ( string)
72
- XCTAssertTrue ( string? . isEqualToString ( " I ❤️ Swift " ) ?? false )
79
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String ) ?? false )
73
80
}
74
81
75
82
func test_FromMalformedUTF8Data( ) {
76
- let bytes : [ UInt8 ] = [ 0xFF ]
83
+ let bytes = mockMalformedUTF8StringBytes
77
84
let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSUTF8StringEncoding)
78
85
XCTAssertNil ( string)
79
86
}
80
87
81
88
func test_FromASCIINSData( ) {
82
- let bytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 ] // "Hello Swift!"
89
+ let bytes = mockASCIIStringBytes
83
90
let data = NSData ( bytes: bytes, length: bytes. count)
84
91
let string = NSString ( data: data, encoding: NSASCIIStringEncoding)
85
92
XCTAssertNotNil ( string)
86
- XCTAssertTrue ( string! . isEqualToString ( " Hello Swift! " ) )
93
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString ) ?? false )
87
94
}
88
95
89
96
func test_FromUTF8NSData( ) {
90
- let bytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ] // "I ❤️ Swift"
97
+ let bytes = mockUTF8StringBytes
91
98
let data = NSData ( bytes: bytes, length: bytes. count)
92
99
let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
93
100
XCTAssertNotNil ( string)
94
- XCTAssertTrue ( string? . isEqualToString ( " I ❤️ Swift " ) ?? false )
101
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String ) ?? false )
95
102
}
96
103
97
104
func test_FromMalformedUTF8NSData( ) {
98
- let bytes : [ UInt8 ] = [ 0xFF ]
105
+ let bytes = mockMalformedUTF8StringBytes
99
106
let data = NSData ( bytes: bytes, length: bytes. count)
100
107
let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
101
108
XCTAssertNil ( string)
102
109
}
103
110
104
111
func test_FromNullTerminatedCStringInASCII( ) {
105
- let bytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 , 0x00 ] // "Hello Swift!"
112
+ let bytes = mockASCIIStringBytes + [ 0x00 ]
106
113
let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSASCIIStringEncoding)
107
114
XCTAssertNotNil ( string)
108
- XCTAssertTrue ( string! . isEqualToString ( " Hello Swift! " ) )
115
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString ) ?? false )
109
116
}
110
117
111
118
func test_FromNullTerminatedCStringInUTF8( ) {
112
- let bytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ] // "I ❤️ Swift"
119
+ let bytes = mockUTF8StringBytes + [ 0x00 ]
113
120
let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSUTF8StringEncoding)
114
121
XCTAssertNotNil ( string)
115
- XCTAssertTrue ( string? . isEqualToString ( " I ❤️ Swift " ) ?? false )
122
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String ) ?? false )
116
123
}
117
124
118
125
func test_FromMalformedNullTerminatedCStringInUTF8( ) {
119
- let bytes : [ UInt8 ] = [ 0xFF , 0x00 ]
126
+ let bytes = mockMalformedUTF8StringBytes + [ 0x00 ]
120
127
let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSUTF8StringEncoding)
121
128
XCTAssertNil ( string)
122
129
}
0 commit comments