forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
128 lines (114 loc) · 3.65 KB
/
table_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
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
package tui
import (
"image"
"testing"
)
var drawTableTests = []struct {
test string
size image.Point
setup func() *Box
want string
}{
{
test: "Long labels are masked (#31)",
size: image.Point{32, 10},
setup: func() *Box {
first := NewTable(0, 0)
first.AppendRow(NewLabel("ABC123"), NewLabel("test"))
first.AppendRow(NewLabel("DEF456"), NewLabel("testing a longer text"))
first.AppendRow(NewLabel("GHI789"), NewLabel("foo"))
first.SetBorder(true)
second := NewVBox(NewLabel("test"))
second.SetBorder(true)
third := NewHBox(first, second)
third.SetBorder(true)
return third
},
want: `
┌──────────────────────────────┐
│┌───────────┬──────────┐┌────┐│
││ABC123 │test ││test││
││ │ ││....││
│├───────────┼──────────┤│....││
││DEF456 │testing a ││....││
│├───────────┼──────────┤│....││
││GHI789 │foo ││....││
│└───────────┴──────────┘└────┘│
└──────────────────────────────┘
`,
},
{
test: "Remove a row from table",
size: image.Point{20, 10},
setup: func() *Box {
table := NewTable(0, 0)
table.AppendRow(NewLabel("A"), NewLabel("apple"))
table.AppendRow(NewLabel("B"), NewLabel("box"))
table.AppendRow(NewLabel("C"), NewLabel("cat"))
table.AppendRow(NewLabel("D"), NewLabel("dog"))
table.SetBorder(true)
table.RemoveRow(1)
box := NewHBox(table)
box.SetBorder(true)
return box
},
want: `
┌──────────────────┐
│┌────────┬───────┐│
││A │apple ││
││ │ ││
│├────────┼───────┤│
││C │cat ││
│├────────┼───────┤│
││D │dog ││
│└────────┴───────┘│
└──────────────────┘
`,
},
{
test: "Remove all rows from table",
size: image.Point{20, 10},
setup: func() *Box {
table := NewTable(0, 0)
table.AppendRow(NewLabel("A"), NewLabel("apple"))
table.AppendRow(NewLabel("B"), NewLabel("box"))
table.AppendRow(NewLabel("C"), NewLabel("cat"))
table.AppendRow(NewLabel("D"), NewLabel("dog"))
table.SetBorder(true)
table.RemoveRows()
box := NewHBox(table)
box.SetBorder(true)
return box
},
want: `
┌──────────────────┐
│┌────────┬───────┐│
││........│.......││
││........│.......││
││........│.......││
││........│.......││
││........│.......││
││........│.......││
│└────────┴───────┘│
└──────────────────┘
`,
},
}
func TestTable_Draw(t *testing.T) {
for _, tt := range drawTableTests {
tt := tt
t.Run(tt.test, func(t *testing.T) {
var surface *testSurface
if tt.size.X == 0 && tt.size.Y == 0 {
surface = newTestSurface(10, 5)
} else {
surface = newTestSurface(tt.size.X, tt.size.Y)
}
painter := NewPainter(surface, NewTheme())
painter.Repaint(tt.setup())
if surface.String() != tt.want {
t.Errorf("got = \n%s\n\nwant = \n%s", surface.String(), tt.want)
}
})
}
}