go-hep.org/x/hep@v0.38.1/hbook/ntup/ntroot/ntroot_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 ntroot_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"go-hep.org/x/hep/hbook/ntup/ntroot"
    12  )
    13  
    14  func TestOpen(t *testing.T) {
    15  	for _, tc := range []struct {
    16  		name string
    17  		tree string
    18  		err  error
    19  	}{
    20  		{
    21  			name: "../../../groot/testdata/simple.root",
    22  			tree: "tree",
    23  		},
    24  		{
    25  			name: "../../../groot/testdata/graphs.root",
    26  			tree: "tg",
    27  			err:  fmt.Errorf(`ROOT object "tg" is not a tree`),
    28  		},
    29  		{
    30  			name: "../../../groot/testdata/simple.root",
    31  			tree: "treeXXX",
    32  			err:  fmt.Errorf(`could not find ROOT tree "treeXXX": riofs: simple.root: could not find key "treeXXX;9999"`),
    33  		},
    34  		{
    35  			name: "../../../groot/testdata/simple.rootXXX",
    36  			tree: "tree",
    37  			err:  fmt.Errorf(`could not open ROOT file: riofs: unable to open "../../../groot/testdata/simple.rootXXX": riofs: no ROOT plugin to open [../../../groot/testdata/simple.rootXXX] (scheme=)`),
    38  		},
    39  	} {
    40  		t.Run(tc.name+":"+tc.tree, func(t *testing.T) {
    41  			nt, err := ntroot.Open(tc.name, tc.tree)
    42  			if err == nil {
    43  				_ = nt.DB().Close()
    44  			}
    45  
    46  			switch {
    47  			case err != nil && tc.err != nil:
    48  				if got, want := err.Error(), tc.err.Error(); got != want {
    49  					t.Fatalf("invalid error:\ngot= %v\nwant=%v", got, want)
    50  				}
    51  			case err != nil && tc.err == nil:
    52  				t.Fatalf("unexpected error: %+v", err)
    53  			case err == nil && tc.err != nil:
    54  				t.Fatalf("expected an error (got=%v, want=%v)", err, tc.err)
    55  			case err == nil && tc.err == nil:
    56  				// ok.
    57  			}
    58  		})
    59  	}
    60  }