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

unhelpful output for difference in slices #350

Open
seehuhn opened this issue Jan 12, 2024 · 1 comment
Open

unhelpful output for difference in slices #350

seehuhn opened this issue Jan 12, 2024 · 1 comment
Labels
reporter Improvements in the difference reporter

Comments

@seehuhn
Copy link

seehuhn commented Jan 12, 2024

In the code below, Diff shows the following "differences" between two data structures:

main.Array{
  	main.Integer(0),
- 	s"<Array, 3 elements>",
+ 	s"<Array, 3 elements>",
  }

Since the + and - lines are the same, this is not very helpful. It would be nice if Diff would show the actual differences. Here is the code (also on the Go Playground):

package main

import (
	"fmt"
	"strconv"
	"strings"

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

type Integer int64

type Array []interface{}

func (x Array) String() string {
	res := []string{}
	res = append(res, "Array")
	res = append(res, strconv.FormatInt(int64(len(x)), 10)+" elements")
	return "<" + strings.Join(res, ", ") + ">"
}

func main() {
	a := Array{Integer(0), Array{float64(1), float64(2), float64(3)}}
	b := Array{Integer(0), Array{Integer(1), Integer(2), Integer(3)}}
	fmt.Println(cmp.Diff(a, b))
}

@dsnet
Copy link
Collaborator

dsnet commented Jan 12, 2024

Thanks for the concise report and reproduction. This suggest that the reporter should check if the String() output of an adjacent difference is identical. If so, to re-format the sub-value to ignore the String method.

@dsnet dsnet added the reporter Improvements in the difference reporter label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reporter Improvements in the difference reporter
Projects
None yet
Development

No branches or pull requests

2 participants