Skip to content

std.testing: Add expectEqualBytes that outputs hexdumps with diffs highlighted in red #13720

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

Merged
merged 1 commit into from
Nov 30, 2022

Conversation

squeek502
Copy link
Collaborator

@squeek502 squeek502 commented Nov 30, 2022

Provides a nice improvement over the current output of expectEqualSlices when dealing with binary data (the status quo is that only the first line of the output shown below is printed [the index 5 incorrect, etc]).

Example when the lengths match:
expectEqualBytes1

Example when the lengths are different:
expectEqualBytes2

The coloring is controlled by std.debug.detectTTYConfig so it will be disabled when appropriate.


Some notes:

  • Instead of making this a separate function, this style of printing could be used by expectEqualSlices when the type is u8. Unsure if that'd be better. There's this relevant TODO in expectEqualSlices, but I'm unsure if hexdumps like this would satisfy it or not:

zig/lib/std/testing.zig

Lines 285 to 288 in 71038c4

// TODO better printing of the difference
// If the arrays are small enough we could print the whole thing
// If the child type is u8 and no weird bytes, we could print it as strings
// Even for the length difference, it would be useful to see the values of the slices probably.

  • It's possible that this will need some sort of limiting in how much it prints, as in theory the slices could be extremely large.

…ghlighted in red

The coloring is controlled by `std.debug.detectTTYConfig` so it will be disabled when appropriate.
@andrewrk andrewrk enabled auto-merge (rebase) November 30, 2022 21:58
@andrewrk andrewrk merged commit 34fa6a1 into ziglang:master Nov 30, 2022
squeek502 added a commit to squeek502/zig that referenced this pull request Dec 8, 2022
- In ziglang#13720, expectEqualBytes was added as a standalone function
- In ziglang#13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
squeek502 added a commit to squeek502/zig that referenced this pull request Dec 8, 2022
- In ziglang#13720, expectEqualBytes was added as a standalone function
- In ziglang#13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
squeek502 added a commit to squeek502/zig that referenced this pull request Dec 9, 2022
- In ziglang#13720, expectEqualBytes was added as a standalone function
- In ziglang#13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
andrewrk pushed a commit that referenced this pull request Dec 9, 2022
- In #13720, expectEqualBytes was added as a standalone function
- In #13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
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

Successfully merging this pull request may close these issues.

2 participants