From d4806ff557dfe9ceff0a6b350d17db5b29ccf225 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sun, 29 Sep 2019 15:36:16 -0700 Subject: [PATCH] Add a few decode examples --- .../Error/BasicJSONAPIErrorTests.swift | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift b/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift index e726143..05bcf2f 100644 --- a/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift +++ b/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift @@ -8,6 +8,7 @@ import Foundation @testable import JSONAPI import XCTest +import Poly final class BasicJSONAPIErrorTests: XCTestCase { func test_initAndEquality() { @@ -89,4 +90,42 @@ final class BasicJSONAPIErrorTests: XCTestCase { XCTAssertEqual(wellPopulatedError.definedFields["pointer"], "/data/attributes/id") XCTAssertEqual(wellPopulatedError.definedFields["parameter"], "id") } + + func test_decodeAFewExamples() { + let datas = [ +""" +{ + "id": "hello" +} +""", +""" +{ + "id": 1234 +} +""", +""" +{ + "status": "404", + "title": "Missing", + "links": { + "about": "https://google.com" + } +} +""", +""" +{ + "status": 404 +} +""" + ].map { $0.data(using: .utf8)! } + + let errors = datas + .map { decoded(type: BasicJSONAPIError>.self, data: $0) } + + XCTAssertEqual(errors[0].payload?.id, .init("hello")) + XCTAssertEqual(errors[1].payload?.id, .init(1234)) + XCTAssertEqual(errors[2].payload?.status, "404") + XCTAssertEqual(errors[2].payload?.title, "Missing") + XCTAssertEqual(errors[3], .unknown) + } }