From fef8136e3e8b043fd897f0ef3c12e24170c69326 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Fri, 28 Oct 2022 10:51:57 +0200 Subject: [PATCH] Add IsReadyInfo model --- Sources/XXClient/Models/IsReadyInfo.swift | 26 +++++++++++++++++++ .../Models/IsReadyInfoTests.swift | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Sources/XXClient/Models/IsReadyInfo.swift create mode 100644 Tests/XXClientTests/Models/IsReadyInfoTests.swift diff --git a/Sources/XXClient/Models/IsReadyInfo.swift b/Sources/XXClient/Models/IsReadyInfo.swift new file mode 100644 index 00000000..916f37df --- /dev/null +++ b/Sources/XXClient/Models/IsReadyInfo.swift @@ -0,0 +1,26 @@ +import Foundation + +public struct IsReadyInfo: Equatable { + public init(isReady: Bool, howClose: Double) { + self.isReady = isReady + self.howClose = howClose + } + + public var isReady: Bool + public var howClose: Double +} + +extension IsReadyInfo: Codable { + enum CodingKeys: String, CodingKey { + case isReady = "IsReady" + case howClose = "HowClose" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/XXClientTests/Models/IsReadyInfoTests.swift b/Tests/XXClientTests/Models/IsReadyInfoTests.swift new file mode 100644 index 00000000..9db31131 --- /dev/null +++ b/Tests/XXClientTests/Models/IsReadyInfoTests.swift @@ -0,0 +1,26 @@ +import CustomDump +import XCTest +@testable import XXClient + +final class IsReadyInfoTests: XCTestCase { + func testCoding() throws { + let jsonString = """ + { + "IsReady": true, + "HowClose": 0.534 + } + """ + let jsonData = jsonString.data(using: .utf8)! + let model = try IsReadyInfo.decode(jsonData) + + XCTAssertNoDifference(model, IsReadyInfo( + isReady: true, + howClose: 0.534 + )) + + let encodedModel = try model.encode() + let decodedModel = try IsReadyInfo.decode(encodedModel) + + XCTAssertNoDifference(decodedModel, model) + } +} -- GitLab