Skip to content

Commit e9ff60a

Browse files
bwetherfieldArjun Gupta
authored andcommitted
Add test for decoding of nested arrays of enums (CoreOffice#126)
Add `NestedChoiceArrayTest` based on the code shared in CoreOffice#122.
1 parent ac32e6b commit e9ff60a

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

XMLCoder.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24+
B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */; };
2425
B5F74472233F74E400BBDB15 /* RootLevelAttributeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */; };
2526
OBJ_148 /* BoolBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* BoolBox.swift */; };
2627
OBJ_149 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* Box.swift */; };
@@ -153,6 +154,7 @@
153154
/* End PBXContainerItemProxy section */
154155

155156
/* Begin PBXFileReference section */
157+
B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedChoiceArrayTest.swift; sourceTree = "<group>"; };
156158
B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootLevelAttributeTest.swift; sourceTree = "<group>"; };
157159
OBJ_100 /* DecimalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalTests.swift; sourceTree = "<group>"; };
158160
OBJ_101 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = "<group>"; };
@@ -448,6 +450,7 @@
448450
OBJ_125 /* SimpleChoiceTests.swift */,
449451
OBJ_126 /* SingleChildTests.swift */,
450452
OBJ_127 /* SpacePreserveTest.swift */,
453+
B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */,
451454
B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */,
452455
);
453456
name = XMLCoderTests;
@@ -721,6 +724,7 @@
721724
OBJ_252 /* NullTests.swift in Sources */,
722725
OBJ_253 /* OptionalTests.swift in Sources */,
723726
OBJ_254 /* StringTests.swift in Sources */,
727+
B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */,
724728
OBJ_255 /* UIntTests.swift in Sources */,
725729
OBJ_256 /* URLTests.swift in Sources */,
726730
OBJ_257 /* UnkeyedIntTests.swift in Sources */,

XMLCoder.xcodeproj/xcshareddata/xcbaselines/XMLCoder::XMLCoderTests.xcbaseline/76E090BF-7AFE-4988-A06A-3C423396A4A4.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
6262
<dict>
6363
<key>baselineAverage</key>
64-
<real>0.08847</real>
64+
<real>0.263</real>
6565
<key>baselineIntegrationDisplayName</key>
6666
<string>Local Baseline</string>
6767
</dict>

0 commit comments

Comments
 (0)