@@ -52,16 +52,15 @@ class JSONDecoderTests: XCTestCase {
52
52
}
53
53
}
54
54
55
- do {
56
- let json = #"{"hello":"world"}"#
57
- let result = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
58
- XCTFail ( " Did not expect to get a result: \( result) " )
59
- } catch let Swift . DecodingError . typeMismatch( type, context) {
60
- // expected
55
+ let json = #"{"hello":"world"}"#
56
+ XCTAssertThrowsError ( _ = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) {
57
+ error in
58
+ guard case let Swift . DecodingError . typeMismatch( type, context) = error else {
59
+ XCTFail ( " Unexpected error: \( error) " ) ; return
60
+ }
61
+
61
62
XCTAssertTrue ( type == [ JSONValue ] . self)
62
63
XCTAssertEqual ( context. debugDescription, " Expected to decode Array<JSONValue> but found a dictionary instead. " )
63
- } catch {
64
- XCTFail ( " Unexpected error: \( error) " )
65
64
}
66
65
}
67
66
@@ -77,16 +76,15 @@ class JSONDecoderTests: XCTestCase {
77
76
}
78
77
}
79
78
80
- do {
81
- let json = #"["haha", "hihi"]"#
82
- let result = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
83
- XCTFail ( " Did not expect to get a result: \( result) " )
84
- } catch let Swift . DecodingError . typeMismatch( type, context) {
85
- // expected
79
+ let json = #"["haha", "hihi"]"#
80
+ XCTAssertThrowsError ( _ = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) {
81
+ error in
82
+ guard case let Swift . DecodingError . typeMismatch( type, context) = error else {
83
+ XCTFail ( " Unexpected error: \( error) " ) ; return
84
+ }
85
+
86
86
XCTAssertTrue ( type == [ String : JSONValue ] . self)
87
87
XCTAssertEqual ( context. debugDescription, " Expected to decode Dictionary<String, JSONValue> but found an array instead. " )
88
- } catch {
89
- XCTFail ( " Unexpected error: \( error) " )
90
88
}
91
89
}
92
90
@@ -101,17 +99,16 @@ class JSONDecoderTests: XCTestCase {
101
99
}
102
100
}
103
101
104
- do {
105
- let json = #"{"hello👩👩👧👧" 123 }"#
106
- let result = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !)
107
- XCTFail ( " Did not expect to get a result: \( result) " )
108
- } catch let Swift . DecodingError . dataCorrupted( context) {
109
- // expected
102
+ let json = #"{"hello👩👩👧👧" 123 }"#
103
+ XCTAssertThrowsError ( _ = try PureSwiftJSON . JSONDecoder ( ) . decode ( HelloWorld . self, from: json. data ( using: . utf8) !) ) {
104
+ error in
105
+ guard case let Swift . DecodingError . dataCorrupted( context) = error else {
106
+ XCTFail ( " Unexpected error: \( error) " ) ; return
107
+ }
108
+
110
109
XCTAssertEqual ( context. codingPath. count, 0 )
111
110
XCTAssertEqual ( context. debugDescription, " Unexpected character `1` at character index: 34 " )
112
111
XCTAssertNotNil ( context. underlyingError)
113
- } catch {
114
- XCTFail ( " Unexpected error: \( error) " )
115
112
}
116
113
}
117
114
}
0 commit comments