-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConfigurationTests.swift
318 lines (242 loc) · 11.5 KB
/
ConfigurationTests.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// CBORReaderTests.swift
// PotentCodables
//
// Copyright © 2021 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//
import Foundation
import PotentJSON
import XCTest
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
class ConfigurationTests: XCTestCase {
struct LimitedString: CodableWithConfiguration {
typealias EncodingConfiguration = Int
typealias DecodingConfiguration = Int
var value: String
init(value: String) {
self.value = value
}
enum CodingKeys: String, CodingKey {
case value
}
init(from decoder: any Decoder, configuration: Int) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.value = try String(container.decode(String.self, forKey: .value).prefix(configuration))
}
func encode(to encoder: any Encoder, configuration: Int) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(String(value.prefix(configuration)), forKey: .value)
}
}
struct Max5: EncodingConfigurationProviding, DecodingConfigurationProviding {
static let encodingConfiguration: Int = 5
static let decodingConfiguration: Int = 5
}
struct TestAnnotation: Codable {
@CodableConfiguration(from: Max5.self)
var string = LimitedString(value: "")
}
struct TestManual: Codable {
var string = LimitedString(value: "")
var optionalString: LimitedString?
enum CodingKeys: CodingKey {
case string
case optionalString
}
init(string: LimitedString, optionalString: LimitedString?) {
self.string = string
self.optionalString = optionalString
}
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.string = try container
.decode(LimitedString.self, forKey: .string, configuration: 5)
self.optionalString = try container
.decodeIfPresent(LimitedString.self, forKey: .optionalString, configuration: 4)
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(string, forKey: .string, configuration: 5)
try container.encodeIfPresent(optionalString, forKey: .optionalString, configuration: 4)
}
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
public func testEncodeTopLevelConfiguration() throws {
let jsonTree = try JSON.Encoder.default.encodeTree(LimitedString(value: "12345678"), configuration: 5)
XCTAssertEqual(jsonTree, .object(["value": "12345"]))
let jsonData = try JSON.Encoder.default.encode(LimitedString(value: "12345678"), configuration: 5)
XCTAssertEqual(jsonData, Data(#"{"value":"12345"}"#.utf8))
let jsonString = try JSON.Encoder.default.encodeString(LimitedString(value: "12345678"), configuration: 5)
XCTAssertEqual(jsonString, #"{"value":"12345"}"#)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
public func testDecodeTopLevelConfiguration() throws {
let stringFromTree = try JSON.Decoder.default
.decodeTree(LimitedString.self, from: .object(["value": "12345678"]), configuration: 5)
XCTAssertEqual(stringFromTree.value, "12345")
let optStringFromTree = try JSON.Decoder.default
.decodeTreeIfPresent(LimitedString.self, from: .object(["value": "12345678"]), configuration: 5)
XCTAssertEqual(optStringFromTree?.value, "12345")
let nilStringFromTree = try JSON.Decoder.default
.decodeTreeIfPresent(LimitedString.self, from: .null, configuration: 5)
XCTAssertEqual(nilStringFromTree?.value, nil)
let stringFromData = try JSON.Decoder.default
.decode(LimitedString.self, from: Data(#"{"value":"12345678"}"#.utf8), configuration: 5)
XCTAssertEqual(stringFromData.value, "12345")
let optStringFromData = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: Data(#"{"value":"12345678"}"#.utf8), configuration: 5)
XCTAssertEqual(optStringFromData?.value, "12345")
let nilStringFromData = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: Data(#"null"#.utf8), configuration: 5)
XCTAssertEqual(nilStringFromData?.value, nil)
let stringFromString = try JSON.Decoder.default
.decode(LimitedString.self, from: #"{"value":"12345678"}"#, configuration: 5)
XCTAssertEqual(stringFromString.value, "12345")
let optStringFromString = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: #"{"value":"12345678"}"#, configuration: 5)
XCTAssertEqual(optStringFromString?.value, "12345")
let nilStringFromString = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: #"null"#, configuration: 5)
XCTAssertEqual(nilStringFromString?.value, nil)
}
public func testEncodeTopLevelConfigurationProvider() throws {
let jsonTree = try JSON.Encoder.default.encodeTree(LimitedString(value: "12345678"), configuration: Max5.self)
XCTAssertEqual(jsonTree, .object(["value": "12345"]))
let jsonData = try JSON.Encoder.default.encode(LimitedString(value: "12345678"), configuration: Max5.self)
XCTAssertEqual(jsonData, Data(#"{"value":"12345"}"#.utf8))
let jsonString = try JSON.Encoder.default.encodeString(LimitedString(value: "12345678"), configuration: Max5.self)
XCTAssertEqual(jsonString, #"{"value":"12345"}"#)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
public func testDecodeTopLevelConfigurationProvider() throws {
let stringFromTree = try JSON.Decoder.default
.decodeTree(LimitedString.self, from: .object(["value": "12345678"]), configuration: Max5.self)
XCTAssertEqual(stringFromTree.value, "12345")
let optStringFromTree = try JSON.Decoder.default
.decodeTreeIfPresent(LimitedString.self, from: .object(["value": "12345678"]), configuration: Max5.self)
XCTAssertEqual(optStringFromTree?.value, "12345")
let nilStringFromTree = try JSON.Decoder.default
.decodeTreeIfPresent(LimitedString.self, from: .null, configuration: 5)
XCTAssertEqual(nilStringFromTree?.value, nil)
let stringFromData = try JSON.Decoder.default
.decode(LimitedString.self, from: Data(#"{"value":"12345678"}"#.utf8), configuration: Max5.self)
XCTAssertEqual(stringFromData.value, "12345")
let optStringFromData = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: Data(#"{"value":"12345678"}"#.utf8), configuration: Max5.self)
XCTAssertEqual(optStringFromData?.value, "12345")
let nilStringFromData = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: Data(#"null"#.utf8), configuration: Max5.self)
XCTAssertEqual(nilStringFromData?.value, nil)
let stringFromString = try JSON.Decoder.default
.decode(LimitedString.self, from: #"{"value":"12345678"}"#, configuration: Max5.self)
XCTAssertEqual(stringFromString.value, "12345")
let optStringFromString = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: #"{"value":"12345678"}"#, configuration: Max5.self)
XCTAssertEqual(optStringFromString?.value, "12345")
let nilStringFromString = try JSON.Decoder.default
.decodeIfPresent(LimitedString.self, from: #"null"#, configuration: Max5.self)
XCTAssertEqual(nilStringFromString?.value, nil)
}
public func testEncodeConfiguredViaAnnotation() throws {
let json = try JSON.Encoder.default.encodeString(TestAnnotation(string: .init(value: "12345678")))
XCTAssertEqual(json, #"{"string":{"value":"12345"}}"#)
}
public func testDecodeConfiguredViaAnnotation() throws {
let test = try JSON.Decoder.default.decode(TestAnnotation.self, from: #"{"string":{"value":"12345678"}}"#)
XCTAssertEqual(test.string.value, "12345")
}
public func testEncodeConfiguredViaManualImplementatinon() throws {
let json = try JSON.Encoder.default
.encodeString(TestManual(string: .init(value: "12345678"), optionalString: nil))
XCTAssertEqual(json, #"{"string":{"value":"12345"}}"#)
let json2 = try JSON.Encoder.default
.encodeString(TestManual(string: .init(value: "12345678"), optionalString: .init(value: "123456")))
XCTAssertEqual(json2, #"{"string":{"value":"12345"},"optionalString":{"value":"1234"}}"#)
}
public func testDecodeConfiguredViaManualImplementatinon() throws {
let test = try JSON.Decoder.default
.decode(TestManual.self, from: #"{"string":{"value":"12345678"}}"#)
XCTAssertEqual(test.string.value, "12345")
XCTAssertEqual(test.optionalString?.value, nil)
let test2 = try JSON.Decoder.default
.decode(TestManual.self, from: #"{"string":{"value":"12345678"},"optionalString":{"value":"123456"}}"#)
XCTAssertEqual(test2.string.value, "12345")
XCTAssertEqual(test2.optionalString?.value, "1234")
}
struct InvalidValue: CodableWithConfiguration {
typealias EncodingConfiguration = Int
typealias DecodingConfiguration = Int
init() {}
init(from decoder: any Decoder, configuration: Int) throws {
}
func encode(to encoder: any Encoder, configuration: Int) throws {
}
}
struct IntCP: EncodingConfigurationProviding, DecodingConfigurationProviding {
static var encodingConfiguration: Int = 0
static var decodingConfiguration: Int = 0
}
func testEncodeInvalidTreeValue() throws {
XCTAssertThrowsError(try JSON.Encoder.default.encodeTree(InvalidValue(), configuration: 0)) { error in
XCTAssertTrue(error is EncodingError)
}
XCTAssertThrowsError(try JSON.Encoder.default.encodeTree(InvalidValue(), configuration: IntCP.self)) { error in
XCTAssertTrue(error is EncodingError)
}
}
func testEncodeInvalidDataValue() throws {
XCTAssertThrowsError(try JSON.Encoder.default.encode(InvalidValue(), configuration: 0)) { error in
XCTAssertTrue(error is EncodingError)
}
XCTAssertThrowsError(try JSON.Encoder.default.encode(InvalidValue(), configuration: IntCP.self)) { error in
XCTAssertTrue(error is EncodingError)
}
}
func testEncodeInvalidStringValue() throws {
XCTAssertThrowsError(try JSON.Encoder.default.encodeString(InvalidValue(), configuration: 0)) { error in
XCTAssertTrue(error is EncodingError)
}
XCTAssertThrowsError(try JSON.Encoder.default.encodeString(InvalidValue(), configuration: IntCP.self)) { error in
XCTAssertTrue(error is EncodingError)
}
}
func testDecodeMissingTreeValue() throws {
XCTAssertThrowsError(
try JSON.Decoder.default.decodeTree(InvalidValue.self, from: nil, configuration: 0)
) { error in
XCTAssertTrue(error is DecodingError)
}
XCTAssertThrowsError(
try JSON.Decoder.default.decodeTree(InvalidValue.self, from: nil, configuration: Max5.self)
) { error in
XCTAssertTrue(error is DecodingError)
}
}
func testDecodeMissingDataValue() throws {
XCTAssertThrowsError(
try JSON.Decoder.default.decode(InvalidValue.self, from: Data("null".utf8), configuration: 0)
) { error in
XCTAssertTrue(error is DecodingError)
}
XCTAssertThrowsError(
try JSON.Decoder.default.decode(InvalidValue.self, from: Data("null".utf8), configuration: Max5.self)
) { error in
XCTAssertTrue(error is DecodingError)
}
}
func testDecodeMissingStringValue() throws {
XCTAssertThrowsError(
try JSON.Decoder.default.decode(InvalidValue.self, from: "null", configuration: 0)
) { error in
XCTAssertTrue(error is DecodingError)
}
XCTAssertThrowsError(
try JSON.Decoder.default.decode(InvalidValue.self, from: "null", configuration: Max5.self)
) { error in
XCTAssertTrue(error is DecodingError)
}
}
}