github.com/googleapis/api-linter@v1.65.2/rules/aip0202/string_only_format_test.go (about)

     1  // Copyright 2023 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 aip0202
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestStringOnlyFormat(t *testing.T) {
    24  	for _, tc := range []struct {
    25  		name, Type, Format string
    26  		problems           testutils.Problems
    27  	}{
    28  		{
    29  			name:     "ValidSkipString",
    30  			Type:     "string",
    31  			Format:   "format: UUID4",
    32  			problems: nil,
    33  		},
    34  		{
    35  			name:     "ValidSkipNoFormat",
    36  			Type:     "int32",
    37  			problems: nil,
    38  		},
    39  		{
    40  			name:     "InvalidNonString",
    41  			Type:     "int32",
    42  			Format:   "format: UUID4",
    43  			problems: testutils.Problems{{Message: "string fields"}},
    44  		},
    45  	} {
    46  		t.Run(tc.name, func(t *testing.T) {
    47  			file := testutils.ParseProto3Tmpl(t, `
    48  			import "google/api/field_info.proto";
    49  			
    50  			message CreateBookRequest {
    51  				{{.Type}} foo = 1 [(google.api.field_info) = {
    52  					{{.Format}}
    53  				}];
    54  			}
    55  			`, tc)
    56  			fd := file.FindMessage("CreateBookRequest").FindFieldByName("foo")
    57  			problems := stringOnlyFormat.Lint(file)
    58  			if diff := tc.problems.SetDescriptor(fd).Diff(problems); diff != "" {
    59  				t.Error(diff)
    60  			}
    61  		})
    62  	}
    63  }