Customize difference output?

Is it possible to customize the output of the difference of a expect/require comparison? When comparing larger structures or collections the current output makes it almost impossible to identify what is actually different.

I'm currently using GitHub - krzysztofzablocki/Difference: Simple way to identify what is different between 2 instances of any type. Must have for TDD. with XCTest, but I'm not sure how to use it or an equivalent with Swift Testing.

Can this be done with overloads (of __checkBinaryOperation?), traits, etc.?

The current output for collections seems to be implemented in swift-testing/Sources/Testing/Expectations/ExpectationChecking+Macro.swift at 0d85d8b32bc93afb30f52346b3612a86ebbb11f3 · swiftlang/swift-testing · GitHub.

1 Like

@grynspan @smontgomery is there currently any way to accomplish this? If not, what would be a good way to enable such customization? I’d be happy to discuss and contribute

First, sorry for overlooking your original post!

On the topic of expectation "diffing", I know this is an incredibly useful feature. Third-party libraries (such as Difference which you mentioned) do a pretty good job in the XCTest space. However, we'd like to eventually incorporate this kind of functionality directly into Swift Testing. Although those of us who maintain the library have talked about it often, and it has come up in Testing Workgroup meetings, I just realized we do not have a GitHub issue formally tracking the feature yet, so I went ahead and filed one here:

If you are inclined to experiment on your own, you might be able to leverage ST-0011: Issue Handling Traits to some benefit, however that's a fairly roundabout way to accomplish this workflow so it's only something I mention as a workaround.

1 Like

Thank you for the reply Stuart! I will have a look at the issue and ST-0011

We have a tool called expectNoDifference that is part of a few other tools for nicely printing the internals of complex types. It allows you to assert in both XCTest and Swift Testing that two values are equal, and when they are not a nicely formatted message is printed:

expectNoDifference(user, other)
expectNoDifference failed: …

  User(
    favoriteNumbers: […],
    id: 2,
-   name: "Blob"
+   name: "Blob!"
  )

(First: −, Second: +)

It tries to collapse arrays/dictionaries where there are no changes in the elements.

4 Likes

Nice! Thank you for the link, I’ll give this a try