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

     1  // Copyright ©2020 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  package main
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/internal/diff"
    14  )
    15  
    16  func TestGenerator(t *testing.T) {
    17  	for _, tc := range []struct {
    18  		name  string
    19  		rules []string
    20  		want  string
    21  	}{
    22  		{
    23  			name: "testdata/datalayout.yaml",
    24  			rules: []string{
    25  				"ex2::->ex2_",
    26  				"ex42::->ex42_",
    27  			},
    28  			want: "testdata/datalayout.go",
    29  		},
    30  	} {
    31  		t.Run(tc.name, func(t *testing.T) {
    32  			got := new(bytes.Buffer)
    33  			err := process(got, "podio", tc.name, strings.Join(tc.rules, ","))
    34  			if err != nil {
    35  				t.Fatalf("could not process %q: %+v", tc.name, err)
    36  			}
    37  
    38  			want, err := os.ReadFile(tc.want)
    39  			if err != nil {
    40  				t.Fatalf("could not read reference file %q: %+v", tc.want, err)
    41  			}
    42  
    43  			if got, want := got.Bytes(), want; !bytes.Equal(got, want) {
    44  				t.Fatalf("invalid generated code:\n%s", diff.Format(string(got), string(want)))
    45  			}
    46  		})
    47  	}
    48  }