github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readall/readall_test.go (about)

     1  package readall_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/readall"
    10  )
    11  
    12  func TestReadAll(t *testing.T) {
    13  	count.Tests(t, 1)
    14  
    15  	s := "foobar"
    16  
    17  	buf := bytes.NewBuffer([]byte(s))
    18  	ctx := context.Background()
    19  
    20  	b, err := readall.ReadAll(ctx, buf)
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  
    25  	if string(b) != s {
    26  		t.Errorf("`%s` != `%s`", string(b), s)
    27  	}
    28  }
    29  
    30  func TestWithCancel(t *testing.T) {
    31  	count.Tests(t, 1)
    32  
    33  	s := "foobar"
    34  
    35  	buf := bytes.NewBuffer([]byte(s))
    36  	ctx := context.Background()
    37  	f := func() {}
    38  
    39  	b, err := readall.WithCancel(ctx, f, buf)
    40  	if err != nil {
    41  		t.Error(err)
    42  	}
    43  
    44  	if string(b) != s {
    45  		t.Errorf("`%s` != `%s`", string(b), s)
    46  	}
    47  }