github.com/googleapis/api-linter@v1.65.2/rules/aip0192/deprecated_test.go (about) 1 // Copyright 2021 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 aip0192 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 // These are split up since templating doesn't play nicely with inserting protobuf options. 24 func TestValidDescriptor(t *testing.T) { 25 file := testutils.ParseProto3String(t, ` 26 // A library service. 27 service Library { 28 // Retrieves a book. 29 rpc GetBook(GetBookRequest) returns (Book); 30 } 31 message GetBookRequest {} 32 message Book {} 33 `) 34 35 serviceProblems := testutils.Problems{} 36 if diff := serviceProblems.Diff(deprecatedComment.Lint(file)); diff != "" { 37 t.Error(diff) 38 } 39 40 methodProblems := testutils.Problems{} 41 if diff := methodProblems.Diff(deprecatedComment.Lint(file)); diff != "" { 42 t.Error(diff) 43 } 44 } 45 46 func TestDeprecatedMethod(t *testing.T) { 47 tests := []struct { 48 testName string 49 MethodComment string 50 problems testutils.Problems 51 }{ 52 {"ValidMethodDeprecated", "// Deprecated: Don't use this.\n// Method comment.", nil}, 53 {"InvalidMethodDeprecated", "// Method comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 54 } 55 56 for _, test := range tests { 57 t.Run(test.testName, func(t *testing.T) { 58 file := testutils.ParseProto3Tmpl(t, ` 59 service Library { 60 61 {{.MethodComment}} 62 rpc GetBook(GetBookRequest) returns (Book) { 63 option deprecated = true; 64 } 65 } 66 message GetBookRequest {} 67 message Book {} 68 `, test) 69 70 problems := deprecatedComment.Lint(file) 71 if diff := test.problems.SetDescriptor(file.GetServices()[0].GetMethods()[0]).Diff(problems); diff != "" { 72 t.Error(diff) 73 } 74 }) 75 } 76 } 77 78 func TestDeprecatedService(t *testing.T) { 79 tests := []struct { 80 testName string 81 ServiceComment string 82 problems testutils.Problems 83 }{ 84 {"ValidServiceDeprecated", "// Deprecated: Don't use this.\n// Service comment.", nil}, 85 {"InvalidServiceDeprecated", "// Service comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 86 } 87 88 for _, test := range tests { 89 t.Run(test.testName, func(t *testing.T) { 90 file := testutils.ParseProto3Tmpl(t, ` 91 {{.ServiceComment}} 92 service Library { 93 option deprecated = true; 94 rpc GetBook(GetBookRequest) returns (Book); 95 } 96 message GetBookRequest {} 97 message Book {} 98 `, test) 99 100 problems := deprecatedComment.Lint(file) 101 if diff := test.problems.SetDescriptor(file.GetServices()[0]).Diff(problems); diff != "" { 102 t.Error(diff) 103 } 104 }) 105 } 106 } 107 108 func TestDeprecatedField(t *testing.T) { 109 tests := []struct { 110 testName string 111 FieldComment string 112 problems testutils.Problems 113 }{ 114 {"ValidFieldDeprecated", "// Deprecated: Don't use this.\n// Field comment.", nil}, 115 {"InvalidFieldDeprecated", "// Field comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 116 } 117 118 for _, test := range tests { 119 t.Run(test.testName, func(t *testing.T) { 120 file := testutils.ParseProto3Tmpl(t, ` 121 message GetBookRequest { 122 {{.FieldComment}} 123 string name = 1 [deprecated = true]; 124 } 125 `, test) 126 127 problems := deprecatedComment.Lint(file) 128 if diff := test.problems.SetDescriptor(file.GetMessageTypes()[0].GetFields()[0]).Diff(problems); diff != "" { 129 t.Error(diff) 130 } 131 }) 132 } 133 } 134 135 func TestDeprecatedEnum(t *testing.T) { 136 tests := []struct { 137 testName string 138 EnumComment string 139 problems testutils.Problems 140 }{ 141 {"ValidEnumDeprecated", "// Deprecated: Don't use this.\n// Enum comment.", nil}, 142 {"InvalidEnumDeprecated", "// Enum comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 143 } 144 145 for _, test := range tests { 146 t.Run(test.testName, func(t *testing.T) { 147 file := testutils.ParseProto3Tmpl(t, ` 148 {{.EnumComment}} 149 enum State { 150 option deprecated = true; 151 152 STATE_UNSPECIFIED = 0; 153 } 154 `, test) 155 156 problems := deprecatedComment.Lint(file) 157 if diff := test.problems.SetDescriptor(file.GetEnumTypes()[0]).Diff(problems); diff != "" { 158 t.Error(diff) 159 } 160 }) 161 } 162 } 163 164 func TestDeprecatedEnumValue(t *testing.T) { 165 tests := []struct { 166 testName string 167 EnumValueComment string 168 problems testutils.Problems 169 }{ 170 {"ValidEnumValueDeprecated", "// Deprecated: Don't use this.\n// EnumValue comment.", nil}, 171 {"InvalidEnumValueDeprecated", "// EnumValue comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 172 } 173 174 for _, test := range tests { 175 t.Run(test.testName, func(t *testing.T) { 176 file := testutils.ParseProto3Tmpl(t, ` 177 enum State { 178 {{.EnumValueComment}} 179 STATE_UNSPECIFIED = 0 [deprecated = true]; 180 } 181 `, test) 182 183 problems := deprecatedComment.Lint(file) 184 if diff := test.problems.SetDescriptor(file.GetEnumTypes()[0].GetValues()[0]).Diff(problems); diff != "" { 185 t.Error(diff) 186 } 187 }) 188 } 189 } 190 191 func TestDeprecatedMessage(t *testing.T) { 192 tests := []struct { 193 testName string 194 MessageComment string 195 problems testutils.Problems 196 }{ 197 {"ValidMessageDeprecated", "// Deprecated: Don't use this.\n// Message comment.", nil}, 198 {"InvalidMessageDeprecated", "// Message comment.", testutils.Problems{{Message: `Use "Deprecated: <reason>"`}}}, 199 } 200 201 for _, test := range tests { 202 t.Run(test.testName, func(t *testing.T) { 203 file := testutils.ParseProto3Tmpl(t, ` 204 {{.MessageComment}} 205 message GetBookRequest { 206 option deprecated = true; 207 208 string name = 1; 209 } 210 `, test) 211 212 problems := deprecatedComment.Lint(file) 213 if diff := test.problems.SetDescriptor(file.GetMessageTypes()[0]).Diff(problems); diff != "" { 214 t.Error(diff) 215 } 216 }) 217 } 218 }