github.com/jgbaldwinbrown/perf@v0.1.1/benchproc/internal/parse/projection_test.go (about) 1 // Copyright 2022 The Go 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 parse 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 func TestParseProjection(t *testing.T) { 13 check := func(proj string, want ...string) { 14 t.Helper() 15 p, err := ParseProjection(proj) 16 if err != nil { 17 t.Errorf("%s: unexpected error %s", proj, err) 18 return 19 } 20 var got []string 21 for _, part := range p { 22 got = append(got, part.String()) 23 } 24 if !reflect.DeepEqual(got, want) { 25 t.Errorf("%s: got %v, want %v", proj, got, want) 26 } 27 } 28 checkErr := func(proj, error string, pos int) { 29 t.Helper() 30 _, err := ParseProjection(proj) 31 if se, _ := err.(*SyntaxError); se == nil || se.Msg != error || se.Off != pos { 32 t.Errorf("%s: want error %s at %d; got %s", proj, error, pos, err) 33 } 34 } 35 36 check("") 37 check("a,b", "a", "b") 38 check("a, b", "a", "b") 39 check("a b", "a", "b") 40 checkErr("a,,b", "expected key", 2) 41 check("a,.name", "a", ".name") 42 check("a,/b", "a", "/b") 43 44 check("a@alpha, b@num", "a@alpha", "b@num") 45 checkErr("a@", "expected named sort order or parenthesized list", 2) 46 checkErr("a@,b", "expected named sort order or parenthesized list", 2) 47 48 check("a@(1 2), b@(3 4)", "a@(1 2)", "b@(3 4)") 49 checkErr("a@(", "missing )", 3) 50 checkErr("a@(,", "missing )", 3) 51 checkErr("a@()", "nothing to match", 3) 52 }