-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor_test.go
91 lines (81 loc) · 1.7 KB
/
cursor_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
package anansi_test
import (
"bufio"
"bytes"
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/jcorbin/anansi"
"github.com/jcorbin/anansi/ansi"
)
func TestCursor(t *testing.T) {
type step struct {
run func(*VirtualCursor)
expect string
}
for _, tc := range []struct {
name string
steps []step
}{
{"hello", []step{
{func(cur *VirtualCursor) {
cur.To(ansi.Pt(5, 5))
cur.WriteSGR(ansi.SGRRed.FG() | ansi.SGRGreen.BG())
cur.WriteString("hello")
}, "\x1b[5;5H\x1B[0;31;42mhello"},
{func(cur *VirtualCursor) {
cur.To(ansi.Pt(5, 6))
cur.WriteSGR(ansi.SGRBlue.FG() | ansi.SGRGreen.BG())
cur.WriteString("world")
}, "\x1b[6;5H\x1b[34mworld"},
}},
} {
t.Run(tc.name, logBuf.With(func(t *testing.T) {
var out bytes.Buffer
var cur VirtualCursor
for i, step := range tc.steps {
out.Reset()
step.run(&cur)
_, err := cur.WriteTo(&out)
require.NoError(t, err)
assert.Equal(t, step.expect, out.String(), "[%d] expected output", i)
}
}))
}
}
type _logBuf struct {
buf bytes.Buffer
t *testing.T
}
var logBuf _logBuf
func init() {
log.SetOutput(&logBuf)
}
func (lb *_logBuf) With(f func(t *testing.T)) func(t *testing.T) {
return func(t *testing.T) {
lb.t = t
defer func() {
for sc := bufio.NewScanner(&lb.buf); sc.Scan(); {
lb.t.Logf(sc.Text())
}
lb.t = nil
}()
f(t)
}
}
func (lb *_logBuf) Write(p []byte) (n int, err error) {
n, _ = lb.buf.Write(p)
if lb.t != nil {
for p := lb.buf.Bytes(); len(p) > 0; {
i := bytes.IndexByte(p, '\n')
if i < 0 {
break
}
lb.t.Logf(string(p[:i]))
p = p[i+1:]
lb.buf.Next(i + 1)
}
}
return n, nil
}