Skip to content

assert: improve equality failure messages for pointers to basic types - #1995

Open
IvanSergunin001 wants to merge 1 commit into
stretchr:masterfrom
IvanSergunin001:fix-pointer-comparison
Open

IvanSergunin001 wants to merge 1 commit into
stretchr:masterfrom
IvanSergunin001:fix-pointer-comparison

Conversation

@IvanSergunin001

Copy link
Copy Markdown

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:

expected: (*int)(0x...)
actual  : (*int)(0x...)

With this change, the underlying values are also displayed:

expected: (*int)(0x...) 5
actual  : (*int)(0x...) 10

Changes

  • Introduced truncatingFormatValues to display the underlying values of pointers to basic types while preserving the existing formatting for other types.
  • Extracted truncateString from truncatingFormat to reuse the truncation logic without introducing additional flags.
  • Added isBasicType to identify supported scalar types using reflect.Kind.
  • Used %#v for dereferenced string values to include quotation marks and escape special characters, such as newlines.
  • Updated formatUnequalValues to use the new formatting helper when the compared values have the same type.
  • Preserved the existing formatting behavior for values of different types, 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:

Error:       Not equal:
             expected: (*string)(0x140001132d0) hello
             actual  : (*string)(0x140001132e0)

With this PR, the output becomes:

Error:       Not equal:
             expected: (*string)(0x140001132d0) "hello"
             actual  : (*string)(0x140001132e0) ""

The changes affect failure message formatting only and do not modify equality comparison semantics.

Related issues

  • Related to #1118.
  • Related discussion: #1273.
  • Based on PR #1287, with additional improvements, regression tests, and changes addressing the original review feedback.

@IvanSergunin001 IvanSergunin001 changed the title improve equality failure messages for pointers to basic types assert: improve equality failure messages for pointers to basic types Oct 22, 2026
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.

1 participant