From bb00851ce2af8607d51bfc5a26720c4f2dcd0039 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Fri, 3 Jun 2022 12:18:46 +0200 Subject: [PATCH] Add ContactFact codable model --- Sources/ElixxirDAppsSDK/ContactFact.swift | 19 ++++++++++++ .../ContactFactTests.swift | 30 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/ContactFact.swift create mode 100644 Tests/ElixxirDAppsSDKTests/ContactFactTests.swift diff --git a/Sources/ElixxirDAppsSDK/ContactFact.swift b/Sources/ElixxirDAppsSDK/ContactFact.swift new file mode 100644 index 00000000..f4401028 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ContactFact.swift @@ -0,0 +1,19 @@ +public struct ContactFact: Equatable { + public init( + fact: String, + type: Int + ) { + self.fact = fact + self.type = type + } + + public var fact: String + public var type: Int +} + +extension ContactFact: Codable { + enum CodingKeys: String, CodingKey { + case fact = "Fact" + case type = "Type" + } +} diff --git a/Tests/ElixxirDAppsSDKTests/ContactFactTests.swift b/Tests/ElixxirDAppsSDKTests/ContactFactTests.swift new file mode 100644 index 00000000..a95b0b9e --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/ContactFactTests.swift @@ -0,0 +1,30 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class ContactFactTests: XCTestCase { + func testCoding() throws { + let jsonString = """ + { + "Fact": "Zezima", + "Type": 0 + } + """ + let jsonData = jsonString.data(using: .utf8)! + let decoder = JSONDecoder() + decoder.dataDecodingStrategy = .base64 + let fact = try decoder.decode(ContactFact.self, from: jsonData) + + XCTAssertNoDifference(fact, ContactFact( + fact: "Zezima", + type: 0 + )) + + let encoder = JSONEncoder() + encoder.dataEncodingStrategy = .base64 + let encodedFact = try encoder.encode(fact) + let decodedFact = try decoder.decode(ContactFact.self, from: encodedFact) + + XCTAssertNoDifference(decodedFact, fact) + } +} -- GitLab