Skip to content

Commit 8d4ddce

Browse files
committed
Unit Tests cleanup
1 parent 7199509 commit 8d4ddce

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

TestFoundation/TestNSString.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestNSString : XCTestCase {
2222
var allTests : [(String, () -> ())] {
2323
return [
2424
("test_BridgeConstruction", test_BridgeConstruction ),
25-
("test_isEqualToStringWithStringLiteral", test_isEqualToStringWithStringLiteral ),
25+
("test_isEqualToStringWithSwiftString", test_isEqualToStringWithSwiftString ),
2626
("test_FromASCIIData", test_FromASCIIData ),
2727
("test_FromUTF8Data", test_FromUTF8Data ),
2828
("test_FromMalformedUTF8Data", test_FromMalformedUTF8Data ),
@@ -53,70 +53,77 @@ class TestNSString : XCTestCase {
5353
XCTAssertEqual(cluster.length, 3)
5454
}
5555

56-
func test_isEqualToStringWithStringLiteral() {
56+
func test_isEqualToStringWithSwiftString() {
5757
let string: NSString = "literal"
58-
XCTAssertTrue(string.isEqualToString("literal"))
58+
let swiftString = "literal"
59+
XCTAssertTrue(string.isEqualToString(swiftString))
5960
}
6061

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+
6168
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
6370
let string = NSString(bytes: bytes, length: bytes.count, encoding: NSASCIIStringEncoding)
6471
XCTAssertNotNil(string)
65-
XCTAssertTrue(string!.isEqualToString("Hello Swift!"))
72+
XCTAssertTrue(string?.isEqualToString(mockASCIIString) ?? false)
6673
}
6774

6875
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
7077
let string = NSString(bytes: bytes, length: bytes.count, encoding: NSUTF8StringEncoding)
7178
XCTAssertNotNil(string)
72-
XCTAssertTrue(string?.isEqualToString("I ❤️ Swift") ?? false)
79+
XCTAssertTrue(string?.isEqualToString(mockUTF8String) ?? false)
7380
}
7481

7582
func test_FromMalformedUTF8Data() {
76-
let bytes: [UInt8] = [0xFF]
83+
let bytes = mockMalformedUTF8StringBytes
7784
let string = NSString(bytes: bytes, length: bytes.count, encoding: NSUTF8StringEncoding)
7885
XCTAssertNil(string)
7986
}
8087

8188
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
8390
let data = NSData(bytes: bytes, length: bytes.count)
8491
let string = NSString(data: data, encoding: NSASCIIStringEncoding)
8592
XCTAssertNotNil(string)
86-
XCTAssertTrue(string!.isEqualToString("Hello Swift!"))
93+
XCTAssertTrue(string?.isEqualToString(mockASCIIString) ?? false)
8794
}
8895

8996
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
9198
let data = NSData(bytes: bytes, length: bytes.count)
9299
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
93100
XCTAssertNotNil(string)
94-
XCTAssertTrue(string?.isEqualToString("I ❤️ Swift") ?? false)
101+
XCTAssertTrue(string?.isEqualToString(mockUTF8String) ?? false)
95102
}
96103

97104
func test_FromMalformedUTF8NSData() {
98-
let bytes: [UInt8] = [0xFF]
105+
let bytes = mockMalformedUTF8StringBytes
99106
let data = NSData(bytes: bytes, length: bytes.count)
100107
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
101108
XCTAssertNil(string)
102109
}
103110

104111
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]
106113
let string = NSString(CString: bytes.map { Int8(bitPattern: $0) }, encoding: NSASCIIStringEncoding)
107114
XCTAssertNotNil(string)
108-
XCTAssertTrue(string!.isEqualToString("Hello Swift!"))
115+
XCTAssertTrue(string?.isEqualToString(mockASCIIString) ?? false)
109116
}
110117

111118
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]
113120
let string = NSString(CString: bytes.map { Int8(bitPattern: $0) }, encoding: NSUTF8StringEncoding)
114121
XCTAssertNotNil(string)
115-
XCTAssertTrue(string?.isEqualToString("I ❤️ Swift") ?? false)
122+
XCTAssertTrue(string?.isEqualToString(mockUTF8String) ?? false)
116123
}
117124

118125
func test_FromMalformedNullTerminatedCStringInUTF8() {
119-
let bytes: [UInt8] = [0xFF, 0x00]
126+
let bytes = mockMalformedUTF8StringBytes + [0x00]
120127
let string = NSString(CString: bytes.map { Int8(bitPattern: $0) }, encoding: NSUTF8StringEncoding)
121128
XCTAssertNil(string)
122129
}

0 commit comments

Comments
 (0)