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

proposal: add package "internal/diff" #23113

Open
hirochachacha opened this issue Dec 13, 2017 · 5 comments

Comments

@hirochachacha
Copy link
Contributor

commented Dec 13, 2017

Many test code are something like:

func TestAAA(t *testing.T) {
  if got != want {
    t.Errorf("got %v, want %v")
  }
}

However, these kind of code are pretty useless when we expected multi-line values.
For example,

--- FAIL: TestExamples (0.00s)
	example_test.go:186: Import: got Play == "package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"os/exec\"\n)\n\nfunc main() {\n\tout, err := exec.Command(\"date\").Output()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Printf(\"The date is %s\\n\", out)\n}\n", want "package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"os/exec\"\n)\n\n// aaa\n\nfunc main() {\n    out, err := exec.Command(\"date\").Output()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Printf(\"The date is %s\\n\", out)\n}\n"
FAIL

So, I propose to support diff algorithm for making error messages human-readable.

The simplest implementation is executing the diff command. It doesn't work on windows.
But we can use fallback, so it can be a good starting point.

I'm not sure that supporting the "diff" package officially is worth doing.
But "internal" packages seem harmless.

@gopherbot gopherbot added this to the Proposal milestone Dec 13, 2017

@gopherbot gopherbot added the Proposal label Dec 13, 2017

@dsnet

This comment has been minimized.

Copy link
Member

commented Dec 13, 2017

We could vendor in github.com/google/go-cmp/cmp, which has a diff algorithm built in. When working on the standard library tests, I have wanted to reach for that package several times.

The cmp.Diff function was designed precisely for this purpose.

@hirochachacha

This comment has been minimized.

Copy link
Contributor Author

commented Dec 13, 2017

@dsnet I tried. but it seems to me that it doesn't help multi-line cases.

package main

import (
	"fmt"

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

func main() {
	fmt.Println(cmp.Diff("a\nb\nc", "a\nb1\nc"))
}

output:

{string}:
	-: "a\nb\nc"
	+: "a\nb1\nc"
@hirochachacha

This comment has been minimized.

Copy link
Contributor Author

commented Dec 13, 2017

Oh, I understand.

package main

import (
	"fmt"
	"strings"

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

func main() {
	fmt.Println(cmp.Diff(strings.Split("a\nb\nc", "\n"), strings.Split("a\nb1\nc", "\n")))
}

output:

{[]string}[1]:
	-: "b"
	+: "b1"
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 5, 2018

It seems to make more sense to encourage experimentation outside and then consider whether to bring a particular outside one in. There are also tests that run diff, and it would be nice to unify them.

Note that cmp is a lot bigger than just this one diff issue.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 12, 2018

Putting on hold for a more specific proposal--should we use cmp, or introduce something new?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.