Skip to content
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

Unexpected diffs of nil slices #157

Closed
cbandy opened this issue Aug 21, 2019 · 1 comment · Fixed by #201
Closed

Unexpected diffs of nil slices #157

cbandy opened this issue Aug 21, 2019 · 1 comment · Fixed by #201
Labels

Comments

@cbandy
Copy link

cbandy commented Aug 21, 2019

For some values (types?), cmp.Diff returns no apparent difference though cmp.Equal returns false. The following program shows no -/+ for []int, []byte, and []float64 types.

Diff does produce output, so I'm not certain this is a bug. I was expecting (and would grateful) to see - nil / + {} in these cases as well.

package main

import (
	"fmt"
	"reflect"

	"github.com/google/go-cmp/cmp"
)

func main() {
	for _, tt := range []struct{ Nil, Empty interface{} }{
		{Nil: []int(nil), Empty: []int{}},
		{Nil: []byte(nil), Empty: []byte{}},
		{Nil: []string(nil), Empty: []string{}},
		{Nil: []float64(nil), Empty: []float64{}},
		{Nil: []struct{}(nil), Empty: []struct{}{}},
		{Nil: []interface{}(nil), Empty: []interface{}{}},
	} {
		fmt.Println("Type:", reflect.TypeOf(tt.Nil).String())
		fmt.Println("DeepEqual:", reflect.DeepEqual(tt.Nil, tt.Empty))
		fmt.Println("cmp.Equal:", cmp.Equal(tt.Nil, tt.Empty))
		fmt.Println("Diff:\n", cmp.Diff(tt.Nil, tt.Empty))
		fmt.Println("=====")
	}
}

Output:

Type: []int
DeepEqual: false
cmp.Equal: false
Diff:
   []int{}

=====
Type: []uint8
DeepEqual: false
cmp.Equal: false
Diff:
   bytes.Join({}, "")

=====
Type: []string
DeepEqual: false
cmp.Equal: false
Diff:
   []string(
- 	nil,
+ 	{},
  )

=====
Type: []float64
DeepEqual: false
cmp.Equal: false
Diff:
   []float64{}

=====
Type: []struct {}
DeepEqual: false
cmp.Equal: false
Diff:
   []struct{}(
- 	nil,
+ 	{},
  )

=====
Type: []interface {}
DeepEqual: false
cmp.Equal: false
Diff:
   []interface{}(
- 	nil,
+ 	{},
  )

=====
@cbandy cbandy changed the title Unusual diffs of nil slices Unexpected diffs of nil slices Aug 21, 2019
@dsnet
Copy link
Collaborator

dsnet commented Aug 21, 2019

This is clearly a bug in the diff output. Thank you reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants