@@ -10,16 +10,12 @@ class FoundationJSONDecoderTests: XCTestCase {
10
10
}
11
11
}
12
12
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
+ }
19
18
XCTAssertTrue ( type == [ Any ] . self)
20
- print ( context)
21
- } catch {
22
- XCTFail ( " Unexpected error: \( error) " )
23
19
}
24
20
}
25
21
@@ -35,16 +31,12 @@ class FoundationJSONDecoderTests: XCTestCase {
35
31
}
36
32
}
37
33
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
+ }
44
39
XCTAssertTrue ( type == [ String : Any ] . self)
45
- print ( context)
46
- } catch {
47
- XCTFail ( " Unexpected error: \( error) " )
48
40
}
49
41
}
50
42
@@ -62,16 +54,12 @@ class FoundationJSONDecoderTests: XCTestCase {
62
54
}
63
55
}
64
56
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
+ }
71
62
XCTAssertTrue ( type == [ String : Any ] . self)
72
- print ( context)
73
- } catch {
74
- XCTFail ( " Unexpected error: \( error) " )
75
63
}
76
64
}
77
65
#endif
@@ -89,16 +77,12 @@ class FoundationJSONDecoderTests: XCTestCase {
89
77
}
90
78
}
91
79
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
+ }
98
85
XCTAssertTrue ( type == String . self)
99
- print ( context)
100
- } catch {
101
- XCTFail ( " Unexpected error: \( error) " )
102
86
}
103
87
}
104
88
@@ -115,16 +99,14 @@ class FoundationJSONDecoderTests: XCTestCase {
115
99
}
116
100
}
117
101
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
+
124
108
XCTAssertEqual ( codingKey as? HelloWorld . CodingKeys , . hello)
125
109
XCTAssertEqual ( context. debugDescription, " No value associated with key CodingKeys(stringValue: \" hello \" , intValue: nil) ( \" hello \" ). " )
126
- } catch {
127
- XCTFail ( " Unexpected error: \( error) " )
128
110
}
129
111
}
130
112
0 commit comments