@@ -22,17 +22,18 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
22
22
}
23
23
}
24
24
25
- func testContains( ) throws {
25
+ func testContains( ) {
26
26
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . object( [ " hello " : . null, " world " : . null] ) , codingPath: [ ] )
27
27
28
28
enum CodingKeys : String , CodingKey {
29
29
case hello
30
30
case haha
31
31
}
32
32
33
- let container = try impl. container ( keyedBy: CodingKeys . self)
34
- XCTAssertEqual ( container. contains ( . hello) , true )
35
- XCTAssertEqual ( container. contains ( . haha) , false )
33
+ var container : KeyedDecodingContainer < CodingKeys > ?
34
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
35
+ XCTAssertEqual ( try XCTUnwrap ( container) . contains ( . hello) , true )
36
+ XCTAssertEqual ( try XCTUnwrap ( container) . contains ( . haha) , false )
36
37
}
37
38
38
39
// MARK: - Null -
@@ -43,19 +44,15 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
43
44
case hello
44
45
}
45
46
46
- do {
47
- let container = try impl. container ( keyedBy: CodingKeys . self)
48
- let result = try container. decodeNil ( forKey: . hello)
49
- XCTFail ( " Did not expect to get a result: \( result) " )
50
- }
51
- catch Swift . DecodingError . keyNotFound( let codingKey, let context) {
52
- // expected
47
+ var container : KeyedDecodingContainer < CodingKeys > ?
48
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
49
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decodeNil ( forKey: . hello) ) { ( error) in
50
+ guard case Swift . DecodingError . keyNotFound( let codingKey, let context) = error else {
51
+ XCTFail ( " Unexpected error: \( error) " ) ; return
52
+ }
53
53
XCTAssertEqual ( codingKey as? CodingKeys , . hello)
54
54
XCTAssertEqual ( context. debugDescription, " No value associated with key CodingKeys(stringValue: \" hello \" , intValue: nil) ( \" hello \" ). " )
55
55
}
56
- catch {
57
- XCTFail ( " Unexpected error: \( error) " )
58
- }
59
56
}
60
57
61
58
func testDecodeNull( ) {
@@ -64,14 +61,11 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
64
61
case hello
65
62
}
66
63
67
- do {
68
- let container = try impl. container ( keyedBy: CodingKeys . self)
69
- let result = try container. decodeNil ( forKey: . hello)
70
- XCTAssertEqual ( result, true )
71
- }
72
- catch {
73
- XCTFail ( " Unexpected error: \( error) " )
74
- }
64
+ var container : KeyedDecodingContainer < CodingKeys > ?
65
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
66
+ var result : Bool ?
67
+ XCTAssertNoThrow ( result = try XCTUnwrap ( container) . decodeNil ( forKey: . hello) )
68
+ XCTAssertEqual ( result, true )
75
69
}
76
70
77
71
func testDecodeNullFromArray( ) {
@@ -80,14 +74,11 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
80
74
case hello
81
75
}
82
76
83
- do {
84
- let container = try impl. container ( keyedBy: CodingKeys . self)
85
- let result = try container. decodeNil ( forKey: . hello)
86
- XCTAssertEqual ( result, false )
87
- }
88
- catch {
89
- XCTFail ( " Unexpected error: \( error) " )
90
- }
77
+ var container : KeyedDecodingContainer < CodingKeys > ?
78
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
79
+ var result : Bool ?
80
+ XCTAssertNoThrow ( result = try XCTUnwrap ( container) . decodeNil ( forKey: . hello) )
81
+ XCTAssertEqual ( result, false )
91
82
}
92
83
93
84
// MARK: - String -
@@ -98,21 +89,17 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
98
89
case hello
99
90
}
100
91
101
- do {
102
- let container = try impl. container ( keyedBy: CodingKeys . self)
103
- let result = try container. decode ( String . self, forKey: . hello)
104
- XCTFail ( " Did not expect to get a result: \( result) " )
105
- }
106
- catch Swift . DecodingError . typeMismatch( let type, let context) {
107
- // expected
92
+ var container : KeyedDecodingContainer < CodingKeys > ?
93
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
94
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( String . self, forKey: . hello) ) { ( error) in
95
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
96
+ XCTFail ( " Unexpected error: \( error) " ) ; return
97
+ }
108
98
XCTAssertTrue ( type == String . self)
109
99
XCTAssertEqual ( context. codingPath. count, 1 )
110
100
XCTAssertEqual ( context. codingPath. first as? CodingKeys , CodingKeys . hello)
111
101
XCTAssertEqual ( context. debugDescription, " Expected to decode String but found a number instead. " )
112
102
}
113
- catch {
114
- XCTFail ( " Unexpected error: \( error) " )
115
- }
116
103
}
117
104
118
105
func testDecodeStringFromKeyNotFound( ) {
@@ -121,19 +108,15 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
121
108
case hello
122
109
}
123
110
124
- do {
125
- let container = try impl. container ( keyedBy: CodingKeys . self)
126
- let result = try container. decode ( String . self, forKey: . hello)
127
- XCTFail ( " Did not expect to get a result: \( result) " )
128
- }
129
- catch Swift . DecodingError . keyNotFound( let codingKey, let context) {
130
- // expected
111
+ var container : KeyedDecodingContainer < CodingKeys > ?
112
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
113
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( String . self, forKey: . hello) ) { ( error) in
114
+ guard case Swift . DecodingError . keyNotFound( let codingKey, let context) = error else {
115
+ XCTFail ( " Unexpected error: \( error) " ) ; return
116
+ }
131
117
XCTAssertEqual ( codingKey as? CodingKeys , . hello)
132
118
XCTAssertEqual ( context. debugDescription, " No value associated with key CodingKeys(stringValue: \" hello \" , intValue: nil) ( \" hello \" ). " )
133
119
}
134
- catch {
135
- XCTFail ( " Unexpected error: \( error) " )
136
- }
137
120
}
138
121
139
122
// MARK: - Bool -
@@ -144,21 +127,17 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
144
127
case hello
145
128
}
146
129
147
- do {
148
- let container = try impl. container ( keyedBy: CodingKeys . self)
149
- let result = try container. decode ( Bool . self, forKey: . hello)
150
- XCTFail ( " Did not expect to get a result: \( result) " )
151
- }
152
- catch Swift . DecodingError . typeMismatch( let type, let context) {
153
- // expected
130
+ var container : KeyedDecodingContainer < CodingKeys > ?
131
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
132
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( Bool . self, forKey: . hello) ) { ( error) in
133
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
134
+ XCTFail ( " Unexpected error: \( error) " ) ; return
135
+ }
154
136
XCTAssertTrue ( type == Bool . self)
155
137
XCTAssertEqual ( context. codingPath. count, 1 )
156
138
XCTAssertEqual ( context. codingPath. first as? CodingKeys , CodingKeys . hello)
157
139
XCTAssertEqual ( context. debugDescription, " Expected to decode Bool but found a number instead. " )
158
140
}
159
- catch {
160
- XCTFail ( " Unexpected error: \( error) " )
161
- }
162
141
}
163
142
164
143
// MARK: - Integers -
@@ -170,46 +149,36 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
170
149
case hello
171
150
}
172
151
173
- do {
174
- let container = try impl. container ( keyedBy: CodingKeys . self)
175
- let result = try container. decode ( UInt8 . self, forKey: . hello)
176
- XCTFail ( " Did not expect to get a result: \( result) " )
177
- }
178
- catch Swift . DecodingError . dataCorrupted( let context) {
179
- // expected
152
+ var container : KeyedDecodingContainer < CodingKeys > ?
153
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
154
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( UInt8 . self, forKey: . hello) ) { ( error) in
155
+ guard case Swift . DecodingError . dataCorrupted( let context) = error else {
156
+ XCTFail ( " Unexpected error: \( error) " ) ; return
157
+ }
180
158
XCTAssertEqual ( context. codingPath. count, 1 )
181
159
XCTAssertEqual ( context. codingPath. first as? CodingKeys , . hello)
182
160
XCTAssertEqual ( context. debugDescription, " Parsed JSON number < \( number) > does not fit in UInt8. " )
183
161
}
184
- catch {
185
- XCTFail ( " Unexpected error: \( error) " )
186
- }
187
162
}
188
163
189
164
func testGetUInt8FromFloat( ) {
190
165
let number = - 3.14
191
166
let type = UInt8 . self
192
-
193
167
enum CodingKeys : String , CodingKey {
194
168
case hello
195
169
}
196
170
197
171
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . object( [ " hello " : . number( " \( number) " ) ] ) , codingPath: [ ] )
198
-
199
- do {
200
- let container = try impl. container ( keyedBy: CodingKeys . self)
201
- let result = try container. decode ( type. self, forKey: . hello)
202
- XCTFail ( " Did not expect to get a result: \( result) " )
203
- }
204
- catch Swift . DecodingError . dataCorrupted( let context) {
205
- // expected
172
+ var container : KeyedDecodingContainer < CodingKeys > ?
173
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
174
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( type, forKey: . hello) ) { ( error) in
175
+ guard case Swift . DecodingError . dataCorrupted( let context) = error else {
176
+ XCTFail ( " Unexpected error: \( error) " ) ; return
177
+ }
206
178
XCTAssertEqual ( context. codingPath. count, 1 )
207
179
XCTAssertEqual ( context. codingPath. first as? CodingKeys , . hello)
208
180
XCTAssertEqual ( context. debugDescription, " Parsed JSON number < \( number) > does not fit in UInt8. " )
209
181
}
210
- catch {
211
- XCTFail ( " Unexpected error: \( error) " )
212
- }
213
182
}
214
183
215
184
func testGetUInt8TypeMismatch( ) {
@@ -220,22 +189,17 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
220
189
}
221
190
222
191
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . object( [ " hello " : . bool( false ) ] ) , codingPath: [ ] )
223
-
224
- do {
225
- let container = try impl. container ( keyedBy: CodingKeys . self)
226
- let result = try container. decode ( type. self, forKey: . hello)
227
- XCTFail ( " Did not expect to get a result: \( result) " )
228
- }
229
- catch Swift . DecodingError . typeMismatch( let type, let context) {
230
- // expected
192
+ var container : KeyedDecodingContainer < CodingKeys > ?
193
+ XCTAssertNoThrow ( container = try impl. container ( keyedBy: CodingKeys . self) )
194
+ XCTAssertThrowsError ( try XCTUnwrap ( container) . decode ( type, forKey: . hello) ) { ( error) in
195
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
196
+ XCTFail ( " Unexpected error: \( error) " ) ; return
197
+ }
231
198
XCTAssertTrue ( type == UInt8 . self)
232
199
XCTAssertEqual ( context. codingPath. count, 1 )
233
200
XCTAssertEqual ( context. codingPath. first as? CodingKeys , . hello)
234
201
XCTAssertEqual ( context. debugDescription, " Expected to decode UInt8 but found bool instead. " )
235
202
}
236
- catch {
237
- XCTFail ( " Unexpected error: \( error) " )
238
- }
239
203
}
240
204
241
205
func testGetUInt8Success( ) {
@@ -551,4 +515,30 @@ class JSONKeyedDecodingContainerTests: XCTestCase {
551
515
XCTFail ( " Unexpected error: \( error) " )
552
516
}
553
517
}
518
+
519
+ func testCantCreateSubDecoderForKey( ) {
520
+ struct Test : Decodable {
521
+ struct SubTest : Decodable {
522
+ let hello : String
523
+ }
524
+
525
+ let sub : SubTest
526
+
527
+ enum CodingKeys : String , CodingKey {
528
+ case sub
529
+ }
530
+ }
531
+
532
+ let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . object( [ " hello " : . bool( false ) ] ) , codingPath: [ ] )
533
+
534
+ XCTAssertThrowsError ( _ = try Test ( from: impl) ) { ( error) in
535
+ guard case Swift . DecodingError . keyNotFound( let codingKey, let context) = error else {
536
+ XCTFail ( " Unexpected error: \( error) " ) ; return
537
+ }
538
+ XCTAssertEqual ( codingKey as? Test . CodingKeys , . sub)
539
+ XCTAssertEqual ( context. debugDescription, " No value associated with key CodingKeys(stringValue: \" sub \" , intValue: nil) ( \" sub \" ). " )
540
+ }
541
+
542
+ }
543
+
554
544
}
0 commit comments