-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_test.go
More file actions
369 lines (321 loc) · 9.61 KB
/
options_test.go
File metadata and controls
369 lines (321 loc) · 9.61 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestDefaultOptions(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
Scrollback int
CursorStyle CursorStyle
CursorWidth int
TabStopWidth int
FontFamily string
FontSize int
LineHeight float64
LogLevel LogLevel
TermName string
}
opts := DefaultOptions()
got := Expectation{
Cols: opts.Cols,
Rows: opts.Rows,
Scrollback: opts.Scrollback,
CursorStyle: opts.CursorStyle,
CursorWidth: opts.CursorWidth,
TabStopWidth: opts.TabStopWidth,
FontFamily: opts.FontFamily,
FontSize: opts.FontSize,
LineHeight: opts.LineHeight,
LogLevel: opts.LogLevel,
TermName: opts.TermName,
}
expected := Expectation{
Cols: 80,
Rows: 24,
Scrollback: 1000,
CursorStyle: CursorStyleBlock,
CursorWidth: 1,
TabStopWidth: 8,
FontFamily: "monospace",
FontSize: 15,
LineHeight: 1.0,
LogLevel: LogLevelInfo,
TermName: "xterm",
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestNewOptionsServiceDefaults(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
Scrollback int
}
s := NewOptionsService(nil)
got := Expectation{
Cols: s.Options.Cols,
Rows: s.Options.Rows,
Scrollback: s.Options.Scrollback,
}
expected := Expectation{Cols: 80, Rows: 24, Scrollback: 1000}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestNewOptionsServiceOverrides(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
Scrollback int
FontFamily string
}
s := NewOptionsService(&TerminalOptions{
Cols: 120,
Rows: 40,
Scrollback: 5000,
})
got := Expectation{
Cols: s.Options.Cols,
Rows: s.Options.Rows,
Scrollback: s.Options.Scrollback,
FontFamily: s.Options.FontFamily,
}
expected := Expectation{Cols: 120, Rows: 40, Scrollback: 5000, FontFamily: "monospace"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceSetOption(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
FiredEvent string
}
s := NewOptionsService(nil)
var firedName string
s.OnOptionChangeEmitter.Event(func(name string) {
firedName = name
})
s.SetOption("cols", 120)
got := Expectation{Cols: s.Options.Cols, FiredEvent: firedName}
expected := Expectation{Cols: 120, FiredEvent: "cols"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceSetOptionNoChangeNoFire(t *testing.T) {
t.Parallel()
type Expectation struct {
FireCount int
}
s := NewOptionsService(nil)
count := 0
s.OnOptionChangeEmitter.Event(func(string) { count++ })
s.SetOption("cols", 80) // same as default
got := Expectation{FireCount: count}
expected := Expectation{FireCount: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceSetOptionScrollback(t *testing.T) {
t.Parallel()
type TestCase struct {
Name string
Value int
Expected int
}
tests := []TestCase{
{"normal value", 500, 500},
{"negative clamped to 0", -1, 0},
{"max clamped", MaxBufferSize + 1, MaxBufferSize},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
s := NewOptionsService(nil)
s.SetOption("scrollback", tc.Value)
type Expectation struct {
Scrollback int
}
got := Expectation{Scrollback: s.Options.Scrollback}
expected := Expectation{Scrollback: tc.Expected}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestOptionsServiceSetOptionCursorStyle(t *testing.T) {
t.Parallel()
type TestCase struct {
Name string
Value CursorStyle
Expected CursorStyle
}
tests := []TestCase{
{"block", CursorStyleBlock, CursorStyleBlock},
{"underline", CursorStyleUnderline, CursorStyleUnderline},
{"bar", CursorStyleBar, CursorStyleBar},
{"invalid stays default", CursorStyle("invalid"), CursorStyleBlock},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
s := NewOptionsService(nil)
s.SetOption("cursorStyle", tc.Value)
type Expectation struct {
CursorStyle CursorStyle
}
got := Expectation{CursorStyle: s.Options.CursorStyle}
expected := Expectation{CursorStyle: tc.Expected}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestOptionsServiceSetOptionTabStopWidth(t *testing.T) {
t.Parallel()
type Expectation struct {
TabStopWidth int
}
t.Run("valid value", func(t *testing.T) {
t.Parallel()
s := NewOptionsService(nil)
s.SetOption("tabStopWidth", 4)
got := Expectation{TabStopWidth: s.Options.TabStopWidth}
expected := Expectation{TabStopWidth: 4}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
t.Run("zero rejected", func(t *testing.T) {
t.Parallel()
s := NewOptionsService(nil)
s.SetOption("tabStopWidth", 0)
got := Expectation{TabStopWidth: s.Options.TabStopWidth}
expected := Expectation{TabStopWidth: 8} // default unchanged
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
func TestOptionsServiceOnSpecificOptionChange(t *testing.T) {
t.Parallel()
type Expectation struct {
CallCount int
}
s := NewOptionsService(nil)
count := 0
s.OnSpecificOptionChange("cols", func() { count++ })
s.SetOption("cols", 120)
s.SetOption("rows", 40) // should not trigger
got := Expectation{CallCount: count}
expected := Expectation{CallCount: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceOnMultipleOptionChange(t *testing.T) {
t.Parallel()
type Expectation struct {
CallCount int
}
s := NewOptionsService(nil)
count := 0
s.OnMultipleOptionChange([]string{"cols", "rows"}, func() { count++ })
s.SetOption("cols", 120)
s.SetOption("rows", 40)
s.SetOption("cursorBlink", true) // should not trigger
got := Expectation{CallCount: count}
expected := Expectation{CallCount: 2}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceDispose(t *testing.T) {
t.Parallel()
type Expectation struct {
CallCount int
}
s := NewOptionsService(nil)
count := 0
s.OnOptionChangeEmitter.Event(func(string) { count++ })
s.Dispose()
s.SetOption("cols", 120)
got := Expectation{CallCount: count}
expected := Expectation{CallCount: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOptionsServiceSetOptionConvertEol(t *testing.T) {
t.Parallel()
type Expectation struct {
ConvertEol bool
FireCount int
}
s := NewOptionsService(nil)
count := 0
s.OnOptionChangeEmitter.Event(func(string) { count++ })
s.SetOption("convertEol", true)
got := Expectation{ConvertEol: s.Options.ConvertEol, FireCount: count}
expected := Expectation{ConvertEol: true, FireCount: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestParamToWindowOption(t *testing.T) {
t.Parallel()
type TestCase struct {
Name string
Param int32
Opts WindowOptions
Allowed bool
}
tests := []TestCase{
{"default denies all", 18, WindowOptions{}, false},
{"getWinSizeChars allowed", 18, WindowOptions{GetWinSizeChars: true}, true},
{"restoreWin allowed", 1, WindowOptions{RestoreWin: true}, true},
{"minimizeWin allowed", 2, WindowOptions{MinimizeWin: true}, true},
{"setWinPosition allowed", 3, WindowOptions{SetWinPosition: true}, true},
{"setWinSizePixels allowed", 4, WindowOptions{SetWinSizePixels: true}, true},
{"raiseWin allowed", 5, WindowOptions{RaiseWin: true}, true},
{"lowerWin allowed", 6, WindowOptions{LowerWin: true}, true},
{"refreshWin allowed", 7, WindowOptions{RefreshWin: true}, true},
{"setWinSizeChars allowed", 8, WindowOptions{SetWinSizeChars: true}, true},
{"maximizeWin allowed", 9, WindowOptions{MaximizeWin: true}, true},
{"fullscreenWin allowed", 10, WindowOptions{FullscreenWin: true}, true},
{"getWinState allowed", 11, WindowOptions{GetWinState: true}, true},
{"getWinPosition allowed", 13, WindowOptions{GetWinPosition: true}, true},
{"getWinSizePixels allowed", 14, WindowOptions{GetWinSizePixels: true}, true},
{"getScreenSizePixels allowed", 15, WindowOptions{GetScreenSizePixels: true}, true},
{"getCellSizePixels allowed", 16, WindowOptions{GetCellSizePixels: true}, true},
{"getScreenSizeChars allowed", 19, WindowOptions{GetScreenSizeChars: true}, true},
{"getIconTitle allowed", 20, WindowOptions{GetIconTitle: true}, true},
{"getWinTitle allowed", 21, WindowOptions{GetWinTitle: true}, true},
{"pushTitle allowed", 22, WindowOptions{PushTitle: true}, true},
{"popTitle allowed", 23, WindowOptions{PopTitle: true}, true},
{"setWinLines allowed", 24, WindowOptions{SetWinLines: true}, true},
{"param > 24 uses setWinLines", 25, WindowOptions{SetWinLines: true}, true},
{"param > 24 denied without setWinLines", 30, WindowOptions{}, false},
{"unmapped param 12 denied", 12, WindowOptions{}, false},
{"wrong option for param", 14, WindowOptions{GetCellSizePixels: true}, false},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
got := paramToWindowOption(tc.Param, tc.Opts)
if got != tc.Allowed {
t.Errorf("paramToWindowOption(%d, ...) = %v, want %v", tc.Param, got, tc.Allowed)
}
})
}
}