assert.Equal panics on string pointers

Open
#677 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
go
Domain
testing

Research direction

Start in the assertions method diff, especially the string-handling branch shown in the issue, and compare it with the earlier pointer-dereferencing deepEqual behavior. Reproduce the provided assert.Equal example and confirm that differing string pointers no longer panic and are reported as unequal.

Written by the indexing model from the issue text.

Description

When asserting on string pointers with different values, assert.Equal will panic.

E.g.,

		a := "a"
		b := "b"
		assert.Equal(t, &a, &b)

Panic interface conversion: interface {} is *string, not stringgoroutine 1 [running]:...

This caused by the following in assertions method diff (v1.2.2) -

	var e, a string
	if ek != reflect.String {
		e = spewConfig.Sdump(expected)
		a = spewConfig.Sdump(actual)
	} else {
		e = expected.(string)
		a = actual.(string)
	}

An earlier deepEqual correctly dereferences both pointers, however in the diff method above the pointers are cast directly to strings (e = expected.(string)).

This was introduced at https://github.com/stretchr/testify/commit/2a15e200fd19f82a56f0d56a3192bf0f9583a15e

Dominant language
Go
Stars
26.2k
Forks
1.9k
Avg merge
2d 4h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stretchr/testify

All issues in stretchr/testify

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.