blob: 3039f001e42d41e5e7a83662ca3876797047208a [file] [log] [blame]
Googlera46b4732023-04-04 03:31:23 -07001// Copyright 2023 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
15package com.google.devtools.build.lib.cmdline;
16
17import static com.google.common.truth.Truth.assertThat;
18import static com.google.devtools.build.lib.cmdline.LabelParser.Parts.parse;
19import static com.google.devtools.build.lib.cmdline.LabelParser.Parts.validateAndCreate;
20
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
25/** Tests for {@link LabelParser}. */
26@RunWith(JUnit4.class)
27public class LabelParserTest {
28
29 /** Checks that the javadoc of {@link LabelParser.Parts#parse} is accurate. */
30 @Test
31 public void parserTable() throws Exception {
32 assertThat(parse("foo/bar"))
33 .isEqualTo(validateAndCreate(null, false, false, "", false, "foo/bar", "foo/bar"));
34 assertThat(parse("...")).isEqualTo(validateAndCreate(null, false, false, "", true, "", "..."));
35 assertThat(parse("...:all"))
36 .isEqualTo(validateAndCreate(null, false, false, "", true, "all", "...:all"));
37 assertThat(parse("foo/..."))
38 .isEqualTo(validateAndCreate(null, false, false, "foo", true, "", "foo/..."));
39 assertThat(parse("//foo/bar"))
40 .isEqualTo(validateAndCreate(null, false, true, "foo/bar", false, "bar", "//foo/bar"));
41 assertThat(parse("//foo/..."))
42 .isEqualTo(validateAndCreate(null, false, true, "foo", true, "", "//foo/..."));
43 assertThat(parse("//foo/...:all"))
44 .isEqualTo(validateAndCreate(null, false, true, "foo", true, "all", "//foo/...:all"));
45 assertThat(parse("//foo/all"))
46 .isEqualTo(validateAndCreate(null, false, true, "foo/all", false, "all", "//foo/all"));
47 assertThat(parse("@repo"))
48 .isEqualTo(validateAndCreate("repo", false, true, "", false, "repo", "@repo"));
49 assertThat(parse("@@repo"))
50 .isEqualTo(validateAndCreate("repo", true, true, "", false, "repo", "@@repo"));
51 assertThat(parse("@repo//foo/bar"))
52 .isEqualTo(
53 validateAndCreate("repo", false, true, "foo/bar", false, "bar", "@repo//foo/bar"));
54 assertThat(parse("@@repo//foo/bar"))
55 .isEqualTo(
56 validateAndCreate("repo", true, true, "foo/bar", false, "bar", "@@repo//foo/bar"));
57 assertThat(parse(":quux"))
58 .isEqualTo(validateAndCreate(null, false, false, "", false, "quux", ":quux"));
59 assertThat(parse("foo/bar:quux"))
60 .isEqualTo(validateAndCreate(null, false, false, "foo/bar", false, "quux", "foo/bar:quux"));
61 assertThat(parse("//foo/bar:quux"))
62 .isEqualTo(
63 validateAndCreate(null, false, true, "foo/bar", false, "quux", "//foo/bar:quux"));
64 assertThat(parse("@repo//foo/bar:quux"))
65 .isEqualTo(
66 validateAndCreate(
67 "repo", false, true, "foo/bar", false, "quux", "@repo//foo/bar:quux"));
68 }
69}