|
| 1 | +// |
| 2 | +// NestedChoiceArrayTest.swift |
| 3 | +// XMLCoder |
| 4 | +// |
| 5 | +// Created by Benjamin Wetherfield on 8/22/19. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import XMLCoder |
| 10 | + |
| 11 | +private struct Book: Decodable { |
| 12 | + let title: String |
| 13 | + let chapters: Chapters |
| 14 | + |
| 15 | + enum CodingKeys: String, CodingKey { |
| 16 | + case title |
| 17 | + case chapters |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +private struct Chapters { |
| 22 | + let items: [Chapter] |
| 23 | +} |
| 24 | + |
| 25 | +extension Chapters: Decodable, Equatable { |
| 26 | + init(from decoder: Decoder) throws { |
| 27 | + let container = try decoder.singleValueContainer() |
| 28 | + items = try container.decode([Chapter].self) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +private enum Chapter { |
| 33 | + struct Content { |
| 34 | + let title: String |
| 35 | + let content: String |
| 36 | + } |
| 37 | + |
| 38 | + case intro(Content) |
| 39 | + case body(Content) |
| 40 | + case outro(Content) |
| 41 | +} |
| 42 | + |
| 43 | +extension Chapter: Decodable { |
| 44 | + enum CodingKeys: String, XMLChoiceCodingKey { |
| 45 | + case intro, body = "chapter", outro |
| 46 | + } |
| 47 | + |
| 48 | + init(from decoder: Decoder) throws { |
| 49 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 50 | + do { |
| 51 | + self = .body(try container.decode(Content.self, forKey: .body)) |
| 52 | + } catch { |
| 53 | + do { |
| 54 | + self = .intro(try container.decode(Content.self, forKey: .intro)) |
| 55 | + } catch { |
| 56 | + self = .outro(try container.decode(Content.self, forKey: .outro)) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extension Chapter.Content: Decodable { |
| 63 | + enum CodingKeys: String, CodingKey { |
| 64 | + case title |
| 65 | + case content = "" |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extension Book: Equatable {} |
| 70 | +extension Chapter: Equatable {} |
| 71 | +extension Chapter.Content: Equatable {} |
| 72 | + |
| 73 | +class NestedChoiceArrayTest: XCTestCase { |
| 74 | + func testDecodingNestedChoiceArray() throws { |
| 75 | + let xml = """ |
| 76 | + <?xml version="1.0" encoding="UTF-8"?> |
| 77 | + <book title="Example"> |
| 78 | + <chapters> |
| 79 | + <intro title="Intro">Content of first chapter</intro> |
| 80 | + <chapter title="Chapter 1">Content of chapter 1</chapter> |
| 81 | + <chapter title="Chapter 2">Content of chapter 2</chapter> |
| 82 | + <outro title="Epilogue">Content of last chapter</outro> |
| 83 | + </chapters> |
| 84 | + </book> |
| 85 | + """ |
| 86 | + let decoded = try XMLDecoder().decode(Book.self, from: xml.data(using: .utf8)!) |
| 87 | + let expected = Book(title: "Example", |
| 88 | + chapters: Chapters(items: [ |
| 89 | + .intro(.init(title: "Intro", content: "Content of first chapter")), |
| 90 | + .body(.init(title: "Chapter 1", content: "Content of chapter 1")), |
| 91 | + .body(.init(title: "Chapter 2", content: "Content of chapter 2")), |
| 92 | + .outro(.init(title: "Epilogue", content: "Content of last chapter")), |
| 93 | + ])) |
| 94 | + XCTAssertEqual(decoded, expected) |
| 95 | + } |
| 96 | +} |
0 commit comments