github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/messageNamesExcludePrepositionsRule_test.go (about)

     1  package rules_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     8  
     9  	"github.com/yoheimuta/go-protoparser/v4/parser"
    10  
    11  	"github.com/yoheimuta/protolint/internal/addon/rules"
    12  	"github.com/yoheimuta/protolint/linter/report"
    13  	"github.com/yoheimuta/protolint/linter/rule"
    14  )
    15  
    16  func TestMessageNamesExcludePrepositionsRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name              string
    19  		inputProto        *parser.Proto
    20  		inputPrepositions []string
    21  		inputExcludes     []string
    22  		wantFailures      []report.Failure
    23  	}{
    24  		{
    25  			name: "no failures for proto without messages",
    26  			inputProto: &parser.Proto{
    27  				ProtoBody: []parser.Visitee{
    28  					&parser.Enum{},
    29  				},
    30  			},
    31  		},
    32  		{
    33  			name: "no failures for proto with valid message names",
    34  			inputProto: &parser.Proto{
    35  				ProtoBody: []parser.Visitee{
    36  					&parser.Service{},
    37  					&parser.Message{
    38  						MessageName: "AccountStatus",
    39  					},
    40  				},
    41  			},
    42  		},
    43  		{
    44  			name: "failures for proto with invalid message names",
    45  			inputProto: &parser.Proto{
    46  				ProtoBody: []parser.Visitee{
    47  					&parser.Message{
    48  						MessageName: "AccountStatus",
    49  						MessageBody: []parser.Visitee{
    50  							&parser.Message{
    51  								MessageName: "StatusOfAccount",
    52  								Meta: meta.Meta{
    53  									Pos: meta.Position{
    54  										Filename: "example.proto",
    55  										Offset:   100,
    56  										Line:     5,
    57  										Column:   10,
    58  									},
    59  								},
    60  							},
    61  							&parser.Message{
    62  								MessageName: "WithAccountForActive",
    63  								Meta: meta.Meta{
    64  									Pos: meta.Position{
    65  										Filename: "example.proto",
    66  										Offset:   200,
    67  										Line:     10,
    68  										Column:   20,
    69  									},
    70  								},
    71  							},
    72  						},
    73  					},
    74  				},
    75  			},
    76  			wantFailures: []report.Failure{
    77  				report.Failuref(
    78  					meta.Position{
    79  						Filename: "example.proto",
    80  						Offset:   100,
    81  						Line:     5,
    82  						Column:   10,
    83  					},
    84  					"MESSAGE_NAMES_EXCLUDE_PREPOSITIONS",
    85  					`Message name "StatusOfAccount" should not include a preposition "Of"`,
    86  				),
    87  				report.Failuref(
    88  					meta.Position{
    89  						Filename: "example.proto",
    90  						Offset:   200,
    91  						Line:     10,
    92  						Column:   20,
    93  					},
    94  					"MESSAGE_NAMES_EXCLUDE_PREPOSITIONS",
    95  					`Message name "WithAccountForActive" should not include a preposition "With"`,
    96  				),
    97  				report.Failuref(
    98  					meta.Position{
    99  						Filename: "example.proto",
   100  						Offset:   200,
   101  						Line:     10,
   102  						Column:   20,
   103  					},
   104  					"MESSAGE_NAMES_EXCLUDE_PREPOSITIONS",
   105  					`Message name "WithAccountForActive" should not include a preposition "For"`,
   106  				),
   107  			},
   108  		},
   109  		{
   110  			name: "failures for proto with invalid message names, but message name including the excluded keyword is no problem",
   111  			inputProto: &parser.Proto{
   112  				ProtoBody: []parser.Visitee{
   113  					&parser.Message{
   114  						MessageName: "AccountStatus",
   115  						MessageBody: []parser.Visitee{
   116  							&parser.Message{
   117  								MessageName: "SpecialEndOfSupport",
   118  								Meta: meta.Meta{
   119  									Pos: meta.Position{
   120  										Filename: "example.proto",
   121  										Offset:   100,
   122  										Line:     5,
   123  										Column:   10,
   124  									},
   125  								},
   126  							},
   127  							&parser.Message{
   128  								MessageName: "EndOfSales",
   129  								Meta: meta.Meta{
   130  									Pos: meta.Position{
   131  										Filename: "example.proto",
   132  										Offset:   200,
   133  										Line:     10,
   134  										Column:   20,
   135  									},
   136  								},
   137  							},
   138  						},
   139  					},
   140  				},
   141  			},
   142  			inputExcludes: []string{
   143  				"EndOfSupport",
   144  			},
   145  			wantFailures: []report.Failure{
   146  				report.Failuref(
   147  					meta.Position{
   148  						Filename: "example.proto",
   149  						Offset:   200,
   150  						Line:     10,
   151  						Column:   20,
   152  					},
   153  					"MESSAGE_NAMES_EXCLUDE_PREPOSITIONS",
   154  					`Message name "EndOfSales" should not include a preposition "Of"`,
   155  				),
   156  			},
   157  		},
   158  	}
   159  
   160  	for _, test := range tests {
   161  		test := test
   162  		t.Run(test.name, func(t *testing.T) {
   163  			rule := rules.NewMessageNamesExcludePrepositionsRule(rule.SeverityError, test.inputPrepositions, test.inputExcludes)
   164  
   165  			got, err := rule.Apply(test.inputProto)
   166  			if err != nil {
   167  				t.Errorf("got err %v, but want nil", err)
   168  				return
   169  			}
   170  			if !reflect.DeepEqual(got, test.wantFailures) {
   171  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   172  			}
   173  		})
   174  	}
   175  }