forked from CoreOffice/XMLCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicNodeDecodingTest.swift
220 lines (191 loc) · 5.77 KB
/
DynamicNodeDecodingTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// DynamicNodeEncodingTest.swift
// XMLCoderTests
//
// Created by Joseph Mattiello on 1/23/19.
//
import Foundation
import XCTest
@testable import XMLCoder
private let overlappingKeys = """
<?xml version="1.0" encoding="UTF-8"?>
<test key="123">
<key>
StringValue
</key>
</test>
""".data(using: .utf8)!
private let libraryXMLYN = """
<?xml version="1.0" encoding="UTF-8"?>
<library count="2">
<book id="123" author="Jack" gender="novel">
<id>123</id>
<author>Jack</author>
<gender>novel</gender>
<title>Cat in the Hat</title>
<category main="Y"><value>Kids</value></category>
<category main="N"><value>Wildlife</value></category>
</book>
<book id="456" author="Susan" gender="fantastic">
<id>456</id>
<author>Susan</author>
<gender>fantastic</gender>
<title>1984</title>
<category main="Y"><value>Classics</value></category>
<category main="N"><value>News</value></category>
</book>
</library>
""".data(using: .utf8)!
private let libraryXMLYNStrategy = """
<?xml version="1.0" encoding="UTF-8"?>
<library>
<count>2</count>
<book title="Cat in the Hat">
<id>123</id>
<author>Jack</author>
<gender>novel</gender>
<category>
<main>true</main>
<value>Kids</value>
</category>
<category>
<main>false</main>
<value>Wildlife</value>
</category>
</book>
<book title="1984">
<id>456</id>
<author>Susan</author>
<gender>fantastic</gender>
<category>
<main>true</main>
<value>Classics</value>
</category>
<category>
<main>false</main>
<value>News</value>
</category>
</book>
</library>
""".data(using: .utf8)!
private struct TestStruct: Codable, Equatable, DynamicNodeDecoding {
let attribute: Int
let element: String
private enum CodingKeys: CodingKey {
case attribute
case element
public var stringValue: String {
return "key"
}
}
static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
switch key {
case CodingKeys.attribute:
return .attribute
default:
return .element
}
}
}
private struct Library: Codable, Equatable, DynamicNodeDecoding {
let count: Int
let books: [Book]
enum CodingKeys: String, CodingKey {
case count
case books = "book"
}
static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
switch key {
case CodingKeys.count:
return .attribute
default:
return .element
}
}
}
private struct Book: Codable, Equatable, DynamicNodeEncoding {
let id: UInt
let author: String
let gender: String
let title: String
let categories: [Category]
enum CodingKeys: String, CodingKey {
case id
case author
case gender
case title
case categories = "category"
}
static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case Book.CodingKeys.id, Book.CodingKeys.author, Book.CodingKeys.gender: return .both
default: return .element
}
}
}
private struct Category: Codable, Equatable, DynamicNodeEncoding {
let main: Bool
let value: String
private enum CodingKeys: String, CodingKey {
case main
case value
}
static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case Category.CodingKeys.main:
return .attribute
default:
return .element
}
}
}
final class DynamicNodeDecodingTest: XCTestCase {
func testDecode() throws {
let decoder = XMLDecoder()
decoder.errorContextLength = 10
let library = try decoder.decode(Library.self, from: libraryXMLYN)
XCTAssertEqual(library.books.count, 2)
XCTAssertEqual(library.count, 2)
let book1 = library.books[0]
XCTAssertEqual(book1.id, 123)
XCTAssertEqual(book1.title, "Cat in the Hat")
let book1Categories = book1.categories
XCTAssertEqual(book1Categories.count, 2)
XCTAssertEqual(book1Categories[0].value, "Kids")
XCTAssertTrue(book1Categories[0].main)
XCTAssertEqual(book1Categories[1].value, "Wildlife")
XCTAssertFalse(book1Categories[1].main)
let book2 = library.books[1]
XCTAssertEqual(book2.id, 456)
XCTAssertEqual(book2.title, "1984")
let book2Categories = book2.categories
XCTAssertEqual(book2Categories.count, 2)
XCTAssertEqual(book2Categories[0].value, "Classics")
XCTAssertTrue(book2Categories[0].main)
XCTAssertEqual(book2Categories[1].value, "News")
XCTAssertFalse(book2Categories[1].main)
}
func testStrategyPriority() throws {
let decoder = XMLDecoder()
decoder.errorContextLength = 10
decoder.nodeDecodingStrategy = .custom { type, _ in
{ key in
guard
type == Book.self &&
key.stringValue == Book.CodingKeys.title.stringValue
else {
return .element
}
return .attribute
}
}
let library = try decoder.decode(Library.self, from: libraryXMLYNStrategy)
XCTAssertEqual(library.count, 2)
}
func testOverlappingKeys() throws {
let decoder = XMLDecoder()
decoder.errorContextLength = 10
let test = try decoder.decode(TestStruct.self, from: overlappingKeys)
XCTAssertEqual(test, TestStruct(attribute: 123, element: "StringValue"))
}
}