github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/ioutils/writers_test.go (about) 1 package 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 33 func TestNopWriter(t *testing.T) { 34 nw := &NopWriter{} 35 l, err := nw.Write([]byte{'c'}) 36 if err != nil { 37 t.Fatal(err) 38 } 39 if l != 1 { 40 t.Fatalf("Expected 1 got %d", l) 41 } 42 } 43 44 func TestWriteCounter(t *testing.T) { 45 dummy1 := "This is a dummy string." 46 dummy2 := "This is another dummy string." 47 totalLength := int64(len(dummy1) + len(dummy2)) 48 49 reader1 := strings.NewReader(dummy1) 50 reader2 := strings.NewReader(dummy2) 51 52 var buffer bytes.Buffer 53 wc := NewWriteCounter(&buffer) 54 55 reader1.WriteTo(wc) 56 reader2.WriteTo(wc) 57 58 if wc.Count != totalLength { 59 t.Errorf("Wrong count: %d vs. %d", wc.Count, totalLength) 60 } 61 62 if buffer.String() != dummy1+dummy2 { 63 t.Error("Wrong message written") 64 } 65 }