-
Notifications
You must be signed in to change notification settings - Fork 104
/
NamespaceTest.swift
302 lines (265 loc) · 7.05 KB
/
NamespaceTest.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// NamespaceTest.swift
// XMLCoder
//
// Created by Max Desiatov on 27/02/2019.
//
import Foundation
import XCTest
@testable import XMLCoder
private let tableXML = """
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
""".data(using: .utf8)!
private struct Table: Codable, Equatable {
struct TR: Codable, Equatable {
let td: [String]
}
let tr: [TR]
}
private struct NamespacedTable: Codable, Equatable {
struct TR: Codable, Equatable {
enum CodingKeys: String, CodingKey {
case td = "h:td"
}
let td: [String]
}
enum CodingKeys: String, CodingKey {
case tr = "h:tr"
}
let tr: [TR]
}
private let worksheetXML = """
<?xml version="1.0" encoding="utf-8"?>
<x:worksheet \
xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:sheetData>
<x:row r="1">
<x:c t="str" r="A1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="B1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="C1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="D1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="E1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="F1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="G1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="H1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="I1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="J1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="K1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="L1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="M1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="N1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="O1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="P1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="Q1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="R1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="S1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="T1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="U1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="V1">
<x:v>Scolumn name</x:v>
</x:c>
<x:c t="str" r="W1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="X1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="Y1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="Z1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AA1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AB1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AC1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AD1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AE1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AF1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AG1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AH1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AI1">
<x:v>column name</x:v>
</x:c>
<x:c t="str" r="AJ1">
<x:v>column name</x:v>
</x:c>
</x:row>
<x:row r="2">
</x:row>
</x:sheetData>
</x:worksheet>
""".data(using: .utf8)!
private struct Cell: Codable, Equatable {
let type: String?
let s: String?
let formula: String?
let value: String?
enum CodingKeys: String, CodingKey {
case formula = "f"
case value = "v"
case type = "t"
case s
}
}
private struct Worksheet: Codable {
struct Data: Codable {
public let rows: [Row]
enum CodingKeys: String, CodingKey {
case rows = "row"
}
}
let data: Data?
enum CodingKeys: String, CodingKey {
case data = "sheetData"
}
}
private struct Row: Codable {
let reference: UInt
let height: Double?
let customHeight: String?
let cells: [Cell]
enum CodingKeys: String, CodingKey {
case cells = "c"
case reference = "r"
case height = "ht"
case customHeight
}
}
private struct NamespacedCell: Codable, Equatable {
let type: String?
let s: String?
let formula: String?
let value: String?
enum CodingKeys: String, CodingKey {
case formula = "f"
case value = "v"
case type = "t"
case s
}
}
private struct NamespacedWorksheet: Codable {
struct Data: Codable {
public let rows: [NamespacedRow]
enum CodingKeys: String, CodingKey {
case rows = "x:row"
}
}
let data: Data?
enum CodingKeys: String, CodingKey {
case data = "x:sheetData"
}
}
private struct NamespacedRow: Codable {
let reference: UInt
let height: Double?
let customHeight: String?
let cells: [NamespacedCell]
enum CodingKeys: String, CodingKey {
case cells = "x:c"
case reference = "r"
case height = "ht"
case customHeight
}
}
final class NamespaceTest: XCTestCase {
func testTable() throws {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
var decoded = try decoder.decode(Table.self, from: tableXML)
XCTAssertEqual(decoded, Table(tr: [.init(td: ["Apples", "Bananas"])]))
decoder.shouldProcessNamespaces = false
decoded = try decoder.decode(Table.self, from: tableXML)
XCTAssertEqual(decoded, Table(tr: []))
}
func testWorksheet() throws {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
var worksheet = try decoder.decode(Worksheet.self, from: worksheetXML)
XCTAssertEqual(worksheet.data?.rows[0].cells.count, 36)
decoder.shouldProcessNamespaces = false
worksheet = try decoder.decode(Worksheet.self, from: worksheetXML)
XCTAssertNil(worksheet.data)
}
func testTableWithoutNamespaces() throws {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = false
var decoded = try decoder.decode(NamespacedTable.self, from: tableXML)
XCTAssertEqual(decoded, NamespacedTable(tr: [.init(td: ["Apples", "Bananas"])]))
decoder.shouldProcessNamespaces = true
decoded = try decoder.decode(NamespacedTable.self, from: tableXML)
XCTAssertEqual(decoded, NamespacedTable(tr: []))
}
func testWorksheetWithoutNamespaces() throws {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = false
var worksheet = try decoder.decode(NamespacedWorksheet.self, from: worksheetXML)
XCTAssertEqual(worksheet.data?.rows[0].cells.count, 36)
decoder.shouldProcessNamespaces = true
worksheet = try decoder.decode(NamespacedWorksheet.self, from: worksheetXML)
XCTAssertNil(worksheet.data)
}
}