go-hep.org/x/hep@v0.38.1/brio/cmd/brio-gen/main_test.go (about)

     1  // Copyright ©2018 The go-hep 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  // Command brio-gen generates (un)marshaler code for types.
     6  package main
     7  
     8  import (
     9  	"bytes"
    10  	"os"
    11  	"testing"
    12  
    13  	// make sure this has been compiled for TestGenerate
    14  	_ "go-hep.org/x/hep/brio/cmd/brio-gen/internal/briotest"
    15  	"go-hep.org/x/hep/internal/diff"
    16  )
    17  
    18  func TestGenerate(t *testing.T) {
    19  	for _, tc := range []struct {
    20  		name  string
    21  		types []string
    22  		want  string
    23  	}{
    24  		{
    25  			name:  "image",
    26  			types: []string{"Point"},
    27  			want:  "testdata/image_brio.go",
    28  		},
    29  		{
    30  			name:  "go-hep.org/x/hep/brio/cmd/brio-gen/internal/briotest",
    31  			types: []string{"Hist", "Bin"},
    32  			want:  "testdata/briotest_brio.go",
    33  		},
    34  	} {
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			buf := new(bytes.Buffer)
    37  			err := generate(buf, tc.name, tc.types)
    38  			if err != nil {
    39  				t.Fatal(err)
    40  			}
    41  
    42  			got := buf.Bytes()
    43  			want, err := os.ReadFile(tc.want)
    44  			if err != nil {
    45  				t.Fatal(err)
    46  			}
    47  			outfile := tc.want + "_got"
    48  			if !bytes.Equal(got, want) {
    49  				t.Fatalf("generated code error:\n%s\n", diff.Format(string(got), string(want)))
    50  			}
    51  			// Remove output if test passes
    52  			// Note: output file are referenced in .gitignore
    53  			_ = os.Remove(outfile)
    54  		})
    55  	}
    56  }