go-hep.org/x/hep@v0.38.1/groot/cmd/root-gen-datareader/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  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/internal/diff"
    13  )
    14  
    15  func TestGenerate(t *testing.T) {
    16  	newCtx := func(file, tree string) *Context {
    17  		const (
    18  			gen     = true
    19  			verbose = false
    20  		)
    21  		return newContext("event", file, tree, gen, verbose)
    22  	}
    23  
    24  	for _, tc := range []struct {
    25  		ctx  *Context
    26  		want string
    27  	}{
    28  		{
    29  			ctx:  newCtx("../../testdata/simple.root", "tree"),
    30  			want: "testdata/simple.root.txt",
    31  		},
    32  		{
    33  			ctx:  newCtx("../../testdata/small-flat-tree.root", "tree"),
    34  			want: "testdata/small-flat-tree.root.txt",
    35  		},
    36  		{
    37  			ctx:  newCtx("../../testdata/small-evnt-tree-fullsplit.root", "tree"),
    38  			want: "testdata/small-evnt-tree.root.txt",
    39  		},
    40  		{
    41  			ctx:  newCtx("../../testdata/small-evnt-tree-nosplit.root", "tree"),
    42  			want: "testdata/small-evnt-tree.root.txt",
    43  		},
    44  		{
    45  			ctx:  newCtx("../../testdata/x-flat-tree.root", "tree"),
    46  			want: "testdata/x-flat-tree.root.txt",
    47  		},
    48  		{
    49  			ctx:  newCtx("../../testdata/leaves.root", "tree"),
    50  			want: "testdata/leaves.root.txt",
    51  		},
    52  	} {
    53  		t.Run(tc.ctx.File, func(t *testing.T) {
    54  			o := new(strings.Builder)
    55  			err := process(o, tc.ctx)
    56  			if err != nil {
    57  				t.Fatalf("error: %+v", err)
    58  			}
    59  
    60  			want, err := os.ReadFile(tc.want)
    61  			if err != nil {
    62  				t.Fatalf("could not read golden file %q: %+v", tc.want, err)
    63  			}
    64  
    65  			if got, want := o.String(), string(want); got != want {
    66  				t.Fatalf("invalid code generation:\n%s", diff.Format(got, want))
    67  			}
    68  		})
    69  	}
    70  }