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