github.com/rawahars/moby@v24.0.4+incompatible/pkg/ioutils/writers_test.go (about)

     1  package ioutils // import "github.com/docker/docker/pkg/ioutils"
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestWriteCloserWrapperClose(t *testing.T) {
    10  	called := false
    11  	writer := bytes.NewBuffer([]byte{})
    12  	wrapper := NewWriteCloserWrapper(writer, func() error {
    13  		called = true
    14  		return nil
    15  	})
    16  	if err := wrapper.Close(); err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	if !called {
    20  		t.Fatalf("writeCloserWrapper should have call the anonymous function.")
    21  	}
    22  }
    23  
    24  func TestNopWriteCloser(t *testing.T) {
    25  	writer := bytes.NewBuffer([]byte{})
    26  	wrapper := NopWriteCloser(writer)
    27  	if err := wrapper.Close(); err != nil {
    28  		t.Fatal("NopWriteCloser always return nil on Close.")
    29  	}
    30  }
    31  
    32  func TestNopWriter(t *testing.T) {
    33  	nw := &NopWriter{}
    34  	l, err := nw.Write([]byte{'c'})
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if l != 1 {
    39  		t.Fatalf("Expected 1 got %d", l)
    40  	}
    41  }
    42  
    43  func TestWriteCounter(t *testing.T) {
    44  	dummy1 := "This is a dummy string."
    45  	dummy2 := "This is another dummy string."
    46  	totalLength := int64(len(dummy1) + len(dummy2))
    47  
    48  	reader1 := strings.NewReader(dummy1)
    49  	reader2 := strings.NewReader(dummy2)
    50  
    51  	var buffer bytes.Buffer
    52  	wc := NewWriteCounter(&buffer)
    53  
    54  	reader1.WriteTo(wc)
    55  	reader2.WriteTo(wc)
    56  
    57  	if wc.Count != totalLength {
    58  		t.Errorf("Wrong count: %d vs. %d", wc.Count, totalLength)
    59  	}
    60  
    61  	if buffer.String() != dummy1+dummy2 {
    62  		t.Error("Wrong message written")
    63  	}
    64  }