github.com/googleapis/api-linter@v1.65.2/rules/internal/testutils/problems_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 testutils
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/googleapis/api-linter/lint"
    21  	"github.com/jhump/protoreflect/desc/builder"
    22  )
    23  
    24  func TestDiffEquivalent(t *testing.T) {
    25  	// Build a message for the descriptor test.
    26  	m, err := builder.NewMessage("Foo").Build()
    27  	if err != nil {
    28  		t.Fatalf("Could not build descriptor.")
    29  	}
    30  
    31  	// Declare a series of tests that should all be equal.
    32  	tests := []struct {
    33  		name string
    34  		x    Problems
    35  		y    []Problem
    36  	}{
    37  		{"NilNil", nil, nil},
    38  		{"ProblemNil", Problems{}, nil},
    39  		{"Descriptor", Problems{{Descriptor: m}}, []Problem{{Descriptor: m}}},
    40  		{"Suggestion", Problems{{Suggestion: "foo"}}, []Problem{{Suggestion: "foo"}}},
    41  		{"MessageExact", Problems{{Message: "foo"}}, []Problem{{Message: "foo"}}},
    42  		{"MessageSubstr", Problems{{Message: "foo"}}, []Problem{{Message: "foo bar"}}},
    43  	}
    44  
    45  	for _, test := range tests {
    46  		t.Run(test.name, func(t *testing.T) {
    47  			if diff := test.x.Diff(test.y); diff != "" {
    48  				t.Errorf("Problems were unequal (x, y):\n%v", diff)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestDiffNotEquivalent(t *testing.T) {
    55  	// Build a message for the descriptor test.
    56  	m1, err1 := builder.NewMessage("Foo").Build()
    57  	m2, err2 := builder.NewMessage("Bar").Build()
    58  	if err1 != nil || err2 != nil {
    59  		t.Fatalf("Could not build descriptor.")
    60  	}
    61  
    62  	// Declare a series of tests that should all be equal.
    63  	tests := []struct {
    64  		name string
    65  		x    Problems
    66  		y    []Problem
    67  	}{
    68  		{"ProblemNil", Problems{{Descriptor: m1}}, nil},
    69  		{"EmptyProblemNil", Problems{{}}, nil},
    70  		{"LengthMismatch", Problems{{}}, []Problem{{}, {}}},
    71  		{"Descriptor", Problems{{Descriptor: m1}}, []Problem{{Descriptor: m2}}},
    72  		{"Suggestion", Problems{{Suggestion: "foo"}}, []Problem{{Suggestion: "bar"}}},
    73  		{"Message", Problems{{Message: "foo"}}, []Problem{{Message: "bar"}}},
    74  		{"MessageSuperstr", Problems{{Message: "foo bar"}}, []Problem{{Message: "foo"}}},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		t.Run(test.name, func(t *testing.T) {
    79  			if diff := test.x.Diff(test.y); diff == "" {
    80  				t.Errorf("Got no diff (x, y); expected one.")
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func TestSetDescriptor(t *testing.T) {
    87  	m, err := builder.NewMessage("Foo").Build()
    88  	if err != nil {
    89  		t.Fatalf("Could not build descriptor.")
    90  	}
    91  	problems := Problems{{}, {}, {}}.SetDescriptor(m)
    92  	for _, p := range problems {
    93  		if p.Descriptor != m {
    94  			t.Errorf("Got %v, expected %v", p.Descriptor, m)
    95  		}
    96  	}
    97  }