| // Copyright 2010 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package io_test |
| |
| import ( |
| . "io" |
| "bytes" |
| "crypto/sha1" |
| "fmt" |
| "strings" |
| "testing" |
| ) |
| |
| func TestMultiWriter(t *testing.T) { |
| sha1 := sha1.New() |
| sink := new(bytes.Buffer) |
| mw := MultiWriter(sha1, sink) |
| |
| sourceString := "My input text." |
| source := strings.NewReader(sourceString) |
| written, err := Copy(mw, source) |
| |
| if written != int64(len(sourceString)) { |
| t.Errorf("short write of %d, not %d", written, len(sourceString)) |
| } |
| |
| if err != nil { |
| t.Errorf("unexpected error: %v", err) |
| } |
| |
| sha1hex := fmt.Sprintf("%x", sha1.Sum()) |
| if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" { |
| t.Error("incorrect sha1 value") |
| } |
| |
| if sink.String() != sourceString { |
| t.Error("expected %q; got %q", sourceString, sink.String()) |
| } |
| } |