-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast_test.go
58 lines (49 loc) · 1.03 KB
/
ast_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
package anchor
import (
"bytes"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNode_Kind(t *testing.T) {
t.Parallel()
assert.Equal(t, Kind, new(Node).Kind())
}
func TestNode_Dump(t *testing.T) {
getStdout := hijackStdout(t)
(&Node{
ID: []byte("foo-bar"),
Level: 1,
Value: []byte("#"),
}).Dump(nil, 0)
got := getStdout()
assert.Contains(t, got, "Anchor {\n")
assert.Contains(t, got, " Value: #\n")
assert.Contains(t, got, " Level: 1\n")
assert.Contains(t, got, " ID: foo-bar\n")
assert.Contains(t, got, "}\n")
}
func hijackStdout(t testing.TB) func() string {
stdout := os.Stdout
t.Cleanup(func() {
os.Stdout = stdout
})
r, w, err := os.Pipe()
done := make(chan struct{})
var buff bytes.Buffer
go func() {
defer close(done)
_, err := io.Copy(&buff, r)
assert.NoError(t, err)
}()
require.NoError(t, err)
os.Stdout = w
return func() string {
os.Stdout = stdout
assert.NoError(t, w.Close())
<-done
return buff.String()
}
}