@@ -8,7 +8,7 @@ final class DecodingTests: XCTestCase {
8
8
case value
9
9
}
10
10
11
- struct User {
11
+ struct User : Equatable {
12
12
var name : String
13
13
var age : Int
14
14
var city : String ?
@@ -96,6 +96,72 @@ final class DecodingTests: XCTestCase {
96
96
XCTAssertEqual ( " Unknown " , user. city)
97
97
}
98
98
99
+ // MARK: - Decoding collections
100
+
101
+ func testDecodingArrayOfSimpleValues( ) throws {
102
+ let json = """
103
+ [
104
+ " one " ,
105
+ " two " ,
106
+ " three "
107
+ ]
108
+ """
109
+
110
+ let strings = try decoder. decode (
111
+ json. data ( using: . utf8) !,
112
+ as: Decoding< String> . array
113
+ )
114
+
115
+ XCTAssertEqual ( [ " one " , " two " , " three " ] , strings)
116
+
117
+ let uppercased = try decoder. decode (
118
+ json. data ( using: . utf8) !,
119
+ as: Decoding< String> . arrayOf( . singleValue. map { $0. uppercased ( ) } )
120
+ )
121
+
122
+ XCTAssertEqual ( [ " ONE " , " TWO " , " THREE " ] , uppercased)
123
+ }
124
+
125
+ func testDecodingArrayOfComplexValues( ) throws {
126
+ let json = """
127
+ [
128
+ {
129
+ " name " : " Joe Bloggs " ,
130
+ " age " : 18
131
+ },
132
+ {
133
+ " name " : " Jane Doe " ,
134
+ " age " : 21,
135
+ " city " : " London "
136
+ }
137
+ ]
138
+ """
139
+
140
+ let name = Decoding< String>
141
+ . withKey( User . CodingKeys. name)
142
+
143
+ let age = Decoding< Int>
144
+ . withKey( User . CodingKeys. age)
145
+
146
+ let city = Decoding< String>
147
+ . optionalWithKey( User . CodingKeys. city)
148
+
149
+ let user = zip ( with: User . init) ( name, age, city)
150
+
151
+ let users = try decoder. decode (
152
+ json. data ( using: . utf8) !,
153
+ as: Decoding< User> . arrayOf( user)
154
+ )
155
+
156
+ XCTAssertEqual (
157
+ users,
158
+ [
159
+ User ( name: " Joe Bloggs " , age: 18 , city: nil ) ,
160
+ User ( name: " Jane Doe " , age: 21 , city: " London " )
161
+ ]
162
+ )
163
+ }
164
+
99
165
// MARK: - Built-in decodings
100
166
101
167
func testDecoding_UInt16( ) throws {
0 commit comments