github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/report/reporters/sonarReporter_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 TestSonarReporter_Report(t *testing.T) {
    14  	tests := []struct {
    15  		name          string
    16  		inputFailures []report.Failure
    17  		wantOutput    string
    18  	}{
    19  		{
    20  			name: "Prints failures in Sonar 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    {
    45      "engineId": "protolint",
    46      "ruleId": "ENUM_NAMES_UPPER_CAMEL_CASE",
    47      "primaryLocation": {
    48        "message": "EnumField name \"fIRST_VALUE\" must be CAPITALS_WITH_UNDERSCORES",
    49        "filePath": "example.proto",
    50        "textRange": {
    51          "startLine": 5,
    52          "startColumn": 10
    53        }
    54      },
    55      "severity": "MAJOR",
    56      "issueType": "CODE_SMELL"
    57    },
    58    {
    59      "engineId": "protolint",
    60      "ruleId": "ENUM_NAMES_UPPER_CAMEL_CASE",
    61      "primaryLocation": {
    62        "message": "EnumField name \"SECOND.VALUE\" must be CAPITALS_WITH_UNDERSCORES",
    63        "filePath": "example.proto",
    64        "textRange": {
    65          "startLine": 10,
    66          "startColumn": 20
    67        }
    68      },
    69      "severity": "MAJOR",
    70      "issueType": "CODE_SMELL"
    71    }
    72  ]`,
    73  		},
    74  	}
    75  
    76  	for _, test := range tests {
    77  		test := test
    78  		t.Run(test.name, func(t *testing.T) {
    79  			buf := &bytes.Buffer{}
    80  			err := reporters.SonarReporter{}.Report(buf, test.inputFailures)
    81  			if err != nil {
    82  				t.Errorf("got err %v, but want nil", err)
    83  				return
    84  			}
    85  			if buf.String() != test.wantOutput {
    86  				t.Errorf("got %s, but want %s", buf.String(), test.wantOutput)
    87  			}
    88  		})
    89  	}
    90  }