github.com/haya14busa/reviewdog@v0.0.0-20180723114510-ffb00ef78fd3/comment_test.go (about)

     1  package reviewdog
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestMultiCommentService_Post(t *testing.T) {
    11  	buf1 := new(bytes.Buffer)
    12  	buf2 := new(bytes.Buffer)
    13  	w := MultiCommentService(NewRawCommentWriter(buf1), NewRawCommentWriter(buf2))
    14  
    15  	const want = "line1\nline2"
    16  
    17  	c := &Comment{CheckResult: &CheckResult{Lines: strings.Split(want, "\n")}}
    18  	if err := w.Post(context.Background(), c); err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	if got := strings.Trim(buf1.String(), "\n"); got != want {
    23  		t.Errorf("writer 1: got %v, want %v", got, want)
    24  	}
    25  
    26  	if got := strings.Trim(buf2.String(), "\n"); got != want {
    27  		t.Errorf("writer 2: got %v, want %v", got, want)
    28  	}
    29  
    30  	if err := w.(BulkCommentService).Flush(context.Background()); err != nil {
    31  		t.Errorf("MultiCommentService implements BulkCommentService and should not return error when any services implements it: %v", err)
    32  	}
    33  }
    34  
    35  type fakeBulkCommentService struct {
    36  	BulkCommentService
    37  	calledFlush bool
    38  }
    39  
    40  func (f *fakeBulkCommentService) Flush(_ context.Context) error {
    41  	f.calledFlush = true
    42  	return nil
    43  }
    44  
    45  func TestMultiCommentService_Flush(t *testing.T) {
    46  	f1 := &fakeBulkCommentService{}
    47  	f2 := &fakeBulkCommentService{}
    48  	w := MultiCommentService(f1, f2)
    49  	if err := w.(BulkCommentService).Flush(context.Background()); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if !f1.calledFlush || !f2.calledFlush {
    53  		t.Error("MultiCommentService_Flush should run Flush() for every services")
    54  	}
    55  }