-
Notifications
You must be signed in to change notification settings - Fork 104
/
BoolTests.swift
77 lines (60 loc) · 2.01 KB
/
BoolTests.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
//
// BoolTests.swift
// XMLCoderTests
//
// Created by Vincent Esche on 12/19/18.
//
import XCTest
@testable import XMLCoder
class BoolTests: XCTestCase {
typealias Value = Bool
struct Container: Codable, Equatable {
let value: Value
}
let values: [(Value, String)] = [
(false, "false"),
(true, "true"),
]
func testMissing() {
let decoder = XMLDecoder()
let xmlString = "<container />"
let xmlData = xmlString.data(using: .utf8)!
XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
func testAttribute() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()
encoder.nodeEncodingStrategy = .custom { _, _ in
{ _ in .attribute }
}
for (value, xmlString) in values {
let xmlString =
"""
<container value="\(xmlString)" />
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertEqual(decoded.value, 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]
for (value, xmlString) in values {
let xmlString =
"""
<container>
<value>\(xmlString)</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let decoded = try decoder.decode(Container.self, from: xmlData)
XCTAssertEqual(decoded.value, value)
let encoded = try encoder.encode(decoded, withRootKey: "container")
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}
}