forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
159 lines (144 loc) · 3.76 KB
/
testing.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package tui
import (
"bytes"
"fmt"
"image"
"strconv"
)
type testCell struct {
Rune rune
Style Style
}
// A TestSurface implements the Surface interface with local buffers,
// and provides accessors to check the output of a draw operation on the Surface.
type TestSurface struct {
cells map[image.Point]testCell
cursor image.Point
size image.Point
emptyCh rune
}
// NewTestSurface returns a new TestSurface.
func NewTestSurface(w, h int) *TestSurface {
return &TestSurface{
cells: make(map[image.Point]testCell),
size: image.Point{w, h},
emptyCh: '.',
}
}
// SetCell sets the contents of the addressed cell.
func (s *TestSurface) SetCell(x, y int, ch rune, style Style) {
s.cells[image.Point{x, y}] = testCell{
Rune: ch,
Style: style,
}
}
// SetCursor moves the Surface's cursor to the specified position.
func (s *TestSurface) SetCursor(x, y int) {
s.cursor = image.Point{x, y}
}
// HideCursor removes the cursor from the display.
func (s *TestSurface) HideCursor() {
s.cursor = image.Point{}
}
// Begin resets the state of the TestSurface, clearing all cells.
// It must be called before drawing the Surface.
func (s *TestSurface) Begin() {
s.cells = make(map[image.Point]testCell)
}
// End indicates the surface has been painted on, and can be rendered.
// It's a no-op for TestSurface.
func (s *TestSurface) End() {
// NOP
}
// Size returns the dimensions of the surface.
func (s *TestSurface) Size() image.Point {
return s.size
}
// String returns the characters written to the TestSurface.
func (s *TestSurface) String() string {
var buf bytes.Buffer
buf.WriteRune('\n')
for j := 0; j < s.size.Y; j++ {
for i := 0; i < s.size.X; i++ {
if cell, ok := s.cells[image.Point{i, j}]; ok {
buf.WriteRune(cell.Rune)
if w := runeWidth(cell.Rune); w > 1 {
i += w - 1
}
} else {
buf.WriteRune(s.emptyCh)
}
}
buf.WriteRune('\n')
}
return buf.String()
}
// FgColors renders the TestSurface's foreground colors, using the digits 0-7 for painted cells, and the empty character for unpainted cells.
func (s *TestSurface) FgColors() string {
var buf bytes.Buffer
buf.WriteRune('\n')
for j := 0; j < s.size.Y; j++ {
for i := 0; i < s.size.X; i++ {
if cell, ok := s.cells[image.Point{i, j}]; ok {
color := cell.Style.Fg
buf.WriteRune('0' + rune(color))
} else {
buf.WriteRune(s.emptyCh)
}
}
buf.WriteRune('\n')
}
return buf.String()
}
// BgColors renders the TestSurface's background colors, using the digits 0-7 for painted cells, and the empty character for unpainted cells.
func (s *TestSurface) BgColors() string {
var buf bytes.Buffer
buf.WriteRune('\n')
for j := 0; j < s.size.Y; j++ {
for i := 0; i < s.size.X; i++ {
if cell, ok := s.cells[image.Point{i, j}]; ok {
color := cell.Style.Bg
buf.WriteRune('0' + rune(color))
} else {
buf.WriteRune(s.emptyCh)
}
}
buf.WriteRune('\n')
}
return buf.String()
}
// Decorations renders the TestSurface's decorations (Reverse, Bold, Underline) using a bitmask:
// Reverse: 1
// Bold: 2
// Underline: 4
func (s *TestSurface) Decorations() string {
var buf bytes.Buffer
buf.WriteRune('\n')
for j := 0; j < s.size.Y; j++ {
for i := 0; i < s.size.X; i++ {
if cell, ok := s.cells[image.Point{i, j}]; ok {
mask := int64(0)
if cell.Style.Reverse == DecorationOn {
mask |= 1
}
if cell.Style.Bold == DecorationOn {
mask |= 2
}
if cell.Style.Underline == DecorationOn {
mask |= 4
}
buf.WriteString(strconv.FormatInt(mask, 16))
} else {
buf.WriteRune(s.emptyCh)
}
}
buf.WriteRune('\n')
}
return buf.String()
}
func surfaceEquals(surface *TestSurface, want string) string {
if surface.String() != want {
return fmt.Sprintf("got = \n%s\n\nwant = \n%s", surface.String(), want)
}
return ""
}