Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTAssertEquals with two dicts in Swift

I am doing the exercism.io programming exercises and the tests I have to perform on my code has the goal to compare to dicts with each other. The sourcecode of the exercise can be found here https://github.com/exercism/xswift/tree/master/word-count

Test code for comparing two dicts

As of what I have understood bridgeToObjectiveC is apples internal methods for doing things and therefore they have been removed. With them i get '[S : T]' does not have a member named 'bridgeToObjectiveC' which is very understandable if they have removed it.

Without the method using only the params in the AssertEquals call i get '[S : T]' does not conform to protocol 'Equatable'. Is two dicts not comparable in Swift? How would I do to get them comparable?

like image 235
Johan Avatar asked Sep 29 '14 03:09

Johan


3 Answers

No, Swift dictionaries are not directly comparable. For the purposes of unit-testing, you can either do manual comparisons of their sizes and pair-wise element comparisons, or you can do the easy thing and create NSDictionarys out of them and compare them that way.

like image 77
NRitH Avatar answered Nov 17 '22 12:11

NRitH


Try

XCTAssertEqual(swiftDict as NSObject, objCDict as NSObject)

Forces the compiler to just chill out and call the isEqual: method on both.

like image 27
Robert Avatar answered Nov 17 '22 12:11

Robert


You can check equality of dictionaries as long as the values are Equatable. Modify XCTAssertEqualDictionaries to include a generic constraint:

func XCTAssertEqualDictionaries<S, T: Equatable>(first: [S:T], _ second: [S:T]) {
    XCTAssert(first == second)
}
like image 8
Nate Cook Avatar answered Nov 17 '22 12:11

Nate Cook