Skip to content

Commit 649d906

Browse files
authored
Pull in tests (#11)
1 parent 2ca8624 commit 649d906

3 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// CompositeChoiceTests.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 IntWrapper: Codable, Equatable {
12+
let wrapped: Int
13+
}
14+
15+
private struct StringWrapper: Codable, Equatable {
16+
let wrapped: String
17+
}
18+
19+
private enum IntOrStringWrapper: Equatable {
20+
case int(IntWrapper)
21+
case string(StringWrapper)
22+
}
23+
24+
extension IntOrStringWrapper: Codable {
25+
26+
enum CodingKeys: String, CodingKey {
27+
case int
28+
case string
29+
}
30+
31+
init(from decoder: Decoder) throws {
32+
let container = try decoder.container(keyedBy: CodingKeys.self)
33+
do {
34+
self = .int(try container.decode(IntWrapper.self, forKey: .int))
35+
} catch {
36+
self = .string(try container.decode(StringWrapper.self, forKey: .string))
37+
}
38+
}
39+
40+
func encode(to encoder: Encoder) throws {
41+
var container = encoder.container(keyedBy: CodingKeys.self)
42+
switch self {
43+
case let .int(value):
44+
try container.encode(value, forKey: .int)
45+
case let .string(value):
46+
try container.encode(value, forKey: .string)
47+
}
48+
}
49+
}
50+
51+
class CompositeChoiceTests: XCTestCase {
52+
53+
func testIntOrStringWrapper() throws {
54+
let xml = """
55+
<container>
56+
<string>
57+
<wrapped>A Word About Woke Times</wrapped>
58+
</string>
59+
</container>
60+
"""
61+
let result = try XMLDecoder().decode(IntOrStringWrapper.self, from: xml.data(using: .utf8)!)
62+
let expected = IntOrStringWrapper.string(StringWrapper(wrapped: "A Word About Woke Times"))
63+
XCTAssertEqual(result, expected)
64+
}
65+
66+
func testArrayOfIntOrStringWrappers() throws {
67+
let xml = """
68+
<container>
69+
<string>
70+
<wrapped>A Word About Woke Times</wrapped>
71+
</string>
72+
<int>
73+
<wrapped>9000</wrapped>
74+
</int>
75+
<string>
76+
<wrapped>A Word About Woke Tomes</wrapped>
77+
</string>
78+
</container>
79+
"""
80+
let result = try XMLDecoder().decode([IntOrStringWrapper].self, from: xml.data(using: .utf8)!)
81+
let expected: [IntOrStringWrapper] = [
82+
.string(StringWrapper(wrapped: "A Word About Woke Times")),
83+
.int(IntWrapper(wrapped: 9000)),
84+
.string(StringWrapper(wrapped: "A Word About Woke Tomes")),
85+
]
86+
XCTAssertEqual(result, expected)
87+
}
88+
89+
#warning("TODO: Add encoding and round-trip tests")
90+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// SimpleChoiceTests.swift
3+
// XMLCoderTests
4+
//
5+
// Created by James Bean on 7/15/19.
6+
//
7+
8+
import XCTest
9+
import XMLCoder
10+
11+
private enum IntOrString: Equatable {
12+
case int(Int)
13+
case string(String)
14+
}
15+
16+
extension IntOrString: Codable {
17+
enum CodingKeys: String, CodingKey {
18+
case int
19+
case string
20+
}
21+
22+
func encode(to encoder: Encoder) throws {
23+
var container = encoder.container(keyedBy: CodingKeys.self)
24+
switch self {
25+
case let .int(value):
26+
try container.encode(value, forKey: .int)
27+
case let .string(value):
28+
try container.encode(value, forKey: .string)
29+
}
30+
}
31+
32+
init(from decoder: Decoder) throws {
33+
let container = try decoder.container(keyedBy: CodingKeys.self)
34+
do {
35+
self = .int(try container.decode(Int.self, forKey: .int))
36+
} catch {
37+
self = .string(try container.decode(String.self, forKey: .string))
38+
}
39+
}
40+
}
41+
42+
class SimpleChoiceTests: XCTestCase {
43+
44+
func testIntOrStringIntDecoding() throws {
45+
let xml = "<int>42</int>"
46+
let result = try XMLDecoder().decode(IntOrString.self, from: xml.data(using: .utf8)!)
47+
let expected = IntOrString.int(42)
48+
XCTAssertEqual(result, expected)
49+
}
50+
51+
func testIntOrStringStringDecoding() throws {
52+
let xml = "<string>forty-two</string>"
53+
let result = try XMLDecoder().decode(IntOrString.self, from: xml.data(using: .utf8)!)
54+
let expected = IntOrString.string("forty-two")
55+
XCTAssertEqual(result, expected)
56+
}
57+
58+
func testIntOrStringArrayDecoding() throws {
59+
let xml = """
60+
<container>
61+
<int>1</int>
62+
<string>two</string>
63+
<string>three</string>
64+
<int>4</int>
65+
<int>5</int>
66+
</container>
67+
"""
68+
let result = try XMLDecoder().decode([IntOrString].self, from: xml.data(using: .utf8)!)
69+
let expected: [IntOrString] = [
70+
.int(1),
71+
.string("two"),
72+
.string("three"),
73+
.int(4),
74+
.int(5),
75+
]
76+
XCTAssertEqual(result, expected)
77+
}
78+
79+
func testIntOrStringRoundTrip() throws {
80+
let original = IntOrString.int(5)
81+
let encoded = try XMLEncoder().encode(original, withRootKey: "container")
82+
let decoded = try XMLDecoder().decode(IntOrString.self, from: encoded)
83+
XCTAssertEqual(original, decoded)
84+
}
85+
86+
func testIntOrStringArrayRoundTrip() throws {
87+
let original: [IntOrString] = [
88+
.int(1),
89+
.string("two"),
90+
.string("three"),
91+
.int(4),
92+
.int(5),
93+
]
94+
let encoded = try XMLEncoder().encode(original, withRootKey: "container")
95+
let decoded = try XMLDecoder().decode([IntOrString].self, from: encoded)
96+
XCTAssertEqual(original, decoded)
97+
}
98+
}

XMLCoder.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
1482D59A22DD2A1700AE2D6E /* XMLChoiceCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D59922DD2A1700AE2D6E /* XMLChoiceCodable.swift */; };
2525
1482D59C22DD2A4400AE2D6E /* XMLChoiceDecodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D59B22DD2A4400AE2D6E /* XMLChoiceDecodable.swift */; };
2626
1482D59E22DD2A6B00AE2D6E /* XMLChoiceEncodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D59D22DD2A6B00AE2D6E /* XMLChoiceEncodable.swift */; };
27+
1482D5A222DD2D9400AE2D6E /* SimpleChoiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */; };
28+
1482D5A422DD2F4D00AE2D6E /* CompositeChoiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */; };
2729
A61DCCD821DF9CA200C0A19D /* ClassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61DCCD621DF8DB300C0A19D /* ClassTests.swift */; };
2830
A61FE03921E4D60B0015D993 /* UnkeyedIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61FE03721E4D4F10015D993 /* UnkeyedIntTests.swift */; };
2931
A61FE03C21E4EAB10015D993 /* KeyedIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A61FE03A21E4EA8B0015D993 /* KeyedIntTests.swift */; };
@@ -149,6 +151,8 @@
149151
1482D59922DD2A1700AE2D6E /* XMLChoiceCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLChoiceCodable.swift; sourceTree = "<group>"; };
150152
1482D59B22DD2A4400AE2D6E /* XMLChoiceDecodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLChoiceDecodable.swift; sourceTree = "<group>"; };
151153
1482D59D22DD2A6B00AE2D6E /* XMLChoiceEncodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLChoiceEncodable.swift; sourceTree = "<group>"; };
154+
1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleChoiceTests.swift; sourceTree = "<group>"; };
155+
1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositeChoiceTests.swift; sourceTree = "<group>"; };
152156
A61DCCD621DF8DB300C0A19D /* ClassTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassTests.swift; sourceTree = "<group>"; };
153157
A61FE03721E4D4F10015D993 /* UnkeyedIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnkeyedIntTests.swift; sourceTree = "<group>"; };
154158
A61FE03A21E4EA8B0015D993 /* KeyedIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyedIntTests.swift; sourceTree = "<group>"; };
@@ -405,6 +409,8 @@
405409
D1AC9464224E3E1F004AB49B /* AttributedEnumIntrinsicTest.swift */,
406410
B3B6902D220A71DF0084D407 /* AttributedIntrinsicTest.swift */,
407411
BF63EF1D21CEC99A001D38C5 /* BenchmarkTests.swift */,
412+
1482D5A122DD2D9400AE2D6E /* SimpleChoiceTests.swift */,
413+
1482D5A322DD2F4D00AE2D6E /* CompositeChoiceTests.swift */,
408414
OBJ_28 /* BooksTest.swift */,
409415
D1B6A2C02297EF5A005B8A6E /* BorderTest.swift */,
410416
OBJ_29 /* BreakfastTest.swift */,
@@ -681,6 +687,7 @@
681687
D1EC3E62225A32F500C610E3 /* BoxTreeTests.swift in Sources */,
682688
BF63EF0A21CD7C1A001D38C5 /* URLTests.swift in Sources */,
683689
BF9457CE21CBB516005ACFDE /* StringBoxTests.swift in Sources */,
690+
1482D5A222DD2D9400AE2D6E /* SimpleChoiceTests.swift in Sources */,
684691
D1CFC8242226B13F00B03222 /* NamespaceTest.swift in Sources */,
685692
BF9457D021CBB516005ACFDE /* UIntBoxTests.swift in Sources */,
686693
OBJ_80 /* BooksTest.swift in Sources */,
@@ -723,6 +730,7 @@
723730
BF9457F121CBB6BC005ACFDE /* FloatTests.swift in Sources */,
724731
BF8171D021D3B1BD00901EB0 /* DecodingContainerTests.swift in Sources */,
725732
BF9457EF21CBB6BC005ACFDE /* NullTests.swift in Sources */,
733+
1482D5A422DD2F4D00AE2D6E /* CompositeChoiceTests.swift in Sources */,
726734
OBJ_90 /* RelationshipsTest.swift in Sources */,
727735
);
728736
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)