|
| 1 | +// |
| 2 | +// NestedChoiceTests.swift |
| 3 | +// XMLCoderTests |
| 4 | +// |
| 5 | +// Created by James Bean on 7/15/19. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +import XMLCoder |
| 10 | + |
| 11 | +private struct Container: Equatable { |
| 12 | + let paragraphs: [Paragraph] |
| 13 | +} |
| 14 | + |
| 15 | +private struct Paragraph: Equatable { |
| 16 | + let entries: [Entry] |
| 17 | +} |
| 18 | + |
| 19 | +private enum Entry: Equatable { |
| 20 | + case run(Run) |
| 21 | + case properties(Properties) |
| 22 | + case br(Break) |
| 23 | +} |
| 24 | + |
| 25 | +private struct Run: Codable, Equatable { |
| 26 | + let id: Int |
| 27 | + let text: String |
| 28 | +} |
| 29 | + |
| 30 | +private struct Properties: Codable, Equatable { |
| 31 | + let id: Int |
| 32 | + let title: String |
| 33 | +} |
| 34 | + |
| 35 | +private struct Break: Codable, Equatable { } |
| 36 | + |
| 37 | +extension Container: Codable { |
| 38 | + enum CodingKeys: String, CodingKey { |
| 39 | + case paragraphs = "p" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +extension Paragraph: Codable { |
| 44 | + init(from decoder: Decoder) throws { |
| 45 | + let container = try decoder.singleValueContainer() |
| 46 | + self.entries = try container.decode([Entry].self) |
| 47 | + } |
| 48 | + |
| 49 | + func encode(to encoder: Encoder) throws { |
| 50 | + var container = encoder.singleValueContainer() |
| 51 | + try container.encode(entries) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +extension Entry: XMLChoiceCodable { |
| 56 | + private enum CodingKeys: String, CodingKey { |
| 57 | + case run, properties, br |
| 58 | + } |
| 59 | + public init(from decoder: Decoder) throws { |
| 60 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 61 | + do { |
| 62 | + self = .run(try container.decode(Run.self, forKey: .run)) |
| 63 | + } catch DecodingError.keyNotFound { |
| 64 | + do { |
| 65 | + self = .properties(try container.decode(Properties.self, forKey: .properties)) |
| 66 | + } catch DecodingError.keyNotFound { |
| 67 | + self = .br(try container.decode(Break.self, forKey: .br)) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + func encode(to encoder: Encoder) throws { |
| 73 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 74 | + switch self { |
| 75 | + case let .run(value): |
| 76 | + try container.encode(value, forKey: .run) |
| 77 | + case let .properties(value): |
| 78 | + try container.encode(value, forKey: .properties) |
| 79 | + case let .br(value): |
| 80 | + try container.encode(value, forKey: .br) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class NestedChoiceTests: XCTestCase { |
| 86 | + |
| 87 | + func testBreakDecoding() throws { |
| 88 | + let xml = "<br></br>" |
| 89 | + let result = try XMLDecoder().decode(Break.self, from: xml.data(using: .utf8)!) |
| 90 | + let expected = Break() |
| 91 | + XCTAssertEqual(result, expected) |
| 92 | + } |
| 93 | + |
| 94 | + func testPropertiesDecoding() throws { |
| 95 | + let xml = """ |
| 96 | + <properties> |
| 97 | + <id>431</id> |
| 98 | + <title>A Word About Wake Times</title> |
| 99 | + </properties> |
| 100 | + """ |
| 101 | + let result = try XMLDecoder().decode(Properties.self, from: xml.data(using: .utf8)!) |
| 102 | + let expected = Properties(id: 431, title: "A Word About Wake Times") |
| 103 | + XCTAssertEqual(result, expected) |
| 104 | + } |
| 105 | + |
| 106 | + func testPropertiesAsEntryDecoding() throws { |
| 107 | + let xml = """ |
| 108 | + <entry> |
| 109 | + <properties> |
| 110 | + <id>431</id> |
| 111 | + <title>A Word About Wake Times</title> |
| 112 | + </properties> |
| 113 | + </entry> |
| 114 | + """ |
| 115 | + let result = try XMLDecoder().decode(Entry.self, from: xml.data(using: .utf8)!) |
| 116 | + let expected: Entry = .properties(Properties(id: 431, title: "A Word About Wake Times")) |
| 117 | + XCTAssertEqual(result, expected) |
| 118 | + } |
| 119 | + |
| 120 | + func testRunDecoding() throws { |
| 121 | + let xml = """ |
| 122 | + <run> |
| 123 | + <id>1518</id> |
| 124 | + <text>I am answering it again.</text> |
| 125 | + </run> |
| 126 | + """ |
| 127 | + let result = try XMLDecoder().decode(Run.self, from: xml.data(using: .utf8)!) |
| 128 | + let expected = Run(id: 1518, text: "I am answering it again.") |
| 129 | + XCTAssertEqual(result, expected) |
| 130 | + } |
| 131 | + |
| 132 | + func testRunAsEntryDecoding() throws { |
| 133 | + let xml = """ |
| 134 | + <entry> |
| 135 | + <run> |
| 136 | + <id>1518</id> |
| 137 | + <text>I am answering it again.</text> |
| 138 | + </run> |
| 139 | + </entry> |
| 140 | + """ |
| 141 | + let result = try XMLDecoder().decode(Entry.self, from: xml.data(using: .utf8)!) |
| 142 | + let expected = Entry.run(Run(id: 1518, text: "I am answering it again.")) |
| 143 | + XCTAssertEqual(result, expected) |
| 144 | + } |
| 145 | + |
| 146 | + func testEntriesDecoding() throws { |
| 147 | + let xml = """ |
| 148 | + <entries> |
| 149 | + <run> |
| 150 | + <id>1518</id> |
| 151 | + <text>I am answering it again.</text> |
| 152 | + </run> |
| 153 | + <properties> |
| 154 | + <id>431</id> |
| 155 | + <title>A Word About Wake Times</title> |
| 156 | + </properties> |
| 157 | + </entries> |
| 158 | + """ |
| 159 | + let result = try XMLDecoder().decode([Entry].self, from: xml.data(using: .utf8)!) |
| 160 | + let expected: [Entry] = [ |
| 161 | + .run(Run(id: 1518, text: "I am answering it again.")), |
| 162 | + .properties(Properties(id: 431, title: "A Word About Wake Times")) |
| 163 | + ] |
| 164 | + XCTAssertEqual(result, expected) |
| 165 | + } |
| 166 | + |
| 167 | + func testParagraphDecoding() throws { |
| 168 | + let xml = """ |
| 169 | + <p> |
| 170 | + <run> |
| 171 | + <id>1518</id> |
| 172 | + <text>I am answering it again.</text> |
| 173 | + </run> |
| 174 | + <properties> |
| 175 | + <id>431</id> |
| 176 | + <title>A Word About Wake Times</title> |
| 177 | + </properties> |
| 178 | + </p> |
| 179 | + """ |
| 180 | + let result = try XMLDecoder().decode(Paragraph.self, from: xml.data(using: .utf8)!) |
| 181 | + let expected = Paragraph( |
| 182 | + entries: [ |
| 183 | + .run(Run(id: 1518, text: "I am answering it again.")), |
| 184 | + .properties(Properties(id: 431, title: "A Word About Wake Times")) |
| 185 | + ] |
| 186 | + ) |
| 187 | + XCTAssertEqual(result, expected) |
| 188 | + } |
| 189 | + |
| 190 | + func testNestedEnums() throws { |
| 191 | + let xml = """ |
| 192 | + <container> |
| 193 | + <p> |
| 194 | + <run> |
| 195 | + <id>1518</id> |
| 196 | + <text>I am answering it again.</text> |
| 197 | + </run> |
| 198 | + <properties> |
| 199 | + <id>431</id> |
| 200 | + <title>A Word About Wake Times</title> |
| 201 | + </properties> |
| 202 | + </p> |
| 203 | + <p> |
| 204 | + <run> |
| 205 | + <id>1519</id> |
| 206 | + <text>I am answering it again.</text> |
| 207 | + </run> |
| 208 | + </p> |
| 209 | + </container> |
| 210 | + """ |
| 211 | + let result = try XMLDecoder().decode(Container.self, from: xml.data(using: .utf8)!) |
| 212 | + let expected = Container( |
| 213 | + paragraphs: [ |
| 214 | + Paragraph( |
| 215 | + entries: [ |
| 216 | + .run(Run(id: 1518, text: "I am answering it again.")), |
| 217 | + .properties(Properties(id: 431, title: "A Word About Wake Times")), |
| 218 | + ] |
| 219 | + ), |
| 220 | + Paragraph( |
| 221 | + entries: [ |
| 222 | + .run(Run(id: 1519, text: "I am answering it again.")), |
| 223 | + ] |
| 224 | + ) |
| 225 | + ] |
| 226 | + ) |
| 227 | + XCTAssertEqual(result, expected) |
| 228 | + } |
| 229 | + |
| 230 | + func testNestedEnumsRoundTrip() throws { |
| 231 | + let original = Container( |
| 232 | + paragraphs: [ |
| 233 | + Paragraph( |
| 234 | + entries: [ |
| 235 | + .run(Run(id: 1518, text: "I am answering it again.")), |
| 236 | + .properties(Properties(id: 431, title: "A Word About Wake Times")), |
| 237 | + ] |
| 238 | + ), |
| 239 | + Paragraph( |
| 240 | + entries: [ |
| 241 | + .run(Run(id: 1519, text: "I am answering it again.")), |
| 242 | + ] |
| 243 | + ) |
| 244 | + ] |
| 245 | + ) |
| 246 | + let encoded = try XMLEncoder().encode(original, withRootKey: "container") |
| 247 | + let decoded = try XMLDecoder().decode(Container.self, from: encoded) |
| 248 | + XCTAssertEqual(decoded, original) |
| 249 | + } |
| 250 | +} |
0 commit comments