github.com/alexey-mercari/reviewdog@v0.10.1-0.20200514053941-928943b10766/parser_test.go (about)

     1  package reviewdog
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestNewParser(t *testing.T) {
    10  	tests := []struct {
    11  		in      *ParserOpt
    12  		typ     Parser
    13  		wantErr bool
    14  	}{
    15  		{
    16  			in: &ParserOpt{
    17  				FormatName: "checkstyle",
    18  			},
    19  			typ: &CheckStyleParser{},
    20  		},
    21  		{
    22  			in: &ParserOpt{
    23  				FormatName: "golint",
    24  			},
    25  			typ: &ErrorformatParser{},
    26  		},
    27  		{
    28  			in: &ParserOpt{
    29  				Errorformat: []string{`%f:%l:%c:%m`},
    30  			},
    31  			typ: &ErrorformatParser{},
    32  		},
    33  		{ // empty
    34  			in:      &ParserOpt{},
    35  			wantErr: true,
    36  		},
    37  		{ // both
    38  			in: &ParserOpt{
    39  				FormatName:  "checkstyle",
    40  				Errorformat: []string{`%f:%l:%c:%m`},
    41  			},
    42  			wantErr: true,
    43  		},
    44  		{ // unsupported
    45  			in: &ParserOpt{
    46  				FormatName: "unsupported format",
    47  			},
    48  			wantErr: true,
    49  		},
    50  	}
    51  	for _, tt := range tests {
    52  		p, err := NewParser(tt.in)
    53  		if tt.wantErr && err != nil {
    54  			continue
    55  		}
    56  		if err != nil {
    57  			t.Error(err)
    58  			continue
    59  		}
    60  		if got, want := reflect.TypeOf(p), reflect.TypeOf(tt.typ); got != want {
    61  			t.Errorf("typ: got %v, want %v", got, want)
    62  		}
    63  	}
    64  }
    65  
    66  func TestNewErrorformatParserString(t *testing.T) {
    67  	in := []string{`%f:%l:%c:%m`, `%-G%.%#`}
    68  
    69  	got, err := NewErrorformatParserString(in)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	if len(got.efm.Efms) != len(in) {
    75  		t.Errorf("NewErrorformatParserString: len: got %v, want %v", len(got.efm.Efms), len(in))
    76  	}
    77  }
    78  
    79  func TestCheckStyleParser(t *testing.T) {
    80  	const sample = `<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="/path/to/file"><error line="1" column="10" severity="error" message="&apos;addOne&apos; is defined but never used. (no-unused-vars)" source="eslint.rules.no-unused-vars" /><error line="2" column="9" severity="error" message="Use the isNaN function to compare with NaN. (use-isnan)" source="eslint.rules.use-isnan" /><error line="3" column="16" severity="error" message="Unexpected space before unary operator &apos;++&apos;. (space-unary-ops)" source="eslint.rules.space-unary-ops" /><error line="3" column="20" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="4" column="12" severity="warning" message="Unnecessary &apos;else&apos; after &apos;return&apos;. (no-else-return)" source="eslint.rules.no-else-return" /><error line="5" column="7" severity="warning" message="Expected indentation of 8 spaces but found 6. (indent)" source="eslint.rules.indent" /><error line="5" column="7" severity="error" message="Expected a return value. (consistent-return)" source="eslint.rules.consistent-return" /><error line="5" column="13" severity="warning" message="Missing semicolon. (semi)" source="eslint.rules.semi" /><error line="7" column="2" severity="error" message="Unnecessary semicolon. (no-extra-semi)" source="eslint.rules.no-extra-semi" /></file></checkstyle>`
    81  
    82  	wants := []string{
    83  
    84  		"/path/to/file:1:10: error: 'addOne' is defined but never used. (no-unused-vars) (eslint.rules.no-unused-vars)",
    85  		"/path/to/file:2:9: error: Use the isNaN function to compare with NaN. (use-isnan) (eslint.rules.use-isnan)",
    86  		"/path/to/file:3:16: error: Unexpected space before unary operator '++'. (space-unary-ops) (eslint.rules.space-unary-ops)",
    87  		"/path/to/file:3:20: warning: Missing semicolon. (semi) (eslint.rules.semi)",
    88  		"/path/to/file:4:12: warning: Unnecessary 'else' after 'return'. (no-else-return) (eslint.rules.no-else-return)",
    89  		"/path/to/file:5:7: warning: Expected indentation of 8 spaces but found 6. (indent) (eslint.rules.indent)",
    90  		"/path/to/file:5:7: error: Expected a return value. (consistent-return) (eslint.rules.consistent-return)",
    91  		"/path/to/file:5:13: warning: Missing semicolon. (semi) (eslint.rules.semi)",
    92  		"/path/to/file:7:2: error: Unnecessary semicolon. (no-extra-semi) (eslint.rules.no-extra-semi)",
    93  	}
    94  
    95  	p := NewCheckStyleParser()
    96  	crs, err := p.Parse(strings.NewReader(sample))
    97  	if err != nil {
    98  		t.Error(err)
    99  	}
   100  	for i, cr := range crs {
   101  		if got, want := cr.Lines[0], wants[i]; got != want {
   102  			t.Errorf("%d: got %v, want %v", i, got, want)
   103  		}
   104  	}
   105  }