Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendations for test helpers to compare outputs? #1141

Open
MrMage opened this issue Apr 21, 2021 · 3 comments
Open

Recommendations for test helpers to compare outputs? #1141

MrMage opened this issue Apr 21, 2021 · 3 comments

Comments

@MrMage
Copy link
Contributor

MrMage commented Apr 21, 2021

In my tests, I often compare the (protobuf message) output of a function with an expected result created via e.g. Message.with { }. However, XCTAssertEqual will output the full string representation of both messages in case of a mismatch, which makes it hard to spot the difference.

Do you have any recommendations on e.g. only outputting the fields that are different? I had a cursory look at this project, but am unsure whether implementing PBTestVisitor is the recommended approach.

@thomasvl
Copy link
Collaborator

We don't currently have any api for recursively comparing messages.

The Visitor interface isn't really going to work for this since it gets called by the message as it walks the field, so it wouldn't be able to walk two messages in parallel to compare things. You might be able to build something for it, but you'd have to build up some sorta journal from the walk, and then walk the second message and try to compare against the journal.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 21, 2021

Thanks for the info! Should I keep this open as a feature request?

@tbkka
Copy link
Contributor

tbkka commented Apr 21, 2021

Three ideas come to mind:

  • At one point, I looked into possibly implementing our == test using exactly this approach: Setting up two visitors and incrementally walking both objects. Such a reflection-based approach would require less code and be more efficient. Of course, it would also require changing our Visitor API so that it was not callback-based. I would like to make this change regardless since it would open that API to uses such as yours, but I don't expect to get to that any time in the foreseeable future.

  • You might try using Swift Reflection via the Mirror(reflecting:) API to compare the two objects. Swift's reflection APIs are pretty basic but might be amenable to this kind of use.

  • You could also write out the objects as protobuf JSON and compare those. I expect there are already reasonable tools for comparing JSON-coded objects that might help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants