Skip to content

Diff output is sometimes confusing and/or hard to read #274

@schellj

Description

@schellj

I've been working with a test apparatus for comparing JSON data. For ease-of-use and readability, I'd like to be able to compare prettified/indented JSON strings and print the differences in an easy-to-read way.

I've found that the cmp.Diff output can be quite varied even on similar inputs and can be either easy-to-read, somewhat hard to read, or hard to read and confusing depending on the exact input for non-obvious reasons.

See https://play.golang.org/p/mu-TsXoGEEL (pasted below):

Code:

package main

import (
	"fmt"

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

func main() {
	fmt.Printf("Confusing diff output:\n%s\n", cmp.Diff(`{
  "id": 1,
  "with_package_maps": true,
  "with_other_maps": true
}`, `{
  "id": 1434180,
  "with_package_maps": true,
  "with_other_maps": true
}`))

	fmt.Printf("Non-confusing, but somewhat hard to find differences diff output:\n%s\n", cmp.Diff(`{
  "id": 1,
  "foo": true,
  "bar": true,
}`, `{
  "id": 1434180,
  "foo": true,
  "bar": true,
}`))

	fmt.Printf("Non-confusing, easy to read and find differences diff output:\n%s\n", cmp.Diff(`
{
  "id": 1,
  "with_package_maps": true,
  "with_other_maps": true
}`, `
{
  "id": 1434180,
  "with_package_maps": true,
  "with_other_maps": true
}`))
}

Output:

Confusing diff output:
  strings.Join({
  	"{\n  \"id\": 1",
+ 	"434180",
  	",\n  \"with_package_maps\": true,\n  \"with_other_maps\": true\n}",
  }, "")

Non-confusing, easy to read and find differences diff output:
  (
  	"""
  	
  	{
- 	  "id": 1,
+ 	  "id": 1434180,
  	  "with_package_maps": true,
  	  "with_other_maps": true
  	}
  	"""
  )

Non-confusing, but somewhat harder to find differences diff output:
  string(
- 	"{\n  \"id\": 1,\n  \"foo\": true,\n  \"bar\": true,\n}",
+ 	"{\n  \"id\": 1434180,\n  \"foo\": true,\n  \"bar\": true,\n}",
  )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions