Skip to content

fix(gomock): display nil slices distinctly from empty slices in error messages#308

Open
wasim-builds wants to merge 1 commit into
uber-go:mainfrom
wasim-builds:fix/slice-nil-print-message
Open

fix(gomock): display nil slices distinctly from empty slices in error messages#308
wasim-builds wants to merge 1 commit into
uber-go:mainfrom
wasim-builds:fix/slice-nil-print-message

Conversation

@wasim-builds

Copy link
Copy Markdown

What does this PR do?

Fixes confusing error messages when comparing nil slices with empty slices in gomock error output.

Problem

When a nil slice is passed where an empty slice is expected (or vice versa), the error message shows identical values for both "Got" and "Want", making it impossible to distinguish them:

Got: [] ([][32]uint8)
Want: is equal to [] ([][32]uint8)

This is because fmt.Sprintf("%v", ...) formats both nil slices and empty slices as [].

Solution

Update formatGottenArg to detect nil slices using reflect.ValueOf and display them as nil instead of []:

Got: nil ([][32]uint8)
Want: is equal to [] ([][32]uint8)

Changes

  • gomock/call.go: Updated formatGottenArg to check if the argument is a nil slice using reflection, and format it as nil (<type>) instead of [] (<type>).
  • gomock/controller_test.go: Added SliceOfArraysMethod to the test Subject struct. Added TestNilSliceVsEmptySlice and TestNilSliceOfArraysVsEmpty test cases to verify the fix for both []byte and [][32]byte types (the exact type from the original issue report).

Testing

All existing tests pass. Two new test cases added:

Closes #69

… messages

When comparing nil slices with empty slices, the error message showed
identical values for both 'Got' and 'Want' sides, making it impossible
to distinguish them (issue uber-go#69).

Updated formatGottenArg to detect nil slices using reflection and display
them as 'nil' instead of '[]' in error output.

Closes uber-go#69
@CLAassistant

CLAassistant commented Jul 23, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@wasim-builds

Copy link
Copy Markdown
Author

By the way, I've really been enjoying contributing to this project! My goal is to get more involved in open source to help maintain projects and learn from great engineering teams. Is there a process or pathway for becoming a member of the uber-go GitHub organization? I'd love to continue helping out where I can!

@wasim-builds

Copy link
Copy Markdown
Author

Hey 👋 Friendly bump on this one — fixes confusing gomock error messages when comparing nil vs empty slices. Small change, ready for review when you have a moment! 🙏

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.

Wrong print msg when comparing initialized and uninitialized slice

2 participants