go-hep.org/x/hep@v0.38.1/groot/rcmd/ls_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 rcmd_test
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/groot/rcmd"
    13  	"go-hep.org/x/hep/internal/diff"
    14  )
    15  
    16  func TestList(t *testing.T) {
    17  	loadRef := func(fname string) string {
    18  		t.Helper()
    19  		raw, err := os.ReadFile(fname)
    20  		if err != nil {
    21  			t.Fatalf("could not load reference file %q: %+v", fname, err)
    22  		}
    23  		return string(raw)
    24  	}
    25  
    26  	opts := []rcmd.ListOption{
    27  		rcmd.ListStreamers(true),
    28  		rcmd.ListTrees(true),
    29  	}
    30  
    31  	for _, tc := range []struct {
    32  		name string
    33  		opts []rcmd.ListOption
    34  		want string
    35  	}{
    36  		{
    37  			name: "../testdata/simple.root",
    38  			want: `=== [../testdata/simple.root] ===
    39  version: 60600
    40  TTree   tree    fake data (cycle=1)
    41  `,
    42  		},
    43  		{
    44  			name: "../testdata/simple.root",
    45  			opts: []rcmd.ListOption{
    46  				rcmd.ListTrees(true),
    47  			},
    48  			want: `=== [../testdata/simple.root] ===
    49  version: 60600
    50    TTree   tree      fake data (entries=4)
    51      one   "one/I"   TBranch
    52      two   "two/F"   TBranch
    53      three "three/C" TBranch
    54  `,
    55  		},
    56  		{
    57  			name: "../testdata/simple.root",
    58  			opts: opts,
    59  			want: loadRef("./testdata/simple.root-ls.txt"),
    60  		},
    61  		{
    62  			name: "../testdata/graphs.root",
    63  			opts: opts,
    64  			want: loadRef("./testdata/graphs.root-ls.txt"),
    65  		},
    66  		{
    67  			name: "../testdata/small-flat-tree.root",
    68  			opts: opts,
    69  			want: loadRef("./testdata/small-flat-tree.root-ls.txt"),
    70  		},
    71  		{
    72  			name: "../testdata/small-evnt-tree-fullsplit.root",
    73  			opts: opts,
    74  			want: loadRef("./testdata/small-evnt-tree-fullsplit.root-ls.txt"),
    75  		},
    76  		{
    77  			name: "../testdata/small-evnt-tree-nosplit.root",
    78  			opts: opts,
    79  			want: loadRef("./testdata/small-evnt-tree-nosplit.root-ls.txt"),
    80  		},
    81  		{
    82  			name: "../testdata/tcanvas.root",
    83  			want: loadRef("./testdata/tcanvas.root-ls.txt"),
    84  		},
    85  	} {
    86  		t.Run(tc.name, func(t *testing.T) {
    87  			out := new(strings.Builder)
    88  			err := rcmd.List(out, tc.name, tc.opts...)
    89  			if err != nil {
    90  				t.Fatalf("could not run root-ls: %+v", err)
    91  			}
    92  
    93  			if got, want := out.String(), tc.want; got != want {
    94  				t.Fatalf("invalid root-ls output:\n%s", diff.Format(got, want))
    95  			}
    96  		})
    97  	}
    98  }