github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/rpcsHaveCommentRule_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 TestRPCsHaveCommentRule_Apply(t *testing.T) {
    17  	tests := []struct {
    18  		name                         string
    19  		inputProto                   *parser.Proto
    20  		inputShouldFollowGolangStyle bool
    21  		wantFailures                 []report.Failure
    22  	}{
    23  		{
    24  			name: "no failures for proto without rpc",
    25  			inputProto: &parser.Proto{
    26  				ProtoBody: []parser.Visitee{},
    27  			},
    28  		},
    29  		{
    30  			name: "no failures for proto including valid rpcs with comments",
    31  			inputProto: &parser.Proto{
    32  				ProtoBody: []parser.Visitee{
    33  					&parser.Service{
    34  						ServiceBody: []parser.Visitee{
    35  							&parser.RPC{
    36  								RPCName: "RPCName",
    37  								Comments: []*parser.Comment{
    38  									{
    39  										Raw: "// a rpc name.",
    40  									},
    41  								},
    42  							},
    43  							&parser.RPC{
    44  								RPCName: "RPCName2",
    45  								InlineComment: &parser.Comment{
    46  									Raw: "// a rpc name.",
    47  								},
    48  							},
    49  							&parser.RPC{
    50  								RPCName: "RPCName3",
    51  								InlineCommentBehindLeftCurly: &parser.Comment{
    52  									Raw: "// a rpc name.",
    53  								},
    54  							},
    55  						},
    56  					},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "no failures for proto including valid rpcs with Golang style comments",
    62  			inputProto: &parser.Proto{
    63  				ProtoBody: []parser.Visitee{
    64  					&parser.Service{
    65  						ServiceBody: []parser.Visitee{
    66  							&parser.RPC{
    67  								RPCName: "RPCName",
    68  								Comments: []*parser.Comment{
    69  									{
    70  										Raw: "// RPCName is a rpc name.",
    71  									},
    72  								},
    73  							},
    74  						},
    75  					},
    76  				},
    77  			},
    78  			inputShouldFollowGolangStyle: true,
    79  		},
    80  		{
    81  			name: "failures for proto with invalid rpcs",
    82  			inputProto: &parser.Proto{
    83  				ProtoBody: []parser.Visitee{
    84  					&parser.Service{
    85  						ServiceBody: []parser.Visitee{
    86  							&parser.RPC{
    87  								RPCName: "RPCName",
    88  								Meta: meta.Meta{
    89  									Pos: meta.Position{
    90  										Filename: "example.proto",
    91  										Offset:   150,
    92  										Line:     7,
    93  										Column:   15,
    94  									},
    95  								},
    96  							},
    97  						},
    98  					},
    99  				},
   100  			},
   101  			wantFailures: []report.Failure{
   102  				report.Failuref(
   103  					meta.Position{
   104  						Filename: "example.proto",
   105  						Offset:   150,
   106  						Line:     7,
   107  						Column:   15,
   108  					},
   109  					"RPCS_HAVE_COMMENT",
   110  					`RPC "RPCName" should have a comment`,
   111  				),
   112  			},
   113  		},
   114  		{
   115  			name: "failures for proto with invalid rpcs without Golang style comments",
   116  			inputProto: &parser.Proto{
   117  				ProtoBody: []parser.Visitee{
   118  					&parser.Service{
   119  						ServiceBody: []parser.Visitee{
   120  							&parser.RPC{
   121  								RPCName: "RPCName",
   122  								Comments: []*parser.Comment{
   123  									{
   124  										Raw: "// a rpc name.",
   125  									},
   126  								},
   127  								Meta: meta.Meta{
   128  									Pos: meta.Position{
   129  										Filename: "example.proto",
   130  										Offset:   150,
   131  										Line:     7,
   132  										Column:   15,
   133  									},
   134  								},
   135  							},
   136  						},
   137  					},
   138  				},
   139  			},
   140  			inputShouldFollowGolangStyle: true,
   141  			wantFailures: []report.Failure{
   142  				report.Failuref(
   143  					meta.Position{
   144  						Filename: "example.proto",
   145  						Offset:   150,
   146  						Line:     7,
   147  						Column:   15,
   148  					},
   149  					"RPCS_HAVE_COMMENT",
   150  					`RPC "RPCName" should have a comment of the form "// RPCName ..."`,
   151  				),
   152  			},
   153  		},
   154  		{
   155  			name: "failures for proto with invalid rpcs without Golang style comments due to inline",
   156  			inputProto: &parser.Proto{
   157  				ProtoBody: []parser.Visitee{
   158  					&parser.Service{
   159  						ServiceBody: []parser.Visitee{
   160  							&parser.RPC{
   161  								RPCName: "RPCName",
   162  								InlineComment: &parser.Comment{
   163  									Raw: "// RPCName is special.",
   164  								},
   165  							},
   166  						},
   167  					},
   168  				},
   169  			},
   170  			inputShouldFollowGolangStyle: true,
   171  			wantFailures: []report.Failure{
   172  				report.Failuref(
   173  					meta.Position{},
   174  					"RPCS_HAVE_COMMENT",
   175  					`RPC "RPCName" should have a comment of the form "// RPCName ..."`,
   176  				),
   177  			},
   178  		},
   179  	}
   180  
   181  	for _, test := range tests {
   182  		test := test
   183  		t.Run(test.name, func(t *testing.T) {
   184  			rule := rules.NewRPCsHaveCommentRule(rule.SeverityError, test.inputShouldFollowGolangStyle)
   185  
   186  			got, err := rule.Apply(test.inputProto)
   187  			if err != nil {
   188  				t.Errorf("got err %v, but want nil", err)
   189  				return
   190  			}
   191  			if !reflect.DeepEqual(got, test.wantFailures) {
   192  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   193  			}
   194  		})
   195  	}
   196  }