-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCodableNumericBoolTests.swift
More file actions
92 lines (79 loc) · 2.38 KB
/
CodableNumericBoolTests.swift
File metadata and controls
92 lines (79 loc) · 2.38 KB
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
import XCTest
import CodableCSV
/// Check support for encoding/decoding numeric Booleans.
final class CodableNumericBoolTests: XCTestCase {
override func setUp() {
self.continueAfterFailure = false
}
}
extension CodableNumericBoolTests {
/// Representation of a CSV row exclusively composed of Booleans.
private struct _Row: Codable, Equatable {
var a: Bool
var b: Bool
var c: Bool?
}
}
extension CodableNumericBoolTests {
/// Tests the regular numeric Boolean encoding/decoding.
func testRegularUsage() throws {
let rows = [_Row(a: true, b: false, c: nil),
_Row(a: true, b: true, c: false),
_Row(a: false, b: false, c: true)]
let string = """
a,b,c
1,0,
1,1,0
0,0,1
""".appending("\n")
let encoder = CSVEncoder {
$0.headers = ["a", "b", "c"]
$0.boolStrategy = .numeric
}
XCTAssertEqual(string, try encoder.encode(rows, into: String.self))
let decoder = CSVDecoder {
$0.headerStrategy = .firstLine
$0.boolStrategy = .numeric
}
XCTAssertEqual(rows, try decoder.decode([_Row].self, from: string.data(using: .utf8)!))
let shuffledString = """
c,a,b
,1,0
0,1,1
1,0,0
""".appending("\n")
XCTAssertEqual(rows, try decoder.decode([_Row].self, from: shuffledString.data(using: .utf8)!))
}
/// Tests the quoteAll configuration option
func testQuoteAll() throws {
let rows = [_Row(a: true, b: false, c: nil),
_Row(a: true, b: true, c: false),
_Row(a: false, b: false, c: true)]
let string = """
"a","b","c"
"1","0",
"1","1","0"
"0","0","1"
""".appending("\n")
let encoder = CSVEncoder {
$0.headers = ["a", "b", "c"]
$0.boolStrategy = .numeric
$0.quoteAll = true
}
XCTAssertEqual(string, try encoder.encode(rows, into: String.self))
}
/// Tests the error throwing/handling.
func testThrows() throws {
// b = nil on 2nd row, must throw an exception.
let data = """
a,b,c
1,0,
1,,0
""".data(using: .utf8)!
let decoder = CSVDecoder {
$0.headerStrategy = .firstLine
$0.boolStrategy = .numeric
}
XCTAssertThrowsError(try decoder.decode([_Row].self, from: data))
}
}