forked from mitchellh/prefixedio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
96 lines (85 loc) · 1.5 KB
/
reader_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package prefixedio
import (
"bytes"
"io"
"strings"
"testing"
)
func TestReader(t *testing.T) {
var fooBuf, barBuf, defBuf bytes.Buffer
original := bytes.NewReader([]byte(strings.TrimSpace(testInput)))
r, err := NewReader(original)
if err != nil {
t.Fatalf("err: %s", err)
}
pFoo, err := r.Prefix("foo: ")
if err != nil {
t.Fatalf("err: %s", err)
}
_, err = r.Prefix("foo: ")
if err == nil {
t.Fatalf("expected prefix already registered error")
}
pBar, err := r.Prefix("bar: ")
if err != nil {
t.Fatalf("err: %s", err)
}
pDefault, err := r.Prefix("")
if err != nil {
t.Fatalf("err: %s", err)
}
doneCh := make(chan struct{}, 3)
go func() {
if _, err := io.Copy(&fooBuf, pFoo); err != nil {
t.Fatalf("err: %s", err)
}
doneCh <- struct{}{}
}()
go func() {
if _, err := io.Copy(&barBuf, pBar); err != nil {
t.Fatalf("err: %s", err)
}
doneCh <- struct{}{}
}()
go func() {
if _, err := io.Copy(&defBuf, pDefault); err != nil {
t.Fatalf("err: %s", err)
}
doneCh <- struct{}{}
}()
// Wait for all the reads to be done
<-doneCh
<-doneCh
<-doneCh
if fooBuf.String() != testInputFoo {
t.Fatalf("bad: %s", fooBuf.String())
}
if barBuf.String() != testInputBar {
t.Fatalf("bad: %s", barBuf.String())
}
if defBuf.String() != testInputDef {
t.Fatalf("bad: %s", defBuf.String())
}
}
const testInput = `
what
hello
foo: 1
bar: 2
bar: 3
42
foo: 4
foo: 5
bar: 6
`
const testInputFoo = `1
4
5
`
const testInputBar = `2
3
42
6`
const testInputDef = `what
hello
`