Twitter | Search | |
Matt Diephouse
I’ve started writing functions that “diff” two values to return a nicely formatted description of their differences to use in test failures. e.g. let a = M(id: “A”, foo: “bar”) let b = M(id: “B”, foo: “baz”) diff(a,b) == “”” • id: “A” != “B” • foo: “bar” != “baz” “””
Reply Retweet Like More
Matt Diephouse Feb 3
Replying to @mdiep
This makes it really easy to see why a test fails. I wish there was an easy way to write generic code for this in Swift—or to get this behavior for free. It feels like a very reasonable thing to want to do.
Reply Retweet Like
Matt Diephouse Feb 3
Replying to @mdiep
Here we go: Diff values with Codable, CaseIterable, and KeyPaths.
Reply Retweet Like
Matt Diephouse Feb 4
Replying to @jckarter
You can diff values with Mirror if conform your type to Hashable! (🎩 ) I had no idea that you could cast to AnyHashable. 🤯 This is the first time I’ve been able to get Mirror do what I want.
Reply Retweet Like
thomasmarsh Feb 3
Replying to @mdiep @pointfreeco
What about using Swift.dump? uses this in their snapshot testing library.
Reply Retweet Like
Matt Diephouse Feb 3
Diffing the dump is general, but I don’t think it reads as nicely.
Reply Retweet Like
Alexander Cyon Feb 3
Replying to @mdiep
Reply Retweet Like
David Rodrigues Feb 3
Replying to @mdiep
I wish this was part of XCTest so badly.
Reply Retweet Like
Suyash Srijan Feb 3
Replying to @mdiep
Nice! I did something similar a while ago for collections (using the new diffing APIs in Swift 5.1) so I can more easily learn what exactly is different vs. inspecting "[array with lots of items] is not equal to [array with lots of items]" failure message in unit tests.
Reply Retweet Like