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

     1  package rules_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser"
     8  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     9  
    10  	"github.com/yoheimuta/protolint/internal/addon/rules"
    11  	"github.com/yoheimuta/protolint/linter/report"
    12  	"github.com/yoheimuta/protolint/linter/rule"
    13  )
    14  
    15  func TestPackageNameLowerCaseRule_Apply(t *testing.T) {
    16  	tests := []struct {
    17  		name         string
    18  		inputProto   *parser.Proto
    19  		wantFailures []report.Failure
    20  	}{
    21  		{
    22  			name: "no failures for proto without service",
    23  			inputProto: &parser.Proto{
    24  				ProtoBody: []parser.Visitee{
    25  					&parser.Message{},
    26  				},
    27  			},
    28  		},
    29  		{
    30  			name: "no failures for proto with the valid package name",
    31  			inputProto: &parser.Proto{
    32  				ProtoBody: []parser.Visitee{
    33  					&parser.Package{
    34  						Name: "package",
    35  					},
    36  				},
    37  			},
    38  		},
    39  		{
    40  			name: "no failures for proto with the valid package name with periods",
    41  			inputProto: &parser.Proto{
    42  				ProtoBody: []parser.Visitee{
    43  					&parser.Package{
    44  						Name: "my.v1.package",
    45  					},
    46  				},
    47  			},
    48  		},
    49  		{
    50  			name: "failures for proto with the invalid package name",
    51  			inputProto: &parser.Proto{
    52  				ProtoBody: []parser.Visitee{
    53  					&parser.Package{
    54  						Name: "myV1Package",
    55  						Meta: meta.Meta{
    56  							Pos: meta.Position{
    57  								Filename: "example.proto",
    58  								Offset:   100,
    59  								Line:     5,
    60  								Column:   10,
    61  							},
    62  						},
    63  					},
    64  				},
    65  			},
    66  			wantFailures: []report.Failure{
    67  				report.Failuref(
    68  					meta.Position{
    69  						Filename: "example.proto",
    70  						Offset:   100,
    71  						Line:     5,
    72  						Column:   10,
    73  					},
    74  					"PACKAGE_NAME_LOWER_CASE",
    75  					`Package name "myV1Package" must not contain any uppercase letter. Consider to change like "myv1package".`,
    76  				),
    77  			},
    78  		},
    79  		{
    80  			name: "no failures for proto with the package name including _",
    81  			inputProto: &parser.Proto{
    82  				ProtoBody: []parser.Visitee{
    83  					&parser.Package{
    84  						Name: "my.some_service",
    85  					},
    86  				},
    87  			},
    88  		},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		test := test
    93  		t.Run(test.name, func(t *testing.T) {
    94  			rule := rules.NewPackageNameLowerCaseRule(rule.SeverityError, false)
    95  
    96  			got, err := rule.Apply(test.inputProto)
    97  			if err != nil {
    98  				t.Errorf("got err %v, but want nil", err)
    99  				return
   100  			}
   101  			if !reflect.DeepEqual(got, test.wantFailures) {
   102  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestPackageNameLowerCaseRule_Apply_fix(t *testing.T) {
   109  	tests := []struct {
   110  		name          string
   111  		inputFilename string
   112  		wantFilename  string
   113  	}{
   114  		{
   115  			name:          "no fix for a correct proto",
   116  			inputFilename: "lowerCase.proto",
   117  			wantFilename:  "lowerCase.proto",
   118  		},
   119  		{
   120  			name:          "fix for an incorrect proto",
   121  			inputFilename: "invalid.proto",
   122  			wantFilename:  "lowerCase.proto",
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		test := test
   128  		t.Run(test.name, func(t *testing.T) {
   129  			r := rules.NewPackageNameLowerCaseRule(rule.SeverityError, true)
   130  			testApplyFix(t, r, test.inputFilename, test.wantFilename)
   131  		})
   132  	}
   133  }