diff_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2022 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package diff
  5. import (
  6. "bytes"
  7. "path/filepath"
  8. "testing"
  9. "golang.org/x/tools/txtar"
  10. )
  11. func clean(text []byte) []byte {
  12. text = bytes.ReplaceAll(text, []byte("$\n"), []byte("\n"))
  13. text = bytes.TrimSuffix(text, []byte("^D\n"))
  14. return text
  15. }
  16. func Test(t *testing.T) {
  17. files, _ := filepath.Glob("testdata/*.txt")
  18. if len(files) == 0 {
  19. t.Fatalf("no testdata")
  20. }
  21. for _, file := range files {
  22. t.Run(filepath.Base(file), func(t *testing.T) {
  23. a, err := txtar.ParseFile(file)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. if len(a.Files) != 3 || a.Files[2].Name != "diff" {
  28. t.Fatalf("%s: want three files, third named \"diff\"", file)
  29. }
  30. diffs := Diff(a.Files[0].Name, clean(a.Files[0].Data), a.Files[1].Name, clean(a.Files[1].Data))
  31. want := clean(a.Files[2].Data)
  32. if !bytes.Equal(diffs, want) {
  33. t.Fatalf("%s: have:\n%s\nwant:\n%s\n%s", file,
  34. diffs, want, Diff("have", diffs, "want", want))
  35. }
  36. })
  37. }
  38. }