github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/report/reporters/jUnitReporter_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 TestJUnitReporter_Report(t *testing.T) {
    14  	tests := []struct {
    15  		name          string
    16  		inputFailures []report.Failure
    17  		wantOutput    string
    18  	}{
    19  		{
    20  			name: "Prints no failures in the JUnit XML format",
    21  			wantOutput: `<?xml version="1.0" encoding="UTF-8"?>
    22    <testsuites>
    23        <testsuite tests="1" failures="0" time="0">
    24            <package>net.protolint</package>
    25            <testcase classname="net.protolint.ALL_RULES" name="All Rules" time="0"></testcase>
    26        </testsuite>
    27    </testsuites>
    28  `,
    29  		},
    30  		{
    31  			name: "Prints failures in the JUnit XML format",
    32  			inputFailures: []report.Failure{
    33  				report.Failuref(
    34  					meta.Position{
    35  						Filename: "example.proto",
    36  						Offset:   100,
    37  						Line:     5,
    38  						Column:   10,
    39  					},
    40  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    41  					`EnumField name "fIRST_VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    42  				),
    43  				report.Failuref(
    44  					meta.Position{
    45  						Filename: "example.proto",
    46  						Offset:   200,
    47  						Line:     10,
    48  						Column:   20,
    49  					},
    50  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    51  					`EnumField name "SECOND.VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    52  				),
    53  			},
    54  			wantOutput: `<?xml version="1.0" encoding="UTF-8"?>
    55    <testsuites>
    56        <testsuite tests="2" failures="2" time="0">
    57            <package>net.protolint</package>
    58            <testcase classname="example" name="net.protolint.ENUM_NAMES_UPPER_CAMEL_CASE" time="0">
    59                <failure message="EnumField name &#34;fIRST_VALUE&#34; must be CAPITALS_WITH_UNDERSCORES" type="error"><![CDATA[line 5, col 10]]></failure>
    60            </testcase>
    61            <testcase classname="example" name="net.protolint.ENUM_NAMES_UPPER_CAMEL_CASE" time="0">
    62                <failure message="EnumField name &#34;SECOND.VALUE&#34; must be CAPITALS_WITH_UNDERSCORES" type="error"><![CDATA[line 10, col 20]]></failure>
    63            </testcase>
    64        </testsuite>
    65    </testsuites>
    66  `,
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		test := test
    72  		t.Run(test.name, func(t *testing.T) {
    73  			buf := &bytes.Buffer{}
    74  			err := reporters.JUnitReporter{}.Report(buf, test.inputFailures)
    75  			if err != nil {
    76  				t.Errorf("got err %v, but want nil", err)
    77  				return
    78  			}
    79  			if buf.String() != test.wantOutput {
    80  				t.Errorf("got %s, but want %s", buf.String(), test.wantOutput)
    81  			}
    82  		})
    83  	}
    84  }