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

     1  // Copyright ©2019 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 // import "go-hep.org/x/hep/cmd/arrow2root"
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/groot/rcmd"
    14  )
    15  
    16  func TestConvert(t *testing.T) {
    17  	tmp, err := os.MkdirTemp("", "arrow2root-")
    18  	if err != nil {
    19  		t.Fatalf("could not create tmpdir: %+v", err)
    20  	}
    21  	defer os.RemoveAll(tmp)
    22  
    23  	for _, tc := range []struct {
    24  		name   string
    25  		panics string
    26  	}{
    27  		{
    28  			name: "testdata/primitives.file.data",
    29  		},
    30  		{
    31  			name: "testdata/arrays.file.data",
    32  		},
    33  		{
    34  			name: "testdata/strings.file.data",
    35  		},
    36  		{
    37  			name: "testdata/fixed_size_binaries.file.data",
    38  		},
    39  		{
    40  			name: "testdata/lists.file.data",
    41  		},
    42  		{
    43  			name:   "testdata/structs.file.data",
    44  			panics: "invalid ARROW data-type: *arrow.StructType", // FIXME(sbinet): needs non-flat-tree writer support
    45  		},
    46  	} {
    47  		t.Run(tc.name, func(t *testing.T) {
    48  			if tc.panics != "" {
    49  				defer func() {
    50  					err := recover()
    51  					if err == nil {
    52  						t.Fatalf("expected a panic (%s)", tc.panics)
    53  					}
    54  					if got, want := err.(error).Error(), tc.panics; got != want {
    55  						t.Fatalf("invalid panic message:\ngot= %v\nwant=%v", got, want)
    56  					}
    57  				}()
    58  			}
    59  			oname := filepath.Join(tmp, filepath.Base(tc.name)+".root")
    60  			tname := "tree"
    61  			err := process(oname, tname, tc.name)
    62  			if err != nil {
    63  				t.Fatalf("could not convert %q: %+v", tc.name, err)
    64  			}
    65  
    66  			var (
    67  				out  = new(strings.Builder)
    68  				deep = true
    69  			)
    70  			err = rcmd.Dump(out, oname, deep, nil)
    71  			if err != nil {
    72  				t.Fatalf("could not dump ROOT file %q: %+v", oname, err)
    73  			}
    74  
    75  			want, err := os.ReadFile(tc.name + ".txt")
    76  			if err != nil {
    77  				t.Fatalf("could not load reference file %q: %+v", tc.name, err)
    78  			}
    79  
    80  			if got, want := out.String(), string(want); got != want {
    81  				t.Fatalf("invalid root-dump output:\ngot:\n%s\nwant:\n%s\n", got, want)
    82  			}
    83  		})
    84  	}
    85  }