Skip to content

Commit b55956b

Browse files
authored
Add nested choice tests (#18)
1 parent 0a8bbe6 commit b55956b

File tree

2 files changed

+254
-0
lines changed

2 files changed

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

XMLCoder.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
1482D5A222DD2D9400AE2D6E /* SimpleChoiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */; };
2828
1482D5A422DD2F4D00AE2D6E /* CompositeChoiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */; };
2929
1482D5A822DD6AEE00AE2D6E /* SingleElementBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A722DD6AED00AE2D6E /* SingleElementBox.swift */; };
30+
1482D5AA22DD961E00AE2D6E /* NestedChoiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A922DD961E00AE2D6E /* NestedChoiceTests.swift */; };
3031
A61DCCD821DF9CA200C0A19D /* ClassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61DCCD621DF8DB300C0A19D /* ClassTests.swift */; };
3132
A61FE03921E4D60B0015D993 /* UnkeyedIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61FE03721E4D4F10015D993 /* UnkeyedIntTests.swift */; };
3233
A61FE03C21E4EAB10015D993 /* KeyedIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61FE03A21E4EA8B0015D993 /* KeyedIntTests.swift */; };
@@ -155,6 +156,7 @@
155156
1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleChoiceTests.swift; sourceTree = "<group>"; };
156157
1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositeChoiceTests.swift; sourceTree = "<group>"; };
157158
1482D5A722DD6AED00AE2D6E /* SingleElementBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleElementBox.swift; sourceTree = "<group>"; };
159+
1482D5A922DD961E00AE2D6E /* NestedChoiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedChoiceTests.swift; sourceTree = "<group>"; };
158160
A61DCCD621DF8DB300C0A19D /* ClassTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassTests.swift; sourceTree = "<group>"; };
159161
A61FE03721E4D4F10015D993 /* UnkeyedIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnkeyedIntTests.swift; sourceTree = "<group>"; };
160162
A61FE03A21E4EA8B0015D993 /* KeyedIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyedIntTests.swift; sourceTree = "<group>"; };
@@ -414,6 +416,7 @@
414416
BF63EF1D21CEC99A001D38C5 /* BenchmarkTests.swift */,
415417
1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */,
416418
1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */,
419+
1482D5A922DD961E00AE2D6E /* NestedChoiceTests.swift */,
417420
OBJ_28 /* BooksTest.swift */,
418421
D1B6A2C02297EF5A005B8A6E /* BorderTest.swift */,
419422
OBJ_29 /* BreakfastTest.swift */,
@@ -724,6 +727,7 @@
724727
BF63EF0821CD7AF8001D38C5 /* URLBoxTests.swift in Sources */,
725728
BF9457DD21CBB62C005ACFDE /* DateBoxTests.swift in Sources */,
726729
A61DCCD821DF9CA200C0A19D /* ClassTests.swift in Sources */,
730+
1482D5AA22DD961E00AE2D6E /* NestedChoiceTests.swift in Sources */,
727731
BF9457CD21CBB516005ACFDE /* FloatBoxTests.swift in Sources */,
728732
BF9457F621CBB6BC005ACFDE /* KeyedTests.swift in Sources */,
729733
BF9457C821CBB516005ACFDE /* BoolBoxTests.swift in Sources */,

0 commit comments

Comments
 (0)