github.com/googleapis/api-linter@v1.65.2/locations/method_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 ) 22 23 func TestMethodRequestType(t *testing.T) { 24 f := parse(t, ` 25 service Library { 26 rpc GetBook(GetBookRequest) returns (Book); 27 } 28 message GetBookRequest {} 29 message Book {} 30 `) 31 loc := MethodRequestType(f.GetServices()[0].GetMethods()[0]) 32 // Three character span: line, start column, end column. 33 if diff := cmp.Diff(loc.GetSpan(), []int32{3, 14, 28}); diff != "" { 34 t.Errorf(diff) 35 } 36 } 37 38 func TestMethodResponseType(t *testing.T) { 39 f := parse(t, ` 40 service Library { 41 rpc GetBook(GetBookRequest) returns (Book); 42 } 43 message GetBookRequest {} 44 message Book {} 45 `) 46 loc := MethodResponseType(f.GetServices()[0].GetMethods()[0]) 47 // Three character span: line, start column, end column. 48 if diff := cmp.Diff(loc.GetSpan(), []int32{3, 39, 43}); diff != "" { 49 t.Errorf(diff) 50 } 51 } 52 53 func TestMethodHTTPRule(t *testing.T) { 54 f := parse(t, ` 55 import "google/api/annotations.proto"; 56 service Library { 57 rpc GetBook(GetBookRequest) returns (Book) { 58 option (google.api.http) = { 59 get: "/v1/{name=publishers/*/books/*}" 60 }; 61 } 62 } 63 message GetBookRequest{} 64 message Book {} 65 `) 66 loc := MethodHTTPRule(f.GetServices()[0].GetMethods()[0]) 67 // Four character span: start line, start column, end line, end column. 68 if diff := cmp.Diff(loc.GetSpan(), []int32{5, 4, 7, 6}); diff != "" { 69 t.Errorf(diff) 70 } 71 } 72 73 func TestMethodOperationInfo(t *testing.T) { 74 f := parse(t, ` 75 import "google/longrunning/operations.proto"; 76 service Library { 77 rpc WriteBook(WriteBookRequest) returns (google.longrunning.Operation) { 78 option (google.longrunning.operation_info) = { 79 response_type: "WriteBookResponse" 80 metadata_type: "WriteBookMetadata" 81 }; 82 } 83 } 84 message WriteBookRequest {} 85 `) 86 loc := MethodOperationInfo(f.GetServices()[0].GetMethods()[0]) 87 // Four character span: start line, start column, end line, end column. 88 if diff := cmp.Diff(loc.GetSpan(), []int32{5, 4, 8, 6}); diff != "" { 89 t.Errorf(diff) 90 } 91 } 92 93 func TestMethodSignature(t *testing.T) { 94 f := parse(t, ` 95 import "google/api/client.proto"; 96 service Library { 97 rpc GetBook(GetBookRequest) returns (Book) { 98 option (google.api.method_signature) = "name"; 99 option (google.api.method_signature) = "name,read_mask"; 100 } 101 } 102 message GetBookRequest{} 103 message Book {} 104 `) 105 for _, test := range []struct { 106 name string 107 index int 108 want []int32 109 }{ 110 {"First", 0, []int32{5, 4, 50}}, 111 {"Second", 1, []int32{6, 4, 60}}, 112 } { 113 loc := MethodSignature(f.GetServices()[0].GetMethods()[0], test.index) 114 // Four character span: start line, start column, end line, end column. 115 if diff := cmp.Diff(loc.GetSpan(), test.want); diff != "" { 116 t.Errorf(diff) 117 } 118 } 119 } 120 121 func TestMethodOption(t *testing.T) { 122 f := parse(t, ` 123 service Library { 124 rpc GetBook(GetBookRequest) returns (Book) { 125 option deprecated = true; 126 } 127 rpc UpdateBook(UpdateBookRequest) returns (Book) {} 128 } 129 message GetBookRequest{} 130 message Book {} 131 message UpdateBookRequest {} 132 `) 133 134 for _, test := range []struct { 135 name string 136 methodIdx int 137 want []int32 138 }{ 139 {"OptionSet", 0, []int32{4, 4, 29}}, 140 {"OptionNotSet", 1, nil}, 141 } { 142 t.Run(test.name, func(t *testing.T) { 143 // field number of the deprecated option == 33 144 loc := MethodOption(f.GetServices()[0].GetMethods()[test.methodIdx], 33) 145 if diff := cmp.Diff(loc.GetSpan(), test.want); diff != "" { 146 t.Errorf("Diff: %s", diff) 147 } 148 }) 149 } 150 }