This repository was archived by the owner on Oct 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathAnyCodableTests.swift
More file actions
130 lines (113 loc) · 4.04 KB
/
AnyCodableTests.swift
File metadata and controls
130 lines (113 loc) · 4.04 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
@testable import AnyCodable
import XCTest
class AnyCodableTests: XCTestCase {
struct SomeCodable: Codable {
var string: String
var int: Int
var bool: Bool
var hasUnderscore: String
enum CodingKeys: String,CodingKey {
case string
case int
case bool
case hasUnderscore = "has_underscore"
}
}
func testJSONDecoding() throws {
let json = """
{
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
},
"null": null
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let dictionary = try decoder.decode([String: AnyCodable].self, from: json)
XCTAssertEqual(dictionary["boolean"]?.value as? Bool, true)
XCTAssertEqual(dictionary["integer"]?.value as? Int, 42)
XCTAssertEqual(try XCTUnwrap(dictionary["double"]?.value as? Double), 3.141592653589793, accuracy: 0.001)
XCTAssertEqual(dictionary["string"]?.value as? String, "string")
XCTAssertEqual(dictionary["array"]?.value as? [Int], [1, 2, 3])
XCTAssertEqual(dictionary["nested"]?.value as? [String: String], ["a": "alpha", "b": "bravo", "c": "charlie"])
XCTAssertEqual(dictionary["null"]?.value as? NSNull, NSNull())
}
func testJSONDecodingEquatable() throws {
let json = """
{
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
},
"null": null
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let dictionary1 = try decoder.decode([String: AnyCodable].self, from: json)
let dictionary2 = try decoder.decode([String: AnyCodable].self, from: json)
XCTAssertEqual(dictionary1["boolean"], dictionary2["boolean"])
XCTAssertEqual(dictionary1["integer"], dictionary2["integer"])
XCTAssertEqual(dictionary1["double"], dictionary2["double"])
XCTAssertEqual(dictionary1["string"], dictionary2["string"])
XCTAssertEqual(dictionary1["array"], dictionary2["array"])
XCTAssertEqual(dictionary1["nested"], dictionary2["nested"])
XCTAssertEqual(dictionary1["null"], dictionary2["null"])
}
func testJSONEncoding() throws {
let someCodable = AnyCodable(SomeCodable(string: "String", int: 100, bool: true, hasUnderscore: "another string"))
let injectedValue = 1234
let dictionary: [String: AnyCodable] = [
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"stringInterpolation": "string \(injectedValue)",
"array": [1, 2, 3],
"nested": [
"a": "alpha",
"b": "bravo",
"c": "charlie",
],
"someCodable": someCodable,
"null": nil
]
let encoder = JSONEncoder()
let json = try encoder.encode(dictionary)
let expected = """
{
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"stringInterpolation": "string 1234",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
},
"someCodable": {
"string":"String",
"int":100,
"bool": true,
"has_underscore":"another string"
},
"null": null
}
"""
try XCTAssertJsonAreIdentical(json, expected)
}
}