github.com/googleapis/api-linter@v1.65.2/locations/descriptor_locations_test.go (about)

     1  // Copyright 2019 Google LLC
     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  // 		https://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  package locations
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  func TestDescriptorName(t *testing.T) {
    25  	f := parse(t, `
    26  		message Foo {
    27  		  string bar = 1;
    28  		  map<string, string> baz = 2;
    29  		}
    30  	`)
    31  
    32  	tests := []struct {
    33  		testName string
    34  		d        desc.Descriptor
    35  		wantSpan []int32
    36  	}{
    37  		{"Message", f.GetMessageTypes()[0], []int32{2, 8, 11}},
    38  		{"Field", f.GetMessageTypes()[0].GetFields()[0], []int32{3, 9, 12}},
    39  		{"MapField", f.GetMessageTypes()[0].GetFields()[1], []int32{4, 22, 25}},
    40  	}
    41  	for _, test := range tests {
    42  		t.Run(test.testName, func(t *testing.T) {
    43  			if diff := cmp.Diff(DescriptorName(test.d).Span, test.wantSpan); diff != "" {
    44  				t.Errorf(diff)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestFileDescriptorName(t *testing.T) {
    51  	f := parse(t, `
    52  		message Foo {}
    53  	`)
    54  	if got := DescriptorName(f); got != nil {
    55  		t.Errorf("%v", got)
    56  	}
    57  }