github.com/OnkarRuikar/reviewdog@v0.0.0-20230802094019-bc1001e3b2e5/comment_iowriter_test.go (about)

     1  package reviewdog
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/reviewdog/reviewdog/filter"
    10  	"github.com/reviewdog/reviewdog/proto/rdf"
    11  )
    12  
    13  func TestUnifiedCommentWriter_Post(t *testing.T) {
    14  	tests := []struct {
    15  		in   *Comment
    16  		want string
    17  	}{
    18  		{
    19  			in: &Comment{
    20  				Result: &filter.FilteredDiagnostic{
    21  					Diagnostic: &rdf.Diagnostic{
    22  						Location: &rdf.Location{Path: "/path/to/file"},
    23  						Message:  "message",
    24  					},
    25  				},
    26  				ToolName: "tool name",
    27  			},
    28  			want: `/path/to/file: [tool name] message`,
    29  		},
    30  		{
    31  			in: &Comment{
    32  				Result: &filter.FilteredDiagnostic{
    33  					Diagnostic: &rdf.Diagnostic{
    34  						Location: &rdf.Location{
    35  							Path: "/path/to/file",
    36  							Range: &rdf.Range{Start: &rdf.Position{
    37  								Column: 14,
    38  							}},
    39  						},
    40  						Message: "message",
    41  					},
    42  				},
    43  				ToolName: "tool name",
    44  			},
    45  			want: `/path/to/file: [tool name] message`,
    46  		},
    47  		{
    48  			in: &Comment{
    49  				Result: &filter.FilteredDiagnostic{
    50  					Diagnostic: &rdf.Diagnostic{
    51  						Location: &rdf.Location{
    52  							Path: "/path/to/file",
    53  							Range: &rdf.Range{Start: &rdf.Position{
    54  								Line: 14,
    55  							}},
    56  						},
    57  						Message: "message",
    58  					},
    59  				},
    60  				ToolName: "tool name",
    61  			},
    62  			want: `/path/to/file:14: [tool name] message`,
    63  		},
    64  		{
    65  			in: &Comment{
    66  				Result: &filter.FilteredDiagnostic{
    67  					Diagnostic: &rdf.Diagnostic{
    68  						Location: &rdf.Location{
    69  							Path: "/path/to/file",
    70  							Range: &rdf.Range{Start: &rdf.Position{
    71  								Line:   14,
    72  								Column: 7,
    73  							}},
    74  						},
    75  						Message: "line1\nline2",
    76  					},
    77  				},
    78  				ToolName: "tool name",
    79  			},
    80  			want: `/path/to/file:14:7: [tool name] line1
    81  line2`,
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		buf := new(bytes.Buffer)
    86  		mc := NewUnifiedCommentWriter(buf)
    87  		if err := mc.Post(context.Background(), tt.in); err != nil {
    88  			t.Error(err)
    89  			continue
    90  		}
    91  		if got := strings.Trim(buf.String(), "\n"); got != tt.want {
    92  			t.Errorf("UnifiedCommentWriter_Post(%v) = \n%v\nwant:\n%v", tt.in, got, tt.want)
    93  		}
    94  	}
    95  }