-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathDefaultCodableTests.swift
More file actions
204 lines (164 loc) · 5.79 KB
/
Copy pathDefaultCodableTests.swift
File metadata and controls
204 lines (164 loc) · 5.79 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
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
import XCTest
import BetterCodable
// MARK: - DefaultCodable
// MARK: -
// MARK: Date Decoding Strategy
private extension Date {
enum DefaultToNow: DefaultCodableStrategy {
static var defaultValue: Date { Date() }
}
}
private let iso8601: DateFormatter = {
let iso8601 = DateFormatter()
iso8601.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
iso8601.timeZone = TimeZone(secondsFromGMT: 0)
iso8601.locale = Locale(identifier: "en_US_POSIX")
return iso8601
}()
private extension JSONDecoder {
static var iso: JSONDecoder {
let encoder = JSONDecoder()
encoder.dateDecodingStrategy = .formatted(iso8601)
return encoder
}
}
private extension JSONEncoder {
static var iso: JSONEncoder {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(iso8601)
return encoder
}
}
class DefaultCodableTest_DateStrategy: XCTestCase {
struct Fixture: Equatable, Codable {
@DefaultCodable<Date.DefaultToNow>
fileprivate var discoverDate: Date
}
func testDecodingAndEncodingWithDateStrategy() throws {
let expectedDate = Date(timeIntervalSinceReferenceDate: 222601260)
let jsonData = #"{ "discoverDate": "2008-01-21T09:41:00.000Z" }"#.data(using: .utf8)!
let fixture = try JSONDecoder.iso.decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.discoverDate, expectedDate)
let data = try JSONEncoder.iso.encode(fixture)
let str = String(data: data, encoding: .utf8)
XCTAssertEqual(str, #"{"discoverDate":"2008-01-21T09:41:00.000Z"}"#)
}
}
// MARK: -
// MARK: Nested Property Wrapper
class DefaultCodableTest_NestedPropertyWrapper: XCTestCase {
enum DefaultToNowTimeStampDateValue: DefaultCodableStrategy {
static var defaultValue: DateValue<TimestampStrategy> {
.init(wrappedValue: Date(timeIntervalSince1970: 0))
}
}
struct Fixture: Codable {
@DefaultCodable<DefaultToNowTimeStampDateValue>
@DateValue<TimestampStrategy>
var returnDate: Date
}
func testNestedPropertyWrappersCanMergeDefaultCodableWithDateStrategy() throws {
let _1970 = Date(timeIntervalSince1970: 0)
let _1971 = Date(timeIntervalSince1970: 31536000)
let jsonData1 = #"{ "returnDate": null }"#.data(using: .utf8)!
let jsonData2 = #"{ }"#.data(using: .utf8)!
let jsonData3 = #"{ "returnDate": 31536000 }"#.data(using: .utf8)!
let fixture1 = try JSONDecoder().decode(Fixture.self, from: jsonData1)
let fixture2 = try JSONDecoder().decode(Fixture.self, from: jsonData2)
let fixture3 = try JSONDecoder().decode(Fixture.self, from: jsonData3)
XCTAssertEqual(fixture1.returnDate, _1970)
XCTAssertEqual(fixture2.returnDate, _1970)
XCTAssertEqual(fixture3.returnDate, _1971)
}
}
// MARK: -
// MARK: Types with Containers
class DefaultCodableTests_TypesWithContainers: XCTestCase {
struct ArrayContainer: Codable {
var value: [Int]
}
enum DefaultArrayContainerType: DefaultCodableStrategy {
static var defaultValue: ArrayContainer { .init(value: [3, 7]) }
}
struct DictionaryContainer: Codable {
var value: [String: Int]
}
enum DefaultDictionaryContainerType: DefaultCodableStrategy {
static var defaultValue: DictionaryContainer { .init(value: ["a": 1]) }
}
struct Fixture: Codable {
@DefaultCodable<DefaultArrayContainerType>
public var type: ArrayContainer
}
struct Fixture2: Codable {
@DefaultCodable<DefaultDictionaryContainerType>
public var type: DictionaryContainer
}
func testDecodingAndEncodingWithArrayContainer() throws {
let jsonData = #"{ "type": { "value": [2, 4, 6] } }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.type.value, [2, 4, 6])
let data = try JSONEncoder().encode(fixture)
let str = String(data: data, encoding: .utf8)
XCTAssertEqual(str, #"{"type":{"value":[2,4,6]}}"#)
}
func testDecodingAndEncodingWithDictionaryContainer() throws {
let jsonData = #"{ "type": { "value": {"b": 17 } } }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture2.self, from: jsonData)
XCTAssertEqual(fixture.type.value, ["b": 17])
let data = try JSONEncoder().encode(fixture)
let str = String(data: data, encoding: .utf8)
XCTAssertEqual(str, #"{"type":{"value":{"b":17}}}"#)
}
}
// MARK: -
// MARK: Enums with Associated Values
class DefaultCodableTests_EnumWithAssociatedValue: XCTestCase {
enum Zar: Equatable {
case ziz(Int)
case zaz(Int)
}
struct CustomType: Codable, Equatable {
enum CodingKeys: String, CodingKey {
case z = "fish"
case i = "int"
}
var z: Zar
init(z: Zar) {
self.z = z
}
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
let k = try c.decode(String.self, forKey: .z)
let i = try c.decode(Int.self, forKey: .i)
if k == "ziz" { z = .ziz(i) }
else { z = .zaz(i) }
}
func encode(to encoder: Encoder) throws {
var c = encoder.container(keyedBy: CodingKeys.self)
switch z {
case .ziz(let i):
try c.encode("ziz", forKey: .z)
try c.encode(i, forKey: .i)
case .zaz(let i):
try c.encode("zaz", forKey: .z)
try c.encode(i, forKey: .i)
}
}
enum Default42: DefaultCodableStrategy {
static var defaultValue: CustomType { .init(z: .zaz(42)) }
}
}
struct Fixture: Equatable, Codable {
@DefaultCodable<CustomType.Default42>
public var value: CustomType
}
func testDecodingAndEncodingCustomEnumWithAssociatedValue() throws {
let jsonData = #"{ "value": { "fish": "ziz", "int": 4 } }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.value.z, .ziz(4))
let data = try JSONEncoder().encode(fixture)
let str = String(data: data, encoding: .utf8)
XCTAssertEqual(str, #"{"value":{"fish":"ziz","int":4}}"#)
}
}