Skip to content

Commit bc3bccd

Browse files
authored
[FirestoreSharedSwift] Write base64 decode test as throwing XCTest (#10638)
Simplified the error handling in testDecodingBase64StringAsBlobData by making it a throwing test. As a side-effect this will prevent the test from continuing if an earlier step throws (similar to `continueAfterFailure` set to `false`). Updated to use XCTUnwrap to explicitly check for optionals when performing type coercion.
1 parent de676f6 commit bc3bccd

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

FirebaseSharedSwift/Tests/third_party/DataEncoderTests.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -482,33 +482,32 @@ class TestFirebaseDataEncoder: XCTestCase {
482482
dataDecodingStrategy: .custom(decode))
483483
}
484484

485-
func testDecodingBase64StringAsBlobData() {
486-
let data = "abcdef".data(using: .utf8)!
485+
// Tests implicit migration of data that was written with .base64 (String type) using Firestore
486+
// 10.0 through 10.3 (see PR #10604).
487+
func testDecodingBase64StringAsBlobData() throws {
488+
let inputString = "abcdef"
489+
let data = inputString.data(using: .utf8)!
487490
let base64String = "YWJjZGVm"
488491

489492
let encoder = FirebaseDataEncoder()
490493
encoder.dataEncodingStrategy = .base64
491-
var payload: Any! = nil
492-
do {
493-
payload = try encoder.encode(data)
494-
} catch {
495-
XCTFail("Failed to encode \(Data.self): \(error)")
496-
}
494+
let payload = try encoder.encode(data)
497495

498496
XCTAssertEqual(
499497
base64String,
500-
payload as? String,
501-
"Encoding did not produce the expected base64-encoded \(String.self)."
498+
try XCTUnwrap(payload as? String, "Encoding did not produce a \(String.self)."),
499+
"Encoding \"\(inputString)\" did not produce expected the base64 string \"\(base64String)\"."
502500
)
503501

504502
let decoder = FirebaseDataDecoder()
505503
decoder.dataDecodingStrategy = .blob
506-
do {
507-
let decoded = try decoder.decode(Data.self, from: payload!)
508-
XCTAssertEqual(data, decoded, "Decoding the base64-encoded payload did not produce a \(Data.self).")
509-
} catch {
510-
XCTFail("Failed to decode \(Data.self): \(error)")
511-
}
504+
let decoded = try decoder.decode(Data.self, from: payload)
505+
506+
XCTAssertEqual(
507+
inputString,
508+
try XCTUnwrap(String(data: decoded, encoding: .utf8)),
509+
"Decoding base64-encoded payload did not produce original value \"\(inputString)\"."
510+
)
512511
}
513512

514513
// MARK: - Non-Conforming Floating Point Strategy Tests

0 commit comments

Comments
 (0)