Skip to content

Commit

Permalink
Added parent/child decoding test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeredpath committed Jun 25, 2021
1 parent 0429229 commit 38860e6
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Tests/CodingTests/DecodingTests.swift
Expand Up @@ -20,6 +20,16 @@ final class DecodingTests: XCTestCase {
}
}

struct Account: Equatable {
var id: UUID
var user: User?

enum CodingKeys: CodingKey {
case id
case user
}
}

func testMap_WithClosure() {
XCTAssertEqual(
"test string",
Expand Down Expand Up @@ -96,6 +106,92 @@ final class DecodingTests: XCTestCase {
XCTAssertEqual("Unknown", user.city)
}

func testDecodingParentChildDecodings() throws {
let json = """
{
"id": "00000000-0000-0000-0000-000000000001",
"user": {
"name": "Joe Bloggs",
"age": 18,
"city": "London"
}
}
"""

let userDecoding = zip(with: User.init)(
Decoding<String>
.withKey(User.CodingKeys.name),

Decoding<Int>
.withKey(User.CodingKeys.age),

Decoding<String>
.optionalWithKey(User.CodingKeys.city)
)

let accountDecoding = zip(with: Account.init)(
Decoding<UUID>
.withKey(Account.CodingKeys.id),

userDecoding
.withKey(Account.CodingKeys.user)
)

let account = try decoder.decode(
json.data(using: .utf8)!,
as: accountDecoding
)

XCTAssertEqual(
UUID(uuidString: "00000000-0000-0000-0000-000000000001"),
account.id
)

XCTAssertEqual(
User(name: "Joe Bloggs", age: 18, city: "London"),
account.user
)
}

func testDecodingParentChildDecodings_MissingOptionalChildValue() throws {
let json = """
{
"id": "00000000-0000-0000-0000-000000000001"
}
"""

let userDecoding = zip(with: User.init)(
Decoding<String>
.withKey(User.CodingKeys.name),

Decoding<Int>
.withKey(User.CodingKeys.age),

Decoding<String>
.optionalWithKey(User.CodingKeys.city)
)

let accountDecoding = zip(with: Account.init)(
Decoding<UUID>
.withKey(Account.CodingKeys.id),

userDecoding
.optionalWithKey(Account.CodingKeys.user)
)

let account = try decoder.decode(
json.data(using: .utf8)!,
as: accountDecoding
)

XCTAssertEqual(
UUID(uuidString: "00000000-0000-0000-0000-000000000001"),
account.id
)

XCTAssertNil(account.user)
}

// MARK: - Decoding collections

func testDecodingArrayOfSimpleValues() throws {
Expand Down

0 comments on commit 38860e6

Please sign in to comment.