Repository navigation
assert: improve equality failure messages for pointers to basic types - #1995
Open
IvanSergunin001 wants to merge 1 commit into
Open
IvanSergunin001 wants to merge 1 commit into
IvanSergunin001 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improve equality assertion failure messages by displaying the dereferenced values of pointers to basic types alongside their pointer addresses.
Previously, when comparing pointers to basic types with different underlying values, the failure message displayed only their addresses, making it difficult to identify the actual difference.
For example, comparing pointers to 5 and 10 previously produced:
With this change, the underlying values are also displayed:
Changes
truncatingFormatValuesto display the underlying values of pointers to basic types while preserving the existing formatting for other types.truncateStringfromtruncatingFormatto reuse the truncation logic without introducing additional flags.isBasicTypeto identify supported scalar types usingreflect.Kind.%#vfor dereferenced string values to include quotation marks and escape special characters, such as newlines.formatUnequalValuesto use the new formatting helper when the compared values have the same type.time.Duration, and pointers to non-basic types.Motivation
As described in issue #1118, equality assertion failures involving pointers to basic types previously displayed only pointer addresses, without revealing the underlying values being compared. This could make debugging confusing, as it might suggest that the comparison was based on pointer addresses rather than their values.
An initial solution was proposed in PR #1287, but it was not merged and was eventually closed.
This PR adapts that implementation to the current version of
testify, incorporates feedback from the original review, and introduces additional improvements to string pointer formatting.In particular, dereferenced strings are now enclosed in quotation marks, making empty strings distinguishable from missing output and ensuring that special characters are properly escaped.
For example, the previous implementation proposed in PR #1287 could produce:
With this PR, the output becomes:
The changes affect failure message formatting only and do not modify equality comparison semantics.
Related issues