github.com/andresbott/yamlfmt@v0.1.0/internal/yamlfmt/yamlfmt_test.go (about)

     1  package yamlfmt
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  	"github.com/google/go-cmp/cmp"
    10  )
    11  
    12  var _ = spew.Dump // prevent spew from being removed if unused
    13  
    14  const sampleDir = "./sampledata"
    15  
    16  func TestName(t *testing.T) {
    17  
    18  	tcs := []struct {
    19  		name   string
    20  		in     string
    21  		expect string
    22  	}{
    23  		{
    24  			name:   "assert line breaks",
    25  			in:     "remove_lines.yaml",
    26  			expect: "remove_lines.out.yaml",
    27  		},
    28  		{
    29  			name:   "assert quotes are removed",
    30  			in:     "unquote_text.yaml",
    31  			expect: "unquote_text.out.yaml",
    32  		},
    33  	}
    34  
    35  	for _, tc := range tcs {
    36  		t.Run(tc.name, func(t *testing.T) {
    37  
    38  			inBytes, err := os.ReadFile(filepath.Join(sampleDir, tc.in))
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			got, err := Format(inBytes)
    44  			if err != nil {
    45  				t.Fatalf("unexpected err: %v", err)
    46  			}
    47  
    48  			expectBytes, err := os.ReadFile(filepath.Join(sampleDir, tc.expect))
    49  			if err != nil {
    50  				t.Fatal(err)
    51  			}
    52  
    53  			if diff := cmp.Diff(string(got), string(expectBytes)); diff != "" {
    54  				t.Errorf("unexpected value (-got +want)\n%s", diff)
    55  			}
    56  
    57  			//_ = os.WriteFile(filepath.Join(sampleDir, tc.expect), got, 0644)
    58  		})
    59  	}
    60  
    61  }