1
1
import XCTest
2
2
import Foundation
3
3
4
- func XCTAssertEqual( _ dictionary: [ String : Any ] , _ expected: [ String : Any ] ) {
4
+ /// Checks if two dictionaries are equal.
5
+ func XCTAssertDeepEqual( _ dictionary: [ String : Any ] , _ expected: [ String : Any ] ) {
5
6
6
7
for (key, value) in dictionary {
7
8
@@ -10,48 +11,84 @@ func XCTAssertEqual(_ dictionary: [String: Any], _ expected: [String: Any]) {
10
11
break
11
12
}
12
13
13
- if let hashable = value as? AnyHashable , let expectedHashable = expectedValue as? AnyHashable {
14
- XCTAssertEqual ( hashable, expectedHashable)
15
- continue
16
- }
14
+ let didCompare = XCTDeepCompare ( value, expectedValue)
17
15
18
- if let array = value as? [ AnyHashable ] , let expectedArray = expectedValue as? [ AnyHashable ] {
19
- XCTAssertEqual ( array , expectedArray )
20
- continue
16
+ guard didCompare == true else {
17
+ XCTFail ( " Could not compare values for key \( key ) " )
18
+ return
21
19
}
22
20
23
- if let arrayArray = value as? [ [ AnyHashable ] ] , let expectedArrayArray = expectedValue as? [ [ AnyHashable ] ] {
21
+ }
22
+
23
+ }
24
+
25
+ /// Checks if two arrays are equal.
26
+ func XCTAssertDeepEqual( _ array: [ Any ] , _ expected: [ Any ] ) {
27
+
28
+ guard array. count == expected. count else {
29
+ XCTFail ( " Array does not have the expected number of items. " )
30
+ return
31
+ }
32
+
33
+ var idx = 0
24
34
25
- XCTAssertEqual ( arrayArray . count , expectedArrayArray . count )
35
+ for (value , expectedValue ) in zip ( array , expected ) {
26
36
27
- for (arr, expectedArr) in zip ( arrayArray, expectedArrayArray) {
28
- XCTAssertEqual ( arr, expectedArr)
29
- }
37
+ let didCompare = XCTDeepCompare ( value, expectedValue)
30
38
31
- continue
32
-
39
+ guard didCompare == true else {
40
+ XCTFail ( " Could not compare values at index \( idx) . " )
41
+ return
33
42
}
34
43
44
+ idx += 1
45
+
46
+ }
47
+
48
+ }
49
+
50
+ private func XCTDeepCompare( _ value: Any , _ expectedValue: Any ) -> Bool {
51
+
52
+ if let hashable = value as? AnyHashable , let expectedHashable = expectedValue as? AnyHashable {
53
+ XCTAssertEqual ( hashable, expectedHashable)
54
+ return true
55
+ }
56
+
57
+ if let array = value as? [ AnyHashable ] , let expectedArray = expectedValue as? [ AnyHashable ] {
58
+ XCTAssertEqual ( array, expectedArray)
59
+ return true
60
+ }
61
+
62
+ if let arrayArray = value as? [ [ AnyHashable ] ] , let expectedArrayArray = expectedValue as? [ [ AnyHashable ] ] {
35
63
36
- if let dictionary = value as? [ String : Any ] , let expectedDictionary = expectedValue as? [ String : Any ] {
37
- XCTAssertEqual ( dictionary, expectedDictionary)
38
- continue
64
+ XCTAssertEqual ( arrayArray. count, expectedArrayArray. count)
65
+
66
+ for (arr, expectedArr) in zip ( arrayArray, expectedArrayArray) {
67
+ XCTAssertEqual ( arr, expectedArr)
39
68
}
40
69
41
- if let dictionaryArray = value as? [ [ String : Any ] ] , let expectedDictionaryArray = expectedValue as? [ [ String : Any ] ] {
70
+ return true
71
+
72
+ }
73
+
42
74
43
- XCTAssertEqual ( dictionaryArray. count, expectedDictionaryArray. count)
75
+ if let dictionary = value as? [ String : Any ] , let expectedDictionary = expectedValue as? [ String : Any ] {
76
+ XCTAssertDeepEqual ( dictionary, expectedDictionary)
77
+ return true
78
+ }
44
79
45
- for (dict, expectedDict) in zip ( dictionaryArray, expectedDictionaryArray) {
46
- XCTAssertEqual ( dict, expectedDict)
47
- }
80
+ if let dictionaryArray = value as? [ [ String : Any ] ] , let expectedDictionaryArray = expectedValue as? [ [ String : Any ] ] {
48
81
49
- continue
82
+ XCTAssertEqual ( dictionaryArray . count , expectedDictionaryArray . count )
50
83
84
+ for (dict, expectedDict) in zip ( dictionaryArray, expectedDictionaryArray) {
85
+ XCTAssertDeepEqual ( dict, expectedDict)
51
86
}
52
87
53
- XCTFail ( " Could not compare values for key \( key ) " )
88
+ return true
54
89
55
90
}
56
91
92
+ return false
93
+
57
94
}
0 commit comments