-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tee_test.go
43 lines (39 loc) · 1.04 KB
/
tee_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package xmlstream
import (
"bytes"
"encoding/xml"
"strings"
"testing"
)
func TestTeeReader(t *testing.T) {
const stream = `<one>two</one>`
d := xml.NewDecoder(strings.NewReader(stream))
wb1 := &bytes.Buffer{}
e1 := xml.NewEncoder(wb1)
wb2 := &bytes.Buffer{}
e2 := xml.NewEncoder(wb2)
n, err := Copy(e2, TeeReader(d, e1))
if err != nil {
t.Fatalf("Unexpected error copying tokens: %q", err)
}
if n != 3 {
t.Fatalf("Wrong number of tokens: want=3, got=%d", n)
}
err = e1.Flush()
if err != nil {
t.Fatalf("Unexpected error flushing e1 tokens: %q", err)
}
err = e2.Flush()
if err != nil {
t.Fatalf("Unexpected error flushing e2 tokens: %q", err)
}
if stream != wb1.String() {
t.Errorf("Unexpected value in output buffer: want=%q, got=%q", stream, wb1.String())
}
if stream != wb2.String() {
t.Errorf("Unexpected value in copy buffer: want=%q, got=%q", stream, wb2.String())
}
}