Serge Bazanski | cbd02be | 2021-09-24 13:39:12 +0200 | [diff] [blame] | 1 | package toolbase |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/google/go-cmp/cmp" |
| 7 | ) |
| 8 | |
| 9 | func TestBazelLabelParse(t *testing.T) { |
| 10 | for i, te := range []struct { |
| 11 | p string |
| 12 | t *BazelLabel |
| 13 | }{ |
| 14 | {"//foo/bar", &BazelLabel{"dev_source_monogon", []string{"foo", "bar"}, "bar"}}, |
| 15 | {"//foo/bar:baz", &BazelLabel{"dev_source_monogon", []string{"foo", "bar"}, "baz"}}, |
| 16 | {"//:foo", &BazelLabel{"dev_source_monogon", nil, "foo"}}, |
| 17 | |
| 18 | {"@test//foo/bar", &BazelLabel{"test", []string{"foo", "bar"}, "bar"}}, |
| 19 | {"@test//foo/bar:baz", &BazelLabel{"test", []string{"foo", "bar"}, "baz"}}, |
| 20 | {"@test//:foo", &BazelLabel{"test", nil, "foo"}}, |
| 21 | |
| 22 | {"", nil}, |
| 23 | {"//", nil}, |
| 24 | {"//foo:bar/foo", nil}, |
| 25 | {"//foo//bar/foo", nil}, |
| 26 | {"/foo/bar/foo", nil}, |
| 27 | {"foo/bar/foo", nil}, |
| 28 | {"@//foo/bar/foo", nil}, |
| 29 | {"@foo/bar//foo/bar/foo", nil}, |
| 30 | {"@foo:bar//foo/bar/foo", nil}, |
| 31 | {"foo//foo/bar/foo", nil}, |
| 32 | } { |
| 33 | want := te.t |
| 34 | got := ParseBazelLabel(te.p) |
| 35 | if diff := cmp.Diff(want, got); diff != "" { |
| 36 | t.Errorf("case %d (%q): %s", i, te.p, diff) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestBazelLabelString(t *testing.T) { |
| 42 | for i, te := range []struct { |
| 43 | in string |
| 44 | want string |
| 45 | }{ |
| 46 | {"//foo/bar", "@dev_source_monogon//foo/bar:bar"}, |
| 47 | {"//foo:bar", "@dev_source_monogon//foo:bar"}, |
| 48 | {"@com_github_example//:run", "@com_github_example//:run"}, |
| 49 | } { |
| 50 | l := ParseBazelLabel(te.in) |
| 51 | if l == nil { |
| 52 | t.Errorf("case %d: wanted %q, got nil", i, te.want) |
| 53 | continue |
| 54 | } |
| 55 | if want, got := te.want, l.String(); want != got { |
| 56 | t.Errorf("case %d: wanted %q, got %q", i, want, got) |
| 57 | } |
| 58 | } |
| 59 | } |