-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhomepage_test.go
More file actions
200 lines (170 loc) · 4.17 KB
/
homepage_test.go
File metadata and controls
200 lines (170 loc) · 4.17 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package glyph_test
// compiler-verified examples matching the homepage code blocks.
// if these break, the homepage is lying.
import (
"io"
"os/exec"
"strings"
"testing"
. "github.com/kungfusheep/glyph"
)
type homepageCommit struct {
Short string
Subject string
Author string
}
type homepageProc struct {
PID int
Command string
CPU float64
Mem float64
}
type homepagePkg struct {
Name string
Desc string
}
// homepage: section 01, first app
func TestHomepage_firstApp(t *testing.T) {
app := NewApp()
app.SetView(
VBox(
Text("Hello, glyph"),
Text("Press q to quit"),
),
)
app.Handle("q", app.Stop)
_ = app
}
// homepage: section 02, compose real layouts
func TestHomepage_composeLayouts(t *testing.T) {
var files []string
preview := ""
_ = HBox(
VBox.Grow(1).Border(BorderRounded)(
List(&files).BindVimNav().OnSelect(func(f *string) {
preview = *f
}),
),
VBox.Grow(2).Border(BorderRounded)(
TextView(&preview).Grow(1),
),
)
}
// homepage: section 02, stream anything
func TestHomepage_streamAnything(t *testing.T) {
// streaming plumbing exercised against a real io.Reader — no subprocess.
pr, pw := io.Pipe()
defer pw.Close()
_ = Log(pr).Grow(1).MaxLines(1000)
// example:
app := NewApp()
cmd := exec.Command("go", "test", "./...")
stdout, _ := cmd.StdoutPipe()
app.SetView(
VBox(
Text("test output").Bold(),
Log(stdout).Grow(1).MaxLines(1000),
),
)
_ = app
// :example
}
// homepage: section 02, conditional rendering
func TestHomepage_conditionalRendering(t *testing.T) {
connected := true
uptime := "3h 42m"
_ = If(&connected).Then(
HBox.Gap(2)(
Text("Status").Bold(),
Text(&uptime).FG(Green),
),
).Else(
Text("Disconnected").FG(Red),
)
}
// homepage: section 02, render any slice
func TestHomepage_renderAnySlice(t *testing.T) {
var commits []homepageCommit
_ = List(&commits).Render(func(c *homepageCommit) Component {
return HBox.Gap(2)(
Text(&c.Short).FG(Yellow),
Text(&c.Subject),
Text(&c.Author).FG(BrightBlack),
)
}).BindVimNav()
}
// homepage: section 02, process monitor
func TestHomepage_processMonitor(t *testing.T) {
var procs []homepageProc
cpuPct := 0.72
memPct := 0.45
_ = VBox(
HBox.Gap(4)(
Text("CPU"), Progress(&cpuPct).Width(30),
Text("Mem"), Progress(&memPct).Width(30),
),
AutoTable(&procs).Sortable().Scrollable(20).BindVimNav(),
)
}
// homepage: section 02, deploy log
func TestHomepage_deployLog(t *testing.T) {
frame := 0
status := "deploying..."
pct := 0.0
done := false
result := "deployed"
output := strings.NewReader("")
_ = VBox.Border(BorderRounded).Title("deploy")(
HBox.Gap(2)(
Spinner(&frame).FG(Cyan),
Text(&status).Bold(),
Progress(&pct).Width(20),
),
Log(output).Grow(1).MaxLines(500),
If(&done).Then(Text(&result).Bold().FG(Green)),
)
}
// homepage: section 02, fuzzy finder
func TestHomepage_fuzzyFinder(t *testing.T) {
var packages []homepagePkg
_ = FilterList(&packages, func(p *homepagePkg) string { return p.Name }).
Render(func(p *homepagePkg) Component {
return HBox.Gap(2)(
Text(&p.Name).Bold(),
Text(&p.Desc).FG(BrightBlack),
)
}).MaxVisible(15).Border(BorderRounded).Title("packages")
}
// homepage: section 02, live dashboard
func TestHomepage_liveDashboard(t *testing.T) {
var reqData, latData []float64
reqRate := "1,204 req/s"
p99 := "12ms"
events := strings.NewReader("")
_ = VBox(
HBox.Gap(1)(
VBox.Grow(1).Border(BorderRounded).Title("requests/s")(
Sparkline(&reqData).FG(Green),
Text(&reqRate).FG(BrightBlack),
),
VBox.Grow(1).Border(BorderRounded).Title("p99 latency")(
Sparkline(&latData).FG(Yellow),
Text(&p99).FG(BrightBlack),
),
),
Log(events).Grow(1).MaxLines(200),
)
}
// homepage: section 02, forms with validation
func TestHomepage_formsWithValidation(t *testing.T) {
var name, email string
role := 0
agree := false
register := func(*FormC) {}
_ = Form.LabelBold().OnSubmit(register)(
Field("Name", Input(&name).Validate(VRequired, VOnBlur)),
Field("Email", Input(&email).Validate(VEmail, VOnBlur)),
Field("Role", Radio(&role, "Admin", "User", "Guest")),
Field("Terms", Checkbox(&agree, "I accept").Validate(VTrue, VOnSubmit)),
)
}