github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/maxLineLengthRule_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/protolint/internal/setting_test"
    10  
    11  	"github.com/yoheimuta/go-protoparser/v4/parser"
    12  
    13  	"github.com/yoheimuta/protolint/internal/addon/rules"
    14  	"github.com/yoheimuta/protolint/linter/report"
    15  	"github.com/yoheimuta/protolint/linter/rule"
    16  )
    17  
    18  func TestMaxLineLengthRule_Apply(t *testing.T) {
    19  	tests := []struct {
    20  		name          string
    21  		inputMaxChars int
    22  		inputTabChars int
    23  		inputProto    *parser.Proto
    24  		wantFailures  []report.Failure
    25  		wantExistErr  bool
    26  	}{
    27  		{
    28  			name: "not found proto file",
    29  			inputProto: &parser.Proto{
    30  				Meta: &parser.ProtoMeta{
    31  					Filename: "",
    32  				},
    33  			},
    34  			wantExistErr: true,
    35  		},
    36  		{
    37  			name:          "not found long lines",
    38  			inputMaxChars: 120,
    39  			inputTabChars: 4,
    40  			inputProto: &parser.Proto{
    41  				Meta: &parser.ProtoMeta{
    42  					Filename: setting_test.TestDataPath("rules", "max_line_length_rule.proto"),
    43  				},
    44  			},
    45  		},
    46  		{
    47  			name:          "found long lines",
    48  			inputTabChars: 4,
    49  			inputProto: &parser.Proto{
    50  				Meta: &parser.ProtoMeta{
    51  					Filename: setting_test.TestDataPath("rules", "max_line_length_rule.proto"),
    52  				},
    53  			},
    54  			wantFailures: []report.Failure{
    55  				report.Failuref(
    56  					meta.Position{
    57  						Filename: setting_test.TestDataPath("rules", "max_line_length_rule.proto"),
    58  						Line:     3,
    59  						Column:   1,
    60  					},
    61  					"MAX_LINE_LENGTH",
    62  					`The line length is 91, but it must be shorter than 80`,
    63  				),
    64  				report.Failuref(
    65  					meta.Position{
    66  						Filename: setting_test.TestDataPath("rules", "max_line_length_rule.proto"),
    67  						Line:     15,
    68  						Column:   1,
    69  					},
    70  					"MAX_LINE_LENGTH",
    71  					`The line length is 88, but it must be shorter than 80`,
    72  				),
    73  			},
    74  		},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		test := test
    79  		t.Run(test.name, func(t *testing.T) {
    80  			rule := rules.NewMaxLineLengthRule(
    81  				rule.SeverityError,
    82  				test.inputMaxChars,
    83  				test.inputTabChars,
    84  			)
    85  
    86  			got, err := rule.Apply(test.inputProto)
    87  			if test.wantExistErr {
    88  				if err == nil {
    89  					t.Errorf("got err nil, but want err")
    90  				}
    91  				return
    92  			}
    93  			if err != nil {
    94  				t.Errorf("got err %v, but want nil", err)
    95  				return
    96  			}
    97  
    98  			if !reflect.DeepEqual(got, test.wantFailures) {
    99  				t.Errorf("got %v, but want %v", got, test.wantFailures)
   100  			}
   101  		})
   102  	}
   103  }