-
Notifications
You must be signed in to change notification settings - Fork 7
/
init_test.go
94 lines (80 loc) · 1.61 KB
/
init_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
package xmlwriter
import (
"bytes"
"io"
"io/ioutil"
"runtime"
)
var memstats runtime.MemStats
func allocs() uint64 {
runtime.ReadMemStats(&memstats)
return memstats.Mallocs
}
type DodgyWriter struct {
writer io.Writer
shouldFail func(b []byte) (fail bool, len int, err error)
}
func (d *DodgyWriter) Write(b []byte) (len int, err error) {
if fail, len, err := d.shouldFail(b); fail {
return len, err
}
return d.writer.Write(b)
}
func open(o ...Option) (*bytes.Buffer, *Writer) {
b := &bytes.Buffer{}
w := Open(b, o...)
return b, w
}
func openNull(o ...Option) *Writer {
return Open(ioutil.Discard, o...)
}
func str(b *bytes.Buffer, w *Writer) string {
must(w.Flush())
return b.String()
}
func doWrite(node ...Writable) string {
b, w := open()
for _, n := range node {
must(w.Write(n))
}
return str(b, w)
}
func doStart(node ...Startable) string {
b, w := open()
for _, n := range node {
must(w.Start(n))
}
return str(b, w)
}
func doBlock(start Startable, children ...Writable) string {
b, w := open()
must(w.Block(start, children...))
return str(b, w)
}
func doWriteErrMsg(nodes ...Writable) (ret string) {
defer func() {
if e := recover(); e != nil {
ret = e.(error).Error()
}
}()
doWrite(nodes...)
return ""
}
func doStartErrMsg(nodes ...Startable) (ret string) {
defer func() {
if e := recover(); e != nil {
ret = e.(error).Error()
}
}()
doStart(nodes...)
return ""
}
func doBlockErrMsg(start Startable, children ...Writable) (ret string) {
defer func() {
if e := recover(); e != nil {
ret = e.(error).Error()
}
}()
doBlock(start, children...)
return ""
}