github.com/wolfd/bazel-gazelle@v0.14.0/internal/label/label_test.go (about) 1 /* Copyright 2017 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package label 17 18 import ( 19 "reflect" 20 "testing" 21 ) 22 23 func TestLabelString(t *testing.T) { 24 for _, spec := range []struct { 25 l Label 26 want string 27 }{ 28 { 29 l: Label{Name: "foo"}, 30 want: "//:foo", 31 }, { 32 l: Label{Pkg: "foo/bar", Name: "baz"}, 33 want: "//foo/bar:baz", 34 }, { 35 l: Label{Pkg: "foo/bar", Name: "bar"}, 36 want: "//foo/bar", 37 }, { 38 l: Label{Repo: "com_example_repo", Pkg: "foo/bar", Name: "baz"}, 39 want: "@com_example_repo//foo/bar:baz", 40 }, { 41 l: Label{Repo: "com_example_repo", Pkg: "foo/bar", Name: "bar"}, 42 want: "@com_example_repo//foo/bar", 43 }, { 44 l: Label{Relative: true, Name: "foo"}, 45 want: ":foo", 46 }, 47 } { 48 if got, want := spec.l.String(), spec.want; got != want { 49 t.Errorf("%#v.String() = %q; want %q", spec.l, got, want) 50 } 51 } 52 } 53 54 func TestParse(t *testing.T) { 55 for _, tc := range []struct { 56 str string 57 want Label 58 wantErr bool 59 }{ 60 {str: "", wantErr: true}, 61 {str: "@//:", wantErr: true}, 62 {str: "@//:a", wantErr: true}, 63 {str: "@a:b", wantErr: true}, 64 {str: ":a", want: Label{Name: "a", Relative: true}}, 65 {str: "a", want: Label{Name: "a", Relative: true}}, 66 {str: "//:a", want: Label{Name: "a", Relative: false}}, 67 {str: "//a", want: Label{Pkg: "a", Name: "a"}}, 68 {str: "//a/b", want: Label{Pkg: "a/b", Name: "b"}}, 69 {str: "//a:b", want: Label{Pkg: "a", Name: "b"}}, 70 {str: "@a//b", want: Label{Repo: "a", Pkg: "b", Name: "b"}}, 71 {str: "@a//b:c", want: Label{Repo: "a", Pkg: "b", Name: "c"}}, 72 {str: "//api_proto:api.gen.pb.go_checkshtest", want: Label{Pkg: "api_proto", Name: "api.gen.pb.go_checkshtest"}}, 73 } { 74 got, err := Parse(tc.str) 75 if err != nil && !tc.wantErr { 76 t.Errorf("for string %q: got error %s ; want success", tc.str, err) 77 continue 78 } 79 if err == nil && tc.wantErr { 80 t.Errorf("for string %q: got label %s ; want error", tc.str, got) 81 continue 82 } 83 if !reflect.DeepEqual(got, tc.want) { 84 t.Errorf("for string %q: got %s ; want %s", tc.str, got, tc.want) 85 } 86 } 87 }