-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoreservice_test.go
More file actions
227 lines (181 loc) · 5.54 KB
/
coreservice_test.go
File metadata and controls
227 lines (181 loc) · 5.54 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCoreServiceDefaults(t *testing.T) {
t.Parallel()
type Expectation struct {
InsertMode bool
Wraparound bool
ApplicationCursorKeys bool
BracketedPasteMode bool
IsCursorInitialized bool
KittyKeyboardFlags int
}
opts := NewOptionsService(nil)
cs := NewCoreService(opts)
got := Expectation{
InsertMode: cs.Modes.InsertMode,
Wraparound: cs.DecPrivateModes.Wraparound,
ApplicationCursorKeys: cs.DecPrivateModes.ApplicationCursorKeys,
BracketedPasteMode: cs.DecPrivateModes.BracketedPasteMode,
IsCursorInitialized: cs.IsCursorInitialized,
KittyKeyboardFlags: cs.KittyKeyboard.Flags,
}
expected := Expectation{
InsertMode: false,
Wraparound: true,
ApplicationCursorKeys: false,
BracketedPasteMode: false,
IsCursorInitialized: false,
KittyKeyboardFlags: 0,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceShowCursorImmediately(t *testing.T) {
t.Parallel()
type Expectation struct {
IsCursorInitialized bool
}
opts := NewOptionsService(&TerminalOptions{ShowCursorImmediately: true})
cs := NewCoreService(opts)
got := Expectation{IsCursorInitialized: cs.IsCursorInitialized}
expected := Expectation{IsCursorInitialized: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceReset(t *testing.T) {
t.Parallel()
type Expectation struct {
InsertMode bool
Wraparound bool
Flags int
}
opts := NewOptionsService(nil)
cs := NewCoreService(opts)
cs.Modes.InsertMode = true
cs.DecPrivateModes.Wraparound = false
cs.KittyKeyboard.Flags = 42
cs.Reset()
got := Expectation{
InsertMode: cs.Modes.InsertMode,
Wraparound: cs.DecPrivateModes.Wraparound,
Flags: cs.KittyKeyboard.Flags,
}
expected := Expectation{InsertMode: false, Wraparound: true, Flags: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceTriggerDataEvent(t *testing.T) {
t.Parallel()
type Expectation struct {
DataEvents []string
UserInputs int
}
opts := NewOptionsService(nil)
cs := NewCoreService(opts)
var dataEvents []string
userInputs := 0
cs.OnDataEmitter.Event(func(s string) { dataEvents = append(dataEvents, s) })
cs.OnUserInputEmitter.Event(func(struct{}) { userInputs++ })
cs.TriggerDataEvent("hello", false, false)
cs.TriggerDataEvent("world", true, false)
got := Expectation{DataEvents: dataEvents, UserInputs: userInputs}
expected := Expectation{DataEvents: []string{"hello", "world"}, UserInputs: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceTriggerDataEventDisableStdin(t *testing.T) {
t.Parallel()
type Expectation struct {
DataEvents int
}
opts := NewOptionsService(&TerminalOptions{DisableStdin: true})
cs := NewCoreService(opts)
count := 0
cs.OnDataEmitter.Event(func(string) { count++ })
cs.TriggerDataEvent("hello", false, false)
got := Expectation{DataEvents: count}
expected := Expectation{DataEvents: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceTriggerDataEventScrollToBottom(t *testing.T) {
t.Parallel()
type Expectation struct {
ScrollRequests int
}
opts := NewOptionsService(&TerminalOptions{ScrollOnUserInput: true})
cs := NewCoreService(opts)
scrollRequests := 0
cs.OnRequestScrollToBottomEmitter.Event(func(struct{}) { scrollRequests++ })
// shouldScroll=true means ybase != ydisp
cs.TriggerDataEvent("a", true, true)
// shouldScroll=false means already at bottom
cs.TriggerDataEvent("b", true, false)
got := Expectation{ScrollRequests: scrollRequests}
expected := Expectation{ScrollRequests: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceTriggerBinaryEvent(t *testing.T) {
t.Parallel()
type Expectation struct {
BinaryEvents []string
}
opts := NewOptionsService(nil)
cs := NewCoreService(opts)
var events []string
cs.OnBinaryEmitter.Event(func(s string) { events = append(events, s) })
cs.TriggerBinaryEvent("\x1b[2J")
got := Expectation{BinaryEvents: events}
expected := Expectation{BinaryEvents: []string{"\x1b[2J"}}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceTriggerBinaryEventDisableStdin(t *testing.T) {
t.Parallel()
type Expectation struct {
BinaryEvents int
}
opts := NewOptionsService(&TerminalOptions{DisableStdin: true})
cs := NewCoreService(opts)
count := 0
cs.OnBinaryEmitter.Event(func(string) { count++ })
cs.TriggerBinaryEvent("data")
got := Expectation{BinaryEvents: count}
expected := Expectation{BinaryEvents: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCoreServiceDispose(t *testing.T) {
t.Parallel()
type Expectation struct {
DataEvents int
BinaryEvents int
}
opts := NewOptionsService(nil)
cs := NewCoreService(opts)
dataCount := 0
binaryCount := 0
cs.OnDataEmitter.Event(func(string) { dataCount++ })
cs.OnBinaryEmitter.Event(func(string) { binaryCount++ })
cs.Dispose()
cs.TriggerDataEvent("a", false, false)
cs.TriggerBinaryEvent("b")
got := Expectation{DataEvents: dataCount, BinaryEvents: binaryCount}
expected := Expectation{DataEvents: 0, BinaryEvents: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}