-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-printer.go
66 lines (55 loc) · 1.14 KB
/
test-printer.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
package toolprinter
import (
"fmt"
"strings"
)
type TestPrinter struct {
defaultPrinter
partial bool
lines []string
}
func NewTestPrinter() *TestPrinter {
tp := TestPrinter{}
tp.stdoutPrinter = tp.print
tp.statusPrinter = func(a ...interface{}) (n int, err error) { text := fmt.Sprint(a...); n = len(text); return }
tp.lines = make([]string, 0)
return &tp
}
func (tp *TestPrinter) print(args ...interface{}) (n int, err error) {
text := fmt.Sprint(args...)
n = len(text)
parts := strings.Split(text, "\n")
end := len(parts) - 1
pos := 0
if tp.partial {
tp.lines[len(tp.lines)-1] += parts[0]
if pos == end {
return
}
pos++
tp.partial = false
}
for pos < end {
tp.lines = append(tp.lines, parts[pos])
pos++
}
if len(parts[pos]) > 0 {
tp.lines = append(tp.lines, parts[pos])
tp.partial = true
}
return
}
func (tp *TestPrinter) GetStatusText() string {
return tp.lastStatusText
}
func (tp *TestPrinter) GetLines() []string {
return tp.lines
}
func (tp *TestPrinter) String() string {
var sb strings.Builder
for _, line := range tp.lines {
sb.WriteString(line)
sb.WriteRune('\n')
}
return sb.String()
}