@@ -62,8 +62,59 @@ func TestMultiReader(t *testing.T) {
62
62
}
63
63
64
64
func TestMultiWriter (t * testing.T ) {
65
- sha1 := sha1 .New ()
66
65
sink := new (bytes.Buffer )
66
+ // Hide bytes.Buffer's WriteString method:
67
+ testMultiWriter (t , struct {
68
+ Writer
69
+ fmt.Stringer
70
+ }{sink , sink })
71
+ }
72
+
73
+ func TestMultiWriter_String (t * testing.T ) {
74
+ testMultiWriter (t , new (bytes.Buffer ))
75
+ }
76
+
77
+ // test that a multiWriter.WriteString calls results in at most 1 allocation,
78
+ // even if multiple targets don't support WriteString.
79
+ func TestMultiWriter_WriteStringSingleAlloc (t * testing.T ) {
80
+ var sink1 , sink2 bytes.Buffer
81
+ type simpleWriter struct { // hide bytes.Buffer's WriteString
82
+ Writer
83
+ }
84
+ mw := MultiWriter (simpleWriter {& sink1 }, simpleWriter {& sink2 })
85
+ allocs := int (testing .AllocsPerRun (1000 , func () {
86
+ WriteString (mw , "foo" )
87
+ }))
88
+ if allocs != 1 {
89
+ t .Errorf ("num allocations = %d; want 1" , allocs )
90
+ }
91
+ }
92
+
93
+ type writeStringChecker struct { called bool }
94
+
95
+ func (c * writeStringChecker ) WriteString (s string ) (n int , err error ) {
96
+ c .called = true
97
+ return len (s ), nil
98
+ }
99
+
100
+ func (c * writeStringChecker ) Write (p []byte ) (n int , err error ) {
101
+ return len (p ), nil
102
+ }
103
+
104
+ func TestMultiWriter_StringCheckCall (t * testing.T ) {
105
+ var c writeStringChecker
106
+ mw := MultiWriter (& c )
107
+ WriteString (mw , "foo" )
108
+ if ! c .called {
109
+ t .Error ("did not see WriteString call to writeStringChecker" )
110
+ }
111
+ }
112
+
113
+ func testMultiWriter (t * testing.T , sink interface {
114
+ Writer
115
+ fmt.Stringer
116
+ }) {
117
+ sha1 := sha1 .New ()
67
118
mw := MultiWriter (sha1 , sink )
68
119
69
120
sourceString := "My input text."
0 commit comments