github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/report/reporters/jsonReporter_test.go (about)

     1  package reporters_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     8  
     9  	"github.com/yoheimuta/protolint/internal/linter/report/reporters"
    10  	"github.com/yoheimuta/protolint/linter/report"
    11  )
    12  
    13  func TestJSONReporter_Report(t *testing.T) {
    14  	tests := []struct {
    15  		name          string
    16  		inputFailures []report.Failure
    17  		wantOutput    string
    18  	}{
    19  		{
    20  			name: "Prints failures in JSON format",
    21  			inputFailures: []report.Failure{
    22  				report.Failuref(
    23  					meta.Position{
    24  						Filename: "example.proto",
    25  						Offset:   100,
    26  						Line:     5,
    27  						Column:   10,
    28  					},
    29  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    30  					`EnumField name "fIRST_VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    31  				),
    32  				report.Failuref(
    33  					meta.Position{
    34  						Filename: "example.proto",
    35  						Offset:   200,
    36  						Line:     10,
    37  						Column:   20,
    38  					},
    39  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    40  					`EnumField name "SECOND.VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    41  				),
    42  			},
    43  			wantOutput: `{
    44    "lints": [
    45      {
    46        "filename": "example.proto",
    47        "line": 5,
    48        "column": 10,
    49        "message": "EnumField name \"fIRST_VALUE\" must be CAPITALS_WITH_UNDERSCORES",
    50        "rule": "ENUM_NAMES_UPPER_CAMEL_CASE"
    51      },
    52      {
    53        "filename": "example.proto",
    54        "line": 10,
    55        "column": 20,
    56        "message": "EnumField name \"SECOND.VALUE\" must be CAPITALS_WITH_UNDERSCORES",
    57        "rule": "ENUM_NAMES_UPPER_CAMEL_CASE"
    58      }
    59    ]
    60  }
    61  `,
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		test := test
    67  		t.Run(test.name, func(t *testing.T) {
    68  			buf := &bytes.Buffer{}
    69  			err := reporters.JSONReporter{}.Report(buf, test.inputFailures)
    70  			if err != nil {
    71  				t.Errorf("got err %v, but want nil", err)
    72  				return
    73  			}
    74  			if buf.String() != test.wantOutput {
    75  				t.Errorf("got %s, but want %s", buf.String(), test.wantOutput)
    76  			}
    77  		})
    78  	}
    79  }