golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/diffp/diff_test.go (about) 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 5 package diffp 6 7 import ( 8 "bytes" 9 "path/filepath" 10 "testing" 11 12 "golang.org/x/tools/txtar" 13 ) 14 15 func clean(text []byte) []byte { 16 text = bytes.ReplaceAll(text, []byte("$\n"), []byte("\n")) 17 text = bytes.TrimSuffix(text, []byte("^D\n")) 18 return text 19 } 20 21 func Test(t *testing.T) { 22 files, _ := filepath.Glob("testdata/*.txt") 23 if len(files) == 0 { 24 t.Fatalf("no testdata") 25 } 26 27 for _, file := range files { 28 t.Run(filepath.Base(file), func(t *testing.T) { 29 a, err := txtar.ParseFile(file) 30 if err != nil { 31 t.Fatal(err) 32 } 33 if len(a.Files) != 3 || a.Files[2].Name != "diff" { 34 t.Fatalf("%s: want three files, third named \"diff\"", file) 35 } 36 diffs := Diff(a.Files[0].Name, clean(a.Files[0].Data), a.Files[1].Name, clean(a.Files[1].Data)) 37 want := clean(a.Files[2].Data) 38 if !bytes.Equal(diffs, want) { 39 t.Fatalf("%s: have:\n%s\nwant:\n%s\n%s", file, 40 diffs, want, Diff("have", diffs, "want", want)) 41 } 42 }) 43 } 44 }