@@ -9,99 +9,91 @@ class JSONUnkeyedDecodingContainerTests: XCTestCase {
9
9
func testDecodeNull( ) {
10
10
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . null] ) , codingPath: [ ] )
11
11
12
- do {
13
- var container = try impl. unkeyedContainer ( )
14
- let result = try container. decodeNil ( )
15
- XCTAssertEqual ( result, true )
16
- XCTAssertEqual ( container. currentIndex, 1 )
17
- }
18
- catch {
19
- XCTFail ( " Unexpected error: \( error) " )
20
- }
12
+ var container : UnkeyedDecodingContainer ?
13
+ var result : Bool ?
14
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
15
+ XCTAssertNoThrow ( result = try container? . decodeNil ( ) )
16
+ XCTAssertEqual ( result, true )
17
+ XCTAssertEqual ( container? . currentIndex, 1 )
18
+ XCTAssertEqual ( container? . isAtEnd, true )
21
19
}
22
20
23
21
func testDecodeNullFromArray( ) {
24
22
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . object( [ : ] ) ] ) , codingPath: [ ] )
25
23
26
- do {
27
- var container = try impl. unkeyedContainer ( )
28
- let result = try container. decodeNil ( )
29
- XCTAssertEqual ( result, false )
30
- XCTAssertEqual ( container. currentIndex, 0 )
31
- }
32
- catch {
33
- XCTFail ( " Unexpected error: \( error) " )
34
- }
24
+ var container : UnkeyedDecodingContainer ?
25
+ var result : Bool ?
26
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
27
+ XCTAssertNoThrow ( result = try container? . decodeNil ( ) )
28
+ XCTAssertEqual ( result, false )
29
+ XCTAssertEqual ( container? . currentIndex, 0 )
30
+ XCTAssertEqual ( container? . isAtEnd, false )
35
31
}
36
32
37
33
// MARK: - String -
38
34
39
35
func testDecodeString( ) {
40
36
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . string( " hello world " ) ] ) , codingPath: [ ] )
41
37
42
- do {
43
- var container = try impl. unkeyedContainer ( )
44
- let result = try container. decode ( String . self)
45
- XCTAssertEqual ( result, " hello world " )
46
- }
47
- catch {
48
- XCTFail ( " Unexpected error: \( error) " )
49
- }
38
+ var container : UnkeyedDecodingContainer ?
39
+ var result : String ?
40
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
41
+ XCTAssertNoThrow ( result = try container? . decode ( String . self) )
42
+ XCTAssertEqual ( result, " hello world " )
43
+ XCTAssertEqual ( container? . currentIndex, 1 )
44
+ XCTAssertEqual ( container? . isAtEnd, true )
50
45
}
51
46
52
47
func testDecodeStringFromNumber( ) {
53
48
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . number( " 123 " ) ] ) , codingPath: [ ] )
49
+ let type = String . self
54
50
55
- do {
56
- var container = try impl. unkeyedContainer ( )
57
- let result = try container. decode ( String . self)
58
- XCTFail ( " Did not expect to get a result: \( result) " )
59
- }
60
- catch Swift . DecodingError . typeMismatch( let type, let context) {
51
+ var container : UnkeyedDecodingContainer ?
52
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
53
+ XCTAssertThrowsError ( _ = try container? . decode ( type. self) ) { ( error) in
54
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
55
+ return XCTFail ( " Unexpected error: \( error) " )
56
+ }
57
+
61
58
// expected
62
59
XCTAssertTrue ( type == String . self)
63
60
XCTAssertEqual ( context. codingPath. count, 1 )
64
61
XCTAssertEqual ( context. codingPath. first as? ArrayKey , ArrayKey ( index: 0 ) )
65
62
XCTAssertEqual ( context. debugDescription, " Expected to decode String but found a number instead. " )
66
63
}
67
- catch {
68
- XCTFail ( " Unexpected error: \( error) " )
69
- }
70
64
}
71
65
72
66
// MARK: - Bool -
73
67
74
68
func testDecodeBool( ) {
75
69
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . bool( false ) ] ) , codingPath: [ ] )
76
70
77
- do {
78
- var container = try impl. unkeyedContainer ( )
79
- let result = try container. decode ( Bool . self)
80
- XCTAssertEqual ( result, false )
81
- }
82
- catch {
83
- XCTFail ( " Unexpected error: \( error) " )
84
- }
71
+ var container : UnkeyedDecodingContainer ?
72
+ var result : Bool ?
73
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
74
+ XCTAssertNoThrow ( result = try container? . decode ( Bool . self) )
75
+ XCTAssertEqual ( result, false )
76
+ XCTAssertEqual ( container? . currentIndex, 1 )
77
+ XCTAssertEqual ( container? . isAtEnd, true )
85
78
}
86
79
87
80
func testDecodeBoolFromNumber( ) {
88
81
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . string( " hallo " ) ] ) , codingPath: [ ] )
89
-
90
- do {
91
- var container = try impl. unkeyedContainer ( )
92
- let result = try container. decode ( Bool . self)
93
- XCTFail ( " Did not expect to get a result: \( result) " )
94
- }
95
- catch Swift . DecodingError . typeMismatch( let type, let context) {
82
+ let type = Bool . self
83
+
84
+ var container : UnkeyedDecodingContainer ?
85
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
86
+ XCTAssertThrowsError ( _ = try container? . decode ( type. self) ) { ( error) in
87
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
88
+ return XCTFail ( " Unexpected error: \( error) " )
89
+ }
90
+
96
91
// expected
97
92
XCTAssertTrue ( type == Bool . self)
98
93
XCTAssertEqual ( context. codingPath. count, 1 )
99
94
XCTAssertEqual ( context. codingPath. first as? ArrayKey , ArrayKey ( index: 0 ) )
100
95
XCTAssertEqual ( context. debugDescription, " Expected to decode Bool but found a string instead. " )
101
96
}
102
- catch {
103
- XCTFail ( " Unexpected error: \( error) " )
104
- }
105
97
}
106
98
107
99
// MARK: - Integer -
@@ -399,24 +391,37 @@ class JSONUnkeyedDecodingContainerTests: XCTestCase {
399
391
func testGetFloatTypeMismatch( ) {
400
392
let type = Float . self
401
393
let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . object( [ : ] ) ] ) , codingPath: [ ] )
394
+
395
+ var container : UnkeyedDecodingContainer ?
396
+ XCTAssertNoThrow ( container = try impl. unkeyedContainer ( ) )
397
+ XCTAssertThrowsError ( _ = try container? . decode ( type. self) ) { ( error) in
398
+ guard case Swift . DecodingError . typeMismatch( let type, let context) = error else {
399
+ return XCTFail ( " Unexpected error: \( error) " )
400
+ }
402
401
403
- do {
404
- var container = try impl. unkeyedContainer ( )
405
- let result = try container. decode ( type. self)
406
- XCTFail ( " Did not expect to get a result: \( result) " )
407
- }
408
- catch Swift . DecodingError . typeMismatch( let type, let context) {
409
402
// expected
410
403
XCTAssertTrue ( type == Float . self)
411
404
XCTAssertEqual ( context. codingPath. count, 1 )
412
405
XCTAssertEqual ( context. codingPath. first as? ArrayKey , ArrayKey ( index: 0 ) )
413
406
XCTAssertEqual ( context. debugDescription, " Expected to decode Float but found a dictionary instead. " )
414
407
}
415
- catch {
416
- XCTFail ( " Unexpected error: \( error) " )
417
- }
418
408
}
419
409
420
-
410
+ // MARK: - Containers -
411
+
412
+ func testGetKeyedContainer( ) {
413
+ let impl = JSONDecoderImpl ( userInfo: [ : ] , from: . array( [ . object( [ " foo " : . string( " bar " ) ] ) ] ) , codingPath: [ ] )
414
+
415
+ enum CodingKeys : String , CodingKey {
416
+ case foo
417
+ }
418
+
419
+ var unkeyedContainer : UnkeyedDecodingContainer ?
420
+ var keyedContainer : KeyedDecodingContainer < CodingKeys > ?
421
+ XCTAssertNoThrow ( unkeyedContainer = try impl. unkeyedContainer ( ) )
422
+ XCTAssertNoThrow ( keyedContainer = try unkeyedContainer? . nestedContainer ( keyedBy: CodingKeys . self) )
423
+ XCTAssertEqual ( unkeyedContainer? . isAtEnd, true )
424
+ XCTAssertEqual ( " bar " , try keyedContainer? . decode ( String . self, forKey: . foo) )
425
+ }
421
426
}
422
427
0 commit comments