-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathNullTests.swift
69 lines (54 loc) · 1.7 KB
/
NullTests.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
//
// NullTests.swift
// XMLCoderTests
//
// Created by Vincent Esche on 12/19/18.
//
import XCTest
@testable import XMLCoder
class NullTests: XCTestCase {
struct Container: Codable, Equatable {
let value: Int?
}
func testAttribute() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()
encoder.nodeEncodingStrategy = .custom { _, _ in
{ _ in .attribute }
}
let xmlString =
"""
<container />
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertNil(decoded.value)
let encoded = try encoder.encode(decoded, withRootKey: "container")
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
func testElement() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()
encoder.outputFormatting = [.prettyPrinted]
let xmlString =
"""
<container />
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertNil(decoded.value)
let encoded = try encoder.encode(decoded, withRootKey: "container")
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
func testNullElement() {
let decoder = XMLDecoder()
let xmlString =
"""
<container>
<value/>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}