go-hep.org/x/hep@v0.38.1/groot/rdict/object_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 rdict
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"go-hep.org/x/hep/groot/rbytes"
    12  )
    13  
    14  func TestObjectFrom(t *testing.T) {
    15  	sictx := StreamerInfos
    16  	loadSI := func(name string) rbytes.StreamerInfo {
    17  		t.Helper()
    18  		si, err := sictx.StreamerInfo(name, -1)
    19  		if err != nil {
    20  			t.Fatalf("could not load streamer %q: %+v", name, err)
    21  		}
    22  		return si
    23  	}
    24  
    25  	for _, tc := range []struct {
    26  		name string
    27  		si   rbytes.StreamerInfo
    28  	}{
    29  		{
    30  			name: "TObject",
    31  			si:   loadSI("TObject"),
    32  		},
    33  	} {
    34  		t.Run(tc.name, func(t *testing.T) {
    35  			obj := ObjectFrom(tc.si, sictx)
    36  
    37  			if got, want := obj.Class(), tc.si.Name(); got != want {
    38  				t.Fatalf("invalid class name: got=%v, want=%v", got, want)
    39  			}
    40  
    41  			if got, want := obj.RVersion(), int16(tc.si.ClassVersion()); got != want {
    42  				t.Fatalf("invalid class version: got=%v, want=%v", got, want)
    43  			}
    44  
    45  			wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    46  			_, err := obj.MarshalROOT(wbuf)
    47  			if err != nil {
    48  				t.Fatalf("could not write object: %+v", err)
    49  			}
    50  
    51  			rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil)
    52  
    53  			got := ObjectFrom(tc.si, sictx)
    54  			err = got.UnmarshalROOT(rbuf)
    55  			if err != nil {
    56  				t.Fatalf("could not read object: %+v", err)
    57  			}
    58  
    59  			if !reflect.DeepEqual(got, obj) {
    60  				t.Fatalf("round-trip failed:\ngot= %#v\nwant=%#v", got, obj)
    61  			}
    62  		})
    63  	}
    64  }