github.com/googleapis/api-linter@v1.65.2/rules/aip0158/response_next_page_token_field_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 aip0158
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  	"github.com/jhump/protoreflect/desc/builder"
    23  )
    24  
    25  type field struct {
    26  	fieldName string
    27  	fieldType *builder.FieldType
    28  }
    29  
    30  func TestResponsePaginationNextPageToken(t *testing.T) {
    31  	// Set up the testing permutations.
    32  	tests := []struct {
    33  		testName      string
    34  		messageName   string
    35  		messageFields []field
    36  		problems      testutils.Problems
    37  		problemDesc   func(m *desc.MessageDescriptor) desc.Descriptor
    38  	}{
    39  		{
    40  			"Valid",
    41  			"ListBooksResponse",
    42  			[]field{{"books", builder.FieldTypeString()}, {"next_page_token", builder.FieldTypeString()}},
    43  			testutils.Problems{},
    44  			nil,
    45  		},
    46  		{
    47  			"MissingField",
    48  			"ListBooksResponse",
    49  			[]field{{"books", builder.FieldTypeString()}},
    50  			testutils.Problems{{Message: "next_page_token"}},
    51  			nil,
    52  		},
    53  		{
    54  			"InvalidType",
    55  			"ListFooResponse",
    56  			[]field{{"name", builder.FieldTypeString()}, {"next_page_token", builder.FieldTypeDouble()}},
    57  			testutils.Problems{{Suggestion: "string"}},
    58  			func(m *desc.MessageDescriptor) desc.Descriptor {
    59  				return m.FindFieldByName("next_page_token")
    60  			},
    61  		},
    62  	}
    63  
    64  	// Run each test individually.
    65  	for _, test := range tests {
    66  		t.Run(test.testName, func(t *testing.T) {
    67  			// Create an appropriate message descriptor.
    68  			messageBuilder := builder.NewMessage(test.messageName)
    69  
    70  			for _, f := range test.messageFields {
    71  				messageBuilder.AddField(
    72  					builder.NewField(f.fieldName, f.fieldType),
    73  				)
    74  			}
    75  
    76  			message, err := messageBuilder.Build()
    77  			if err != nil {
    78  				t.Fatalf("Could not build %s message.", test.messageName)
    79  			}
    80  
    81  			// Determine the descriptor that a failing test will attach to.
    82  			var problemDesc desc.Descriptor = message
    83  			if test.problemDesc != nil {
    84  				problemDesc = test.problemDesc(message)
    85  			}
    86  
    87  			// Run the lint rule, and establish that it returns the correct problems.
    88  			problems := responsePaginationNextPageToken.Lint(message.GetFile())
    89  			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
    90  				t.Errorf(diff)
    91  			}
    92  		})
    93  	}
    94  }