blob: 558edae54b066ded0d9e5720a9b998187e599274 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanskicbd02be2021-09-24 13:39:12 +02004package toolbase
5
6import (
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10)
11
12func TestBazelLabelParse(t *testing.T) {
13 for i, te := range []struct {
14 p string
15 t *BazelLabel
16 }{
Tim Windelschmidtafeb4c42024-07-17 21:37:26 +020017 {"//foo/bar", &BazelLabel{"@", []string{"foo", "bar"}, "bar"}},
18 {"//foo/bar:baz", &BazelLabel{"@", []string{"foo", "bar"}, "baz"}},
19 {"//:foo", &BazelLabel{"@", nil, "foo"}},
Serge Bazanskicbd02be2021-09-24 13:39:12 +020020
21 {"@test//foo/bar", &BazelLabel{"test", []string{"foo", "bar"}, "bar"}},
22 {"@test//foo/bar:baz", &BazelLabel{"test", []string{"foo", "bar"}, "baz"}},
23 {"@test//:foo", &BazelLabel{"test", nil, "foo"}},
24
25 {"", nil},
26 {"//", nil},
27 {"//foo:bar/foo", nil},
28 {"//foo//bar/foo", nil},
29 {"/foo/bar/foo", nil},
30 {"foo/bar/foo", nil},
31 {"@//foo/bar/foo", nil},
32 {"@foo/bar//foo/bar/foo", nil},
33 {"@foo:bar//foo/bar/foo", nil},
34 {"foo//foo/bar/foo", nil},
35 } {
36 want := te.t
37 got := ParseBazelLabel(te.p)
38 if diff := cmp.Diff(want, got); diff != "" {
39 t.Errorf("case %d (%q): %s", i, te.p, diff)
40 }
41 }
42}
43
44func TestBazelLabelString(t *testing.T) {
45 for i, te := range []struct {
46 in string
47 want string
48 }{
Tim Windelschmidtafeb4c42024-07-17 21:37:26 +020049 {"//foo/bar", "@@//foo/bar:bar"},
50 {"//foo:bar", "@@//foo:bar"},
Serge Bazanskicbd02be2021-09-24 13:39:12 +020051 {"@com_github_example//:run", "@com_github_example//:run"},
52 } {
53 l := ParseBazelLabel(te.in)
54 if l == nil {
55 t.Errorf("case %d: wanted %q, got nil", i, te.want)
56 continue
57 }
58 if want, got := te.want, l.String(); want != got {
59 t.Errorf("case %d: wanted %q, got %q", i, want, got)
60 }
61 }
62}