Skip to content

Commit a51eb88

Browse files
committed
[gardening] Remove some optional usage from the tests.
In some cases in the tests, even if an API returns an optional, we know that it should never be optional, and it might create warnings in later code when the optional is interpolated into a string (for example). Change those cases into using `.unwrapped()` to get a non-optional type, and create a big error if a nil is actually received. This should remove a bunch of warnings while compiling the tests.
1 parent 2e6971e commit a51eb88

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

TestFoundation/TestNSString.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,34 +1180,34 @@ class TestNSString: LoopbackServerTest {
11801180
}
11811181
}
11821182

1183-
func test_ExternalRepresentation() {
1183+
func test_ExternalRepresentation() throws {
11841184
// Ensure NSString can be used to create an external data representation
11851185

11861186
do {
11871187
let string = NSString(string: "this is an external string that should be representable by data")
11881188

1189-
let UTF8Data = string.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false) as NSData?
1190-
let UTF8Length = UTF8Data?.length
1189+
let UTF8Data = try string.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false).unwrapped() as NSData
1190+
let UTF8Length = UTF8Data.length
11911191
XCTAssertEqual(UTF8Length, 63, "NSString should successfully produce an external UTF8 representation with a length of 63 but got \(UTF8Length) bytes")
11921192

1193-
let UTF16Data = string.data(using: String.Encoding.utf16.rawValue, allowLossyConversion: false) as NSData?
1194-
let UTF16Length = UTF16Data?.length
1193+
let UTF16Data = try string.data(using: String.Encoding.utf16.rawValue, allowLossyConversion: false).unwrapped() as NSData
1194+
let UTF16Length = UTF16Data.length
11951195
XCTAssertEqual(UTF16Length, 128, "NSString should successfully produce an external UTF16 representation with a length of 128 but got \(UTF16Length) bytes")
11961196

1197-
let ISOLatin1Data = string.data(using: String.Encoding.isoLatin1.rawValue, allowLossyConversion: false) as NSData?
1198-
let ISOLatin1Length = ISOLatin1Data?.length
1197+
let ISOLatin1Data = try string.data(using: String.Encoding.isoLatin1.rawValue, allowLossyConversion: false).unwrapped() as NSData
1198+
let ISOLatin1Length = ISOLatin1Data.length
11991199
XCTAssertEqual(ISOLatin1Length, 63, "NSString should successfully produce an external ISOLatin1 representation with a length of 63 but got \(ISOLatin1Length) bytes")
12001200
}
12011201

12021202
do {
12031203
let string = NSString(string: "🐢 encoding all the way down. 🐢🐢🐢")
12041204

1205-
let UTF8Data = string.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false) as NSData?
1206-
let UTF8Length = UTF8Data?.length
1205+
let UTF8Data = try string.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false).unwrapped() as NSData
1206+
let UTF8Length = UTF8Data.length
12071207
XCTAssertEqual(UTF8Length, 44, "NSString should successfully produce an external UTF8 representation with a length of 44 but got \(UTF8Length) bytes")
12081208

1209-
let UTF16Data = string.data(using: String.Encoding.utf16.rawValue, allowLossyConversion: false) as NSData?
1210-
let UTF16Length = UTF16Data?.length
1209+
let UTF16Data = try string.data(using: String.Encoding.utf16.rawValue, allowLossyConversion: false).unwrapped() as NSData
1210+
let UTF16Length = UTF16Data.length
12111211
XCTAssertEqual(UTF16Length, 74, "NSString should successfully produce an external UTF16 representation with a length of 74 but got \(UTF16Length) bytes")
12121212

12131213
let ISOLatin1Data = string.data(using: String.Encoding.isoLatin1.rawValue, allowLossyConversion: false) as NSData?

TestFoundation/TestURL.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,9 @@ class TestURLComponents : XCTestCase {
670670
XCTAssertEqual(receivedString, expectedString, "expected \(expectedString) but received \(receivedString as Optional)")
671671
}
672672

673-
func test_url() {
673+
func test_url() throws {
674674

675-
let baseURL = URL(string: "https://www.example.com")
675+
let baseURL = try URL(string: "https://www.example.com").unwrapped()
676676

677677
/* test NSURLComponents without authority */
678678
guard var compWithAuthority = URLComponents(string: "https://www.swift.org") else {

0 commit comments

Comments
 (0)