-
Notifications
You must be signed in to change notification settings - Fork 104
/
KeyedTests.swift
152 lines (122 loc) · 3.98 KB
/
KeyedTests.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
//
// KeyedTests.swift
// XMLCoderTests
//
// Created by Max Desiatov on 19/11/2018.
//
import XCTest
@testable import XMLCoder
class KeyedTests: XCTestCase {
struct Container: Codable, Equatable {
let value: [String: Int]
}
struct ContainerCamelCase: Codable, Equatable {
let valUe: [String: Int]
let testAttribute: String
}
struct AnyKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
intValue = nil
}
init?(intValue: Int) {
stringValue = String(intValue)
self.intValue = intValue
}
}
func testEmpty() throws {
let decoder = XMLDecoder()
let xmlString = "<container />"
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertEqual(decoded.value, [:])
}
func testSingleElement() throws {
let decoder = XMLDecoder()
let xmlString =
"""
<container>
<value>
<foo>12</foo>
</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertEqual(decoded.value, ["foo": 12])
}
func testMultiElement() throws {
let decoder = XMLDecoder()
let xmlString =
"""
<container>
<value>
<foo>12</foo>
<bar>34</bar>
</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertEqual(decoded.value, ["foo": 12, "bar": 34])
}
func testAttribute() {
let encoder = XMLEncoder()
encoder.nodeEncodingStrategy = .custom { _, _ in
{ _ in .attribute }
}
let container = Container(value: ["foo": 12, "bar": 34])
XCTAssertThrowsError(
try encoder.encode(container, withRootKey: "container")
)
}
func testConvertFromSnakeCase() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let xmlString =
"""
<cont_ainer test_attribute="test_container">
<val_ue>
<fo_o>12</fo_o>
</val_ue>
</cont_ainer>
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(ContainerCamelCase.self, from: xmlData)
XCTAssertEqual(decoded.valUe, ["foO": 12])
}
func testErrorDescriptionConvertFromSnakeCase() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let xmlString =
"""
<cont_aine test_attribut="test_container">
<val_u>
<fo_oo>12</fo_oo>
</val_u>
</cont_aine>
"""
let xmlData = xmlString.data(using: .utf8)!
XCTAssertThrowsError(try decoder.decode(ContainerCamelCase.self, from: xmlData))
}
func testCustomDecoderConvert() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .custom { keys in
let lastComponent = keys.last!.stringValue.split(separator: "_").last!
return AnyKey(stringValue: String(lastComponent))!
}
let xmlString =
"""
<container testAttribute="test_container">
<test_valUe>
<foo>12</foo>
</test_valUe>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(ContainerCamelCase.self, from: xmlData)
XCTAssertEqual(decoded.valUe, ["foo": 12])
}
}