github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/cmd/json2test/main_test.go (about)

     1  // Copyright (c) 2018 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/kylelemons/godebug/diff"
    14  )
    15  
    16  func TestWriteTestOutput(t *testing.T) {
    17  	for name, tc := range map[string]struct {
    18  		verbose   bool
    19  		inputFile string
    20  		goldFile  string
    21  	}{
    22  		"quiet": {
    23  			verbose:   false,
    24  			inputFile: "testdata/input.txt",
    25  			goldFile:  "testdata/gold-quiet.txt",
    26  		},
    27  		"verbose": {
    28  			verbose:   true,
    29  			inputFile: "testdata/input.txt",
    30  			goldFile:  "testdata/gold-verbose.txt",
    31  		},
    32  	} {
    33  		t.Run(name, func(t *testing.T) {
    34  			input, err := os.Open(tc.inputFile)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  			var out bytes.Buffer
    39  			if err := writeTestOutput(input, &out, tc.verbose); err != errTestFailure {
    40  				t.Error("expected test failure")
    41  			}
    42  
    43  			gold, err := os.Open(tc.goldFile)
    44  			if err != nil {
    45  				t.Fatal(err)
    46  			}
    47  			expected, err := ioutil.ReadAll(gold)
    48  			if err != nil {
    49  				t.Fatal(err)
    50  			}
    51  
    52  			if !bytes.Equal(out.Bytes(), expected) {
    53  				t.Errorf("output does not match %s", tc.goldFile)
    54  				t.Error("\n" + diff.Diff(string(expected), out.String()))
    55  			}
    56  		})
    57  	}
    58  }