@@ -10,16 +10,12 @@ class FoundationJSONDecoderTests: XCTestCase {
1010 }
1111 }
1212
13- do {
14- let json = #"{"hello":"world"}"#
15- let result = try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
16- XCTFail ( " Did not expect to get a result: \( result) " )
17- } catch Swift . DecodingError . typeMismatch( let type, let context) {
18- // expected
13+ let json = #"{"hello":"world"}"#
14+ XCTAssertThrowsError ( try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) { error in
15+ guard case Swift . DecodingError . typeMismatch( let type, _) = error else {
16+ return XCTFail ( " Unexpected error: \( error) " )
17+ }
1918 XCTAssertTrue ( type == [ Any ] . self)
20- print ( context)
21- } catch {
22- XCTFail ( " Unexpected error: \( error) " )
2319 }
2420 }
2521
@@ -35,16 +31,12 @@ class FoundationJSONDecoderTests: XCTestCase {
3531 }
3632 }
3733
38- do {
39- let json = #"["haha", "hihi"]"#
40- let result = try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
41- XCTFail ( " Did not expect to get a result: \( result) " )
42- } catch Swift . DecodingError . typeMismatch( let type, let context) {
43- // expected
34+ let json = #"["haha", "hihi"]"#
35+ XCTAssertThrowsError ( try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) { error in
36+ guard case Swift . DecodingError . typeMismatch( let type, _) = error else {
37+ return XCTFail ( " Unexpected error: \( error) " )
38+ }
4439 XCTAssertTrue ( type == [ String : Any ] . self)
45- print ( context)
46- } catch {
47- XCTFail ( " Unexpected error: \( error) " )
4840 }
4941 }
5042
@@ -62,16 +54,12 @@ class FoundationJSONDecoderTests: XCTestCase {
6254 }
6355 }
6456
65- do {
66- let json = #""haha""#
67- let result = try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
68- XCTFail ( " Did not expect to get a result: \( result) " )
69- } catch Swift . DecodingError . typeMismatch( let type, let context) {
70- // expected
57+ let json = #""haha""#
58+ XCTAssertThrowsError ( try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) { error in
59+ guard case Swift . DecodingError . typeMismatch( let type, _) = error else {
60+ return XCTFail ( " Unexpected error: \( error) " )
61+ }
7162 XCTAssertTrue ( type == [ String : Any ] . self)
72- print ( context)
73- } catch {
74- XCTFail ( " Unexpected error: \( error) " )
7563 }
7664 }
7765 #endif
@@ -89,16 +77,12 @@ class FoundationJSONDecoderTests: XCTestCase {
8977 }
9078 }
9179
92- do {
93- let json = #"{"hello": 12}"#
94- let result = try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
95- XCTFail ( " Did not expect to get a result: \( result) " )
96- } catch Swift . DecodingError . typeMismatch( let type, let context) {
97- // expected
80+ let json = #"{"hello": 12}"#
81+ XCTAssertThrowsError ( try JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) { error in
82+ guard case Swift . DecodingError . typeMismatch( let type, _) = error else {
83+ return XCTFail ( " Unexpected error: \( error) " )
84+ }
9885 XCTAssertTrue ( type == String . self)
99- print ( context)
100- } catch {
101- XCTFail ( " Unexpected error: \( error) " )
10286 }
10387 }
10488
@@ -115,16 +99,14 @@ class FoundationJSONDecoderTests: XCTestCase {
11599 }
116100 }
117101
118- do {
119- let json = " {} "
120- let result = try JSONDecoder ( ) . decode ( HelloWorld . self , from : json . data ( using : . utf8 ) ! )
121- XCTFail ( " Did not expect to get a result : \( result ) " )
122- } catch Swift . DecodingError . keyNotFound ( let codingKey , let context ) {
123- // expected
102+ let json = " {} "
103+ XCTAssertThrowsError ( try JSONDecoder ( ) . decode ( HelloWorld . self , from : json. data ( using : . utf8 ) ! ) ) { error in
104+ guard case Swift . DecodingError . keyNotFound ( let codingKey , let context ) = error else {
105+ return XCTFail ( " Unexpected error : \( error ) " )
106+ }
107+
124108 XCTAssertEqual ( codingKey as? HelloWorld . CodingKeys , . hello)
125109 XCTAssertEqual ( context. debugDescription, " No value associated with key CodingKeys(stringValue: \" hello \" , intValue: nil) ( \" hello \" ). " )
126- } catch {
127- XCTFail ( " Unexpected error: \( error) " )
128110 }
129111 }
130112
0 commit comments