go-hep.org/x/hep@v0.38.1/hbook/yoda_test.go (about) 1 // Copyright ©2018 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 "fmt" 9 "testing" 10 ) 11 12 func TestReadYODAHeader(t *testing.T) { 13 const mark = "BEGIN YODA_HISTO1D" 14 for _, tc := range []struct { 15 str string 16 want string 17 vers int 18 err error 19 }{ 20 { 21 str: "BEGIN YODA_HISTO1D /name\n", 22 want: "/name", 23 vers: 1, 24 }, 25 { 26 str: "BEGIN YODA_HISTO1D /name with whitespace\n", 27 want: "/name with whitespace", 28 vers: 1, 29 }, 30 { 31 str: "BEGIN YODA_HISTO1D_V2 /name\n", 32 want: "/name", 33 vers: 2, 34 }, 35 { 36 str: "BEGIN YODA_HISTO1D_V2 /name with whitespace\n", 37 want: "/name with whitespace", 38 vers: 2, 39 }, 40 { 41 str: "BEGIN YODA /name", 42 want: "", 43 err: fmt.Errorf("hbook: could not find %s line", mark), 44 }, 45 { 46 str: "BEGIN YODA /name\n", 47 want: "", 48 err: fmt.Errorf("hbook: could not find %s mark", mark), 49 }, 50 { 51 str: "\nBEGIN YODA /name", 52 want: "", 53 err: fmt.Errorf("hbook: could not find %s mark", mark), 54 }, 55 { 56 str: "\nBEGIN YODA /name\n", 57 want: "", 58 err: fmt.Errorf("hbook: could not find %s mark", mark), 59 }, 60 { 61 str: " BEGIN YODA /name\n", 62 want: "", 63 err: fmt.Errorf("hbook: could not find %s mark", mark), 64 }, 65 } { 66 t.Run(tc.want, func(t *testing.T) { 67 name, vers, err := readYODAHeader(newRBuffer([]byte(tc.str)), mark) 68 if err == nil && tc.err != nil { 69 t.Fatalf("got err=nil, want=%v", tc.err.Error()) 70 } 71 if err != nil && tc.err == nil { 72 t.Fatalf("got=%v, want=nil", err.Error()) 73 } 74 if err != nil && tc.err != nil { 75 if got, want := err.Error(), tc.err.Error(); got != want { 76 t.Fatalf("got error=%v, want=%v", got, want) 77 } 78 } 79 if got, want := name, tc.want; got != want { 80 t.Fatalf("invalid name: got: %q, want: %q", got, want) 81 } 82 if got, want := vers, tc.vers; got != want { 83 t.Fatalf("invalid version: got: %d, want: %d", got, want) 84 } 85 }) 86 } 87 }