1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestMultiReader(t
*testing
.T
) {
20 withFooBar
:= func(tests
func()) {
21 r1
:= strings
.NewReader("foo ")
22 r2
:= strings
.NewReader("")
23 r3
:= strings
.NewReader("bar")
24 mr
= MultiReader(r1
, r2
, r3
)
25 buf
= make([]byte, 20)
28 expectRead
:= func(size
int, expected
string, eerr error
) {
30 n
, gerr
:= mr
.Read(buf
[0:size
])
31 if n
!= len(expected
) {
32 t
.Errorf("#%d, expected %d bytes; got %d",
33 nread
, len(expected
), n
)
35 got
:= string(buf
[0:n
])
37 t
.Errorf("#%d, expected %q; got %q",
41 t
.Errorf("#%d, expected error %v; got %v",
47 expectRead(2, "fo", nil)
48 expectRead(5, "o ", nil)
49 expectRead(5, "bar", nil)
50 expectRead(5, "", EOF
)
53 expectRead(4, "foo ", nil)
54 expectRead(1, "b", nil)
55 expectRead(3, "ar", nil)
56 expectRead(1, "", EOF
)
59 expectRead(5, "foo ", nil)
63 func TestMultiWriter(t
*testing
.T
) {
65 sink
:= new(bytes
.Buffer
)
66 mw
:= MultiWriter(sha1
, sink
)
68 sourceString
:= "My input text."
69 source
:= strings
.NewReader(sourceString
)
70 written
, err
:= Copy(mw
, source
)
72 if written
!= int64(len(sourceString
)) {
73 t
.Errorf("short write of %d, not %d", written
, len(sourceString
))
77 t
.Errorf("unexpected error: %v", err
)
80 sha1hex
:= fmt
.Sprintf("%x", sha1
.Sum(nil))
81 if sha1hex
!= "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
82 t
.Error("incorrect sha1 value")
85 if sink
.String() != sourceString
{
86 t
.Errorf("expected %q; got %q", sourceString
, sink
.String())