Skip to content
Snippets Groups Projects
Commit 450ca488 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add NotificationReport array coding helpers

parent c11308a8
No related branches found
No related tags found
2 merge requests!142Fix GetNotificationsReport,!102Release 1.0.0
...@@ -45,3 +45,13 @@ extension NotificationReport: Codable { ...@@ -45,3 +45,13 @@ extension NotificationReport: Codable {
try JSONEncoder().encode(self) try JSONEncoder().encode(self)
} }
} }
extension Array where Element == NotificationReport {
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
...@@ -28,4 +28,43 @@ final class NotificationReportTests: XCTestCase { ...@@ -28,4 +28,43 @@ final class NotificationReportTests: XCTestCase {
XCTAssertNoDifference(decodedModel, model) XCTAssertNoDifference(decodedModel, model)
} }
func testCodingArray() throws {
let source1B64 = "dGVzdGVyMTIzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
let source2B64 = "ciI1cpyUUY/UPaVeMy1zBFWbZRgiZSXhY+cVoM+fCxwD"
let jsonString = """
[
{
"ForMe": true,
"Type": "\(NotificationReport.ReportType.default.rawValue)",
"Source": "\(source1B64)"
},
{
"ForMe": false,
"Type": "\(NotificationReport.ReportType.request.rawValue)",
"Source": "\(source2B64)"
},
]
"""
let jsonData = jsonString.data(using: .utf8)!
let models = try [NotificationReport].decode(jsonData)
XCTAssertNoDifference(models, [
NotificationReport(
forMe: true,
type: .default,
source: Data(base64Encoded: source1B64)!
),
NotificationReport(
forMe: false,
type: .request,
source: Data(base64Encoded: source2B64)!
)
])
let encodedModels = try models.encode()
let decodedModels = try [NotificationReport].decode(encodedModels)
XCTAssertNoDifference(decodedModels, models)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment