From b43ecd4670e7e860399a3272215cd6ee0aac2a7d Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Thu, 5 Dec 2024 16:54:20 +0800 Subject: [PATCH] update codec --- Codec/Tests/CodecTests/ResultTests.swift | 30 +++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Codec/Tests/CodecTests/ResultTests.swift b/Codec/Tests/CodecTests/ResultTests.swift index b3178711..bc6469e9 100644 --- a/Codec/Tests/CodecTests/ResultTests.swift +++ b/Codec/Tests/CodecTests/ResultTests.swift @@ -18,7 +18,6 @@ struct ResultCodingTests { init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - if container.contains(.success) { let value = try container.decode(Success.self, forKey: .success) self = .success(value) @@ -43,6 +42,24 @@ struct ResultCodingTests { } } + struct TestStruct: Codable, Equatable { + let name: String + } + + @Test func structTests() throws { + let successResult: MyResult = .success(TestStruct(name: "Success")) + let failureResult: MyResult = .failure(.unknownError("Test Error")) + + let encodedSuccess = try JamEncoder.encode(successResult) + let encodedFailure = try JamEncoder.encode(failureResult) + + let decodedSuccess = try JamDecoder.decode(MyResult.self, from: encodedSuccess) + let decodedFailure = try JamDecoder.decode(MyResult.self, from: encodedFailure) + + #expect(decodedFailure != nil) + #expect(decodedSuccess != nil) + } + @Test func result() throws { let successResult: MyResult = .success("Success!") let failureResult: MyResult = .failure(.unknownError("An unknown error occurred")) @@ -56,10 +73,17 @@ struct ResultCodingTests { #expect(decodedSuccess != nil) } - @Test func invalidVariant() throws { + @Test func variant() throws { + let invalidData0 = Data([0x00] + "Variant".utf8) + let invalidData1 = Data([0x01] + "Variant".utf8) // Invalid variant value (e.g. value 2) let invalidData = Data([0x02] + "Invalid variant".utf8) - + #expect(throws: Error.self) { + _ = try JamDecoder.decode(Result.self, from: invalidData0) + } + #expect(throws: Error.self) { + _ = try JamDecoder.decode(Result.self, from: invalidData1) + } // Expect decoding to fail and return nil #expect(throws: Error.self) { _ = try JamDecoder.decode(Result.self, from: invalidData)