go-hep.org/x/hep@v0.38.1/hbook/ann_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 hbook 6 7 import ( 8 "bytes" 9 "reflect" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 ) 14 15 func TestAnnotationYODA(t *testing.T) { 16 ann1 := make(Annotation) 17 ann1["title"] = "my title" 18 ann1["name"] = "h1d" 19 ann1["meta"] = "data" 20 21 got1, err := ann1.MarshalYODA() 22 if err != nil { 23 t.Fatalf("could not marshal annotation: %+v", err) 24 } 25 26 ann2 := make(Annotation) 27 err = ann2.UnmarshalYODA(got1) 28 if err != nil { 29 t.Fatalf("could not unmarshal annotation: %+v", err) 30 } 31 32 if !reflect.DeepEqual(ann1, ann2) { 33 t.Fatalf("r/w roundtrip failed:\ngot= %#v\nwant=%#v", ann2, ann1) 34 } 35 } 36 37 func TestAnnotationClone(t *testing.T) { 38 ann := make(Annotation) 39 ann["title"] = "my title" 40 ann["name"] = "h1d" 41 ann["meta"] = []float64{1.1, 2.1, 3.1, 4.1} 42 43 clo := ann.clone() 44 45 if got, want := clo, ann; !reflect.DeepEqual(got, want) { 46 t.Fatalf("cloning failed:\ngot= %#v\nwant=%#v", got, want) 47 } 48 49 got, err := clo.MarshalYODA() 50 if err != nil { 51 t.Fatalf("error: %+v", err) 52 } 53 54 want, err := ann.MarshalYODA() 55 if err != nil { 56 t.Fatalf("error: %+v", err) 57 } 58 59 if !bytes.Equal(got, want) { 60 t.Fatalf("ann differ:\n%s\n", 61 cmp.Diff( 62 string(want), 63 string(got), 64 ), 65 ) 66 } 67 68 // test the 2 values are indeed decoupled. 69 delete(clo, "meta") 70 clo["data"] = 42.42 71 72 got, err = ann.MarshalYODA() 73 if err != nil { 74 t.Fatalf("error: %+v", err) 75 } 76 77 if !bytes.Equal(got, want) { 78 t.Fatalf("ann differ:\n%s\n", 79 cmp.Diff( 80 string(want), 81 string(got), 82 ), 83 ) 84 } 85 }