Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Decoding of Nested Arrays of Enums #126

Merged
merged 6 commits into from Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 96 additions & 0 deletions Tests/XMLCoderTests/NestedChoiceArrayTest.swift
@@ -0,0 +1,96 @@
//
// NestedChoiceArrayTest.swift
// XMLCoder
//
// Created by Benjamin Wetherfield on 8/22/19.
//

import XCTest
@testable import XMLCoder

private struct Book: Decodable {
let title: String
let chapters: Chapters

enum CodingKeys: String, CodingKey {
case title
case chapters
}
}

private struct Chapters {
let items: [Chapter]
}

extension Chapters: Decodable, Equatable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
items = try container.decode([Chapter].self)
}
}

private enum Chapter {
struct Content {
let title: String
let content: String
}

case intro(Content)
case body(Content)
case outro(Content)
}

extension Chapter: Decodable {
enum CodingKeys: String, XMLChoiceCodingKey {
case intro, body = "chapter", outro
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self = .body(try container.decode(Content.self, forKey: .body))
} catch {
do {
self = .intro(try container.decode(Content.self, forKey: .intro))
} catch {
self = .outro(try container.decode(Content.self, forKey: .outro))
}
}
}
}

extension Chapter.Content: Decodable {
enum CodingKeys: String, CodingKey {
case title
case content = ""
}
}

extension Book: Equatable {}
extension Chapter: Equatable {}
extension Chapter.Content: Equatable {}

class NestedChoiceArrayTest: XCTestCase {
func testDecodingNestedChoiceArray() throws {
let xml = """
<?xml version="1.0" encoding="UTF-8"?>
<book title="Example">
<chapters>
<intro title="Intro">Content of first chapter</intro>
<chapter title="Chapter 1">Content of chapter 1</chapter>
<chapter title="Chapter 2">Content of chapter 2</chapter>
<outro title="Epilogue">Content of last chapter</outro>
</chapters>
</book>
"""
let decoded = try XMLDecoder().decode(Book.self, from: xml.data(using: .utf8)!)
let expected = Book(title: "Example",
chapters: Chapters(items: [
.intro(.init(title: "Intro", content: "Content of first chapter")),
.body(.init(title: "Chapter 1", content: "Content of chapter 1")),
.body(.init(title: "Chapter 2", content: "Content of chapter 2")),
.outro(.init(title: "Epilogue", content: "Content of last chapter")),
]))
XCTAssertEqual(decoded, expected)
}
}
4 changes: 4 additions & 0 deletions XMLCoder.xcodeproj/project.pbxproj
Expand Up @@ -21,6 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */; };
B5F74472233F74E400BBDB15 /* RootLevelAttributeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */; };
OBJ_148 /* BoolBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* BoolBox.swift */; };
OBJ_149 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* Box.swift */; };
Expand Down Expand Up @@ -153,6 +154,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedChoiceArrayTest.swift; sourceTree = "<group>"; };
B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootLevelAttributeTest.swift; sourceTree = "<group>"; };
OBJ_100 /* DecimalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalTests.swift; sourceTree = "<group>"; };
OBJ_101 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -448,6 +450,7 @@
OBJ_125 /* SimpleChoiceTests.swift */,
OBJ_126 /* SingleChildTests.swift */,
OBJ_127 /* SpacePreserveTest.swift */,
B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */,
B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */,
);
name = XMLCoderTests;
Expand Down Expand Up @@ -721,6 +724,7 @@
OBJ_252 /* NullTests.swift in Sources */,
OBJ_253 /* OptionalTests.swift in Sources */,
OBJ_254 /* StringTests.swift in Sources */,
B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */,
OBJ_255 /* UIntTests.swift in Sources */,
OBJ_256 /* URLTests.swift in Sources */,
OBJ_257 /* UnkeyedIntTests.swift in Sources */,
Expand Down
Expand Up @@ -61,7 +61,7 @@
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.08847</real>
<real>0.263</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
Expand Down