Added tests for StdWriter.Write.
The tests added cover the case when the Writer field returns and error and, related to that, when the number of written bytes is less than 0. Signed-off-by: Federico Gimenez <fgimenez@coit.es>
This commit is contained in:
parent
a6d3018c00
commit
2ef06c2b56
1 changed files with 36 additions and 0 deletions
|
@ -2,6 +2,7 @@ package stdcopy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -49,6 +50,41 @@ func TestWrite(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errWriter struct {
|
||||||
|
n int
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *errWriter) Write(buf []byte) (int, error) {
|
||||||
|
return f.n, f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteWithWriterError(t *testing.T) {
|
||||||
|
expectedError := errors.New("expected")
|
||||||
|
expectedReturnedBytes := 10
|
||||||
|
writer := NewStdWriter(&errWriter{
|
||||||
|
n: stdWriterPrefixLen + expectedReturnedBytes,
|
||||||
|
err: expectedError}, Stdout)
|
||||||
|
data := []byte("This won't get written, sigh")
|
||||||
|
n, err := writer.Write(data)
|
||||||
|
if err != expectedError {
|
||||||
|
t.Fatalf("Didn't get expected error.")
|
||||||
|
}
|
||||||
|
if n != expectedReturnedBytes {
|
||||||
|
t.Fatalf("Didn't get expected writen bytes %d, got %d.",
|
||||||
|
expectedReturnedBytes, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteDoesNotReturnNegativeWrittenBytes(t *testing.T) {
|
||||||
|
writer := NewStdWriter(&errWriter{n: -1}, Stdout)
|
||||||
|
data := []byte("This won't get written, sigh")
|
||||||
|
actual, _ := writer.Write(data)
|
||||||
|
if actual != 0 {
|
||||||
|
t.Fatalf("Expected returned written bytes equal to 0, got %d", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStdCopyWithInvalidInputHeader(t *testing.T) {
|
func TestStdCopyWithInvalidInputHeader(t *testing.T) {
|
||||||
dstOut := NewStdWriter(ioutil.Discard, Stdout)
|
dstOut := NewStdWriter(ioutil.Discard, Stdout)
|
||||||
dstErr := NewStdWriter(ioutil.Discard, Stderr)
|
dstErr := NewStdWriter(ioutil.Discard, Stderr)
|
||||||
|
|
Loading…
Reference in a new issue