...

Source file src/github.com/bazelbuild/buildtools/labels/labels_test.go

Documentation: github.com/bazelbuild/buildtools/labels

     1  /*
     2   * Copyright 2020 Google LLC
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package labels
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  var parseLabelTests = []struct {
    24  	in     string
    25  	repo   string
    26  	pkg    string
    27  	target string
    28  }{
    29  	{"//devtools/buildozer:rule", "", "devtools/buildozer", "rule"},
    30  	{"devtools/buildozer:rule", "", "devtools/buildozer", "rule"},
    31  	{"//devtools/buildozer", "", "devtools/buildozer", "buildozer"},
    32  	{"//base", "", "base", "base"},
    33  	{"//base:", "", "base", "base"},
    34  	{"@r//devtools/buildozer:rule", "r", "devtools/buildozer", "rule"},
    35  	{"@r//devtools/buildozer", "r", "devtools/buildozer", "buildozer"},
    36  	{"@r//base", "r", "base", "base"},
    37  	{"@r//base:", "r", "base", "base"},
    38  	{"@foo", "foo", "", "foo"},
    39  	{":label", "", "", "label"},
    40  	{"label", "", "", "label"},
    41  	{"/abs/path/to/WORKSPACE:rule", "", "/abs/path/to/WORKSPACE", "rule"},
    42  }
    43  
    44  func TestParseLabel(t *testing.T) {
    45  	for i, tt := range parseLabelTests {
    46  		l := Parse(tt.in)
    47  		if l.Repository != tt.repo || l.Package != tt.pkg || l.Target != tt.target {
    48  			t.Errorf("%d. Parse(%q) => (%q, %q, %q), want (%q, %q, %q)",
    49  				i, tt.in, l.Repository, l.Package, l.Target, tt.repo, tt.pkg, tt.target)
    50  		}
    51  	}
    52  }
    53  
    54  var shortenLabelTests = []struct {
    55  	in     string
    56  	pkg    string
    57  	result string
    58  }{
    59  	{"//devtools/buildozer:rule", "devtools/buildozer", ":rule"},
    60  	{"@//devtools/buildozer:rule", "devtools/buildozer", ":rule"},
    61  	{"//devtools/buildozer:rule", "devtools", "//devtools/buildozer:rule"},
    62  	{"//base:rule", "devtools", "//base:rule"},
    63  	{"//base:base", "devtools", "//base"},
    64  	{"//base", "base", ":base"},
    65  	{"//devtools/buildozer:buildozer", "", "//devtools/buildozer"},
    66  	{"@r//devtools/buildozer:buildozer", "devtools/buildozer", "@r//devtools/buildozer"},
    67  	{"@r//devtools/buildozer", "devtools/buildozer", "@r//devtools/buildozer"},
    68  	{"@r//devtools", "devtools", "@r//devtools"},
    69  	{"@r:rule", "", "@r:rule"},
    70  	{"@r", "", "@r"},
    71  	{"@foo//:foo", "", "@foo"},
    72  	{"@foo//devtools:foo", "", "@foo//devtools:foo"},
    73  	{"@foo//devtools:foo", "devtools", "@foo//devtools:foo"},
    74  	{"@foo//foo:foo", "", "@foo//foo"},
    75  	{":local", "", ":local"},
    76  	{"something else", "", "something else"},
    77  	{"/path/to/file", "path/to", "/path/to/file"},
    78  	{"\"//baz\"", "", "\"//baz\""},
    79  }
    80  
    81  func TestShortenLabel(t *testing.T) {
    82  	for i, tt := range shortenLabelTests {
    83  		result := Shorten(tt.in, tt.pkg)
    84  		if result != tt.result {
    85  			t.Errorf("%d. Shorten(%q, %q) => %q, want %q",
    86  				i, tt.in, tt.pkg, result, tt.result)
    87  		}
    88  	}
    89  }
    90  
    91  var labelsEqualTests = []struct {
    92  	label1   string
    93  	label2   string
    94  	pkg      string
    95  	expected bool
    96  }{
    97  	{"//devtools/buildozer:rule", "rule", "devtools/buildozer", true},
    98  	{"//devtools/buildozer:rule", "rule:jar", "devtools", false},
    99  }
   100  
   101  func TestLabelsEqual(t *testing.T) {
   102  	for i, tt := range labelsEqualTests {
   103  		if got := Equal(tt.label1, tt.label2, tt.pkg); got != tt.expected {
   104  			t.Errorf("%d. Equal(%q, %q, %q) => %v, want %v",
   105  				i, tt.label1, tt.label2, tt.pkg, got, tt.expected)
   106  		}
   107  	}
   108  }
   109  

View as plain text