pointfreeco / swift-custom-dump

A collection of tools for debugging, diffing, and testing your application's data structures.
MIT License
800 stars 89 forks source link

XCTAssertNoDifference diff doesn't highlight type differences #48

Closed cameroncooke closed 2 years ago

cameroncooke commented 2 years ago

Describe the bug When working with two heterogeneous dictionaries of type [AnyHashable, AnyHasable] in order to compare them where one dictionary represents a floating point value using a Float type and the other using Double leads to a very subtle error where the diff presented shows the two comparing fields as different yet show a identical literal representation.

To Reproduce

    func testBug() {

        let values: [AnyHashable: AnyHashable] = [
            "value": Float(29.99)
        ]

        let expected: [AnyHashable: AnyHashable] = [
            "value": 29.99, // Is a Double
        ]

        XCTAssertNoDifference(values, expected)
    }

Expected behavior I assume due to precision that Float vs Double can't be compared like for like but I would expected XCTAssertNoDifference to maybe show the type information where the two values are literal identical and where the types are different, so something like:

XCTAssertNoDifference failed: …

    [
  −   "value": 29.99 (Float)
  +   "value": 29.99 (Double)
    ]

(First: −, Second: +)

I spent hours trying to work out why my two hashable dictionaries that appeared to have identical values were failing and the diff output was confusing things further. It was very subtle that the code under test was using a Float value and my dictionary I wanted to compare against used a Double literal.

Screenshots

Screenshot 2022-05-03 at 10 01 31

Environment

cameroncooke commented 2 years ago

Awesome thanks for the quick update!