forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicEditingTest.cs
More file actions
463 lines (381 loc) · 19 KB
/
Copy pathBasicEditingTest.cs
File metadata and controls
463 lines (381 loc) · 19 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using Microsoft.PowerShell;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Xunit;
namespace Test
{
public partial class ReadLine
{
[SkippableFact]
public void Input()
{
TestSetup(KeyMode.Cmd);
Test("exit", Keys(
"exit",
_.Enter,
CheckThat(() => AssertCursorLeftIs(0))));
}
[SkippableFact]
public void RevertLine()
{
// Add one test for chords
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+x,Escape", PSConsoleReadLine.RevertLine));
Test("ls", Keys("di", _.Escape, "ls"));
Test("ls", Keys("di", _.Ctrl_x, _.Escape, "ls"));
TestSetup(KeyMode.Emacs);
Test("ls", Keys("di", _.Escape, _.r, "ls"));
Test("ls", Keys("di", _.Alt_r, "ls"));
}
[SkippableFact]
public void CancelLine()
{
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+C", PSConsoleReadLine.CancelLine));
Test("", Keys("oops", _.Ctrl_C,
CheckThat(() => AssertScreenIs(1,
TokenClassification.Command, "oops",
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
Test("", Keys("exit", _.Ctrl_C,
CheckThat(() => AssertScreenIs(1,
TokenClassification.Keyword, "exit",
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
// Test near/at/over buffer width input
var width = _console.BufferWidth;
var line1 = new string('a', width - 2);
Test("", Keys(line1, _.Ctrl_C,
CheckThat(() => AssertScreenIs(1,
TokenClassification.Command, line1,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
var line2 = new string('a', width - 1);
Test("", Keys(line2, _.Ctrl_C,
CheckThat(() => AssertScreenIs(2,
TokenClassification.Command, line2,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
var line3 = new string('a', width);
Test("", Keys(line3, _.Ctrl_C,
CheckThat(() => AssertScreenIs(2,
TokenClassification.Command, line3,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
}
[SkippableFact]
public void ForwardDeleteInput()
{
ConsoleKeyInfo deleteToEnd;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
TestSetup(KeyMode.Cmd);
deleteToEnd = _.Ctrl_End;
}
else
{
TestSetup(KeyMode.Emacs);
deleteToEnd = _.Ctrl_k;
}
// Empty input (does nothing but don't crash)
Test("", Keys("", deleteToEnd));
// at end of input - doesn't change anything
Test("abc", Keys("abc", deleteToEnd));
// More normal usage - actually delete stuff
Test("a", Keys("abc", _.LeftArrow, _.LeftArrow, deleteToEnd));
}
[SkippableFact]
public void ForwardDeleteLine()
{
TestSetup(KeyMode.Emacs);
PSConsoleReadLine.SetKeyHandler(new[] { "Shift+Tab" }, PSConsoleReadLine.ForwardDeleteLine, "", "");
Test("\"H\nWorld\"", Keys(
_.DQuote, "Hello", _.Enter,
"World", _.DQuote,
_.Home, _.Home, _.RightArrow, _.RightArrow,
_.Shift_Tab // delete to the end of the current logical line
));
}
[SkippableFact]
public void BackwardDeleteInput()
{
TestSetup(KeyMode.Cmd);
// Empty input (does nothing but don't crash)
Test("", Keys(_.Ctrl_Home));
// at beginning of input - doesn't change anything
Test("abc", Keys("abc", _.Home, _.Ctrl_Home));
// More typical usage
Test("c", Keys("abc", _.LeftArrow, _.Ctrl_Home));
Test("", Keys("abc", _.Ctrl_Home));
}
[SkippableFact]
public void BackwardDeleteChar()
{
TestSetup(KeyMode.Cmd);
// Empty input (does nothing but don't crash)
Test("", Keys("", _.Backspace));
// At end, delete all input
Test("", Keys("a", _.Backspace));
// At end, delete all input with extra backspaces
Test("", Keys("a", _.Backspace, _.Backspace));
// Delete first character
Test("b", Keys("ab", _.LeftArrow, _.Backspace));
// Delete first character with extra backspaces
Test("b", Keys("ab", _.LeftArrow, _.Backspace, _.Backspace));
// Delete middle character
Test("ac", Keys("abc", _.LeftArrow, _.Backspace));
}
[SkippableFact]
public void DeleteChar()
{
TestSetup(KeyMode.Cmd);
// Empty input (does nothing, but don't crash)
Test("", Keys(_.Delete));
// At end but input not empty (does nothing, but don't crash)
Test("a", Keys('a', _.Delete));
// Delete last character
Test("a", Keys("ab", _.LeftArrow, _.Delete));
// Delete first character
Test("b", Keys("ab", _.Home, _.Delete));
// Delete middle character
Test("ac", Keys("abc", _.Home, _.RightArrow, _.Delete));
}
[SkippableFact]
public void DeleteCharAfterDigitArgument()
{
TestSetup(KeyMode.Cmd);
Test("abc", Keys(
"ab", _.LeftArrow, _.Alt_2, _.Delete,
CheckThat(() => AssertLineIs("a")),
CheckThat(() => AssertCursorLeftIs(1)),
_.Delete, // 'Delete' again does nothing, but doesn't crash
CheckThat(() => AssertLineIs("a")),
CheckThat(() => AssertCursorLeftIs(1)),
"bc"));
}
[SkippableFact]
public void DeleteCharOrExit()
{
TestSetup(KeyMode.Emacs);
Test("exit", Keys(_.Ctrl_d, InputAcceptedNow));
Test("foo", Keys("foo", _.Ctrl_d));
Test("oo", Keys("foo", _.Home, _.Ctrl_d));
Test("exit", Keys("foo", _.Home, Enumerable.Repeat(_.Ctrl_d, 4), InputAcceptedNow));
}
[SkippableFact]
public void UpcaseWord()
{
TestSetup(KeyMode.Emacs);
Test("FOO", Keys("foo", _.Alt_b, _.Alt_u));
Test("FOO bar", Keys("foo bar", _.Home, _.Alt_u));
Test("foo BAR", Keys("foo bar", _.Alt_b, _.Alt_u));
Test("FOO BAR", Keys("foo bar", _.Home, Enumerable.Repeat(_.Alt_u, 2)));
}
[SkippableFact]
public void DowncaseWord()
{
TestSetup(KeyMode.Emacs);
Test("foo", Keys("fOO", _.Alt_b, _.Alt_l));
Test("FOO bar", Keys("FOO BAR", _.Alt_b, _.Alt_l));
Test("foo BAR", Keys("FOO BAR", _.Home, _.Alt_l));
Test("foo bar", Keys("FOO BAR", _.Home, Enumerable.Repeat(_.Alt_l, 2)));
}
[SkippableFact]
public void CapitalizeWord()
{
TestSetup(KeyMode.Emacs);
Test("Foo", Keys("fOO", _.Alt_b, _.Alt_c));
Test("fOO Bar", Keys("fOO bAR", _.Alt_b, _.Alt_c));
Test("Foo BAR", Keys("fOO BAR", _.Home, _.Alt_c));
Test("Foo Bar", Keys("fOO BAR", _.Home, Enumerable.Repeat(_.Alt_c, 2)));
Test("Foo ^&*() Bar", Keys("Foo ^&*() bar", _.Home, Enumerable.Repeat(_.Alt_c, 2)));
}
[SkippableFact]
public void SelectAndDelete()
{
TestSetup(KeyMode.Cmd);
Test("abcde", Keys(
"abcde",
CheckThat(() => AssertCursorLeftIs(5)),
_.Shift_LeftArrow, _.Shift_LeftArrow, _.Shift_LeftArrow,
_.Backspace,
CheckThat(() => AssertLineIs("ab")),
CheckThat(() => AssertCursorLeftIs(2)),
_.Ctrl_z,
CheckThat(() => AssertLineIs("abcde")),
CheckThat(() => AssertCursorLeftIs(5))));
Test("abcde", Keys(
"abcde", _.Home,
CheckThat(() => AssertCursorLeftIs(0)),
_.RightArrow, _.RightArrow,
CheckThat(() => AssertCursorLeftIs(2)),
_.Shift_RightArrow, _.Shift_RightArrow,
_.Delete,
CheckThat(() => AssertLineIs("abe")),
CheckThat(() => AssertCursorLeftIs(2)),
_.Ctrl_z,
CheckThat(() => AssertLineIs("abcde")),
CheckThat(() => AssertCursorLeftIs(4))));
}
[SkippableFact]
public void SwapCharacters()
{
TestSetup(KeyMode.Emacs);
TestMustDing("", Keys(_.Ctrl_t));
TestMustDing("a", Keys("a", _.Ctrl_t));
TestMustDing("abc", Keys("abc", _.Home, _.Ctrl_t));
Test("abc", Keys(
"abc", CheckThat(() => AssertLineIs("abc")),
_.Ctrl_t, CheckThat(() => AssertLineIs("acb")),
_.Ctrl_Underbar, CheckThat(() => AssertCursorLeftIs(3))
));
Test("abcd", Keys(
"abcd", CheckThat(() => AssertLineIs("abcd")),
_.Ctrl_a, _.Ctrl_t, CheckThat(() => AssertLineIs("abcd")),
_.Ctrl_f, Enumerable.Repeat(_.Ctrl_t, 3), CheckThat(() => AssertLineIs("bcda")),
_.Ctrl_t, CheckThat(() => AssertLineIs("bcad")),
Enumerable.Repeat(_.Ctrl_Underbar, 4)
));
}
[SkippableFact]
public void AcceptAndGetNext()
{
TestSetup(KeyMode.Emacs);
// No history
Test("", Keys(_.Ctrl_o, InputAcceptedNow));
// One item in history
SetHistory("echo 1");
Test("", Keys(_.Ctrl_o, InputAcceptedNow));
// Two items in history, make sure after Ctrl+O, second history item
// is recalled.
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys(_.UpArrow, _.UpArrow, _.Ctrl_o, InputAcceptedNow));
Test("echo 2", Keys(_.Enter));
// Test that the current saved line is saved after AcceptAndGetNext
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.Ctrl_o, InputAcceptedNow));
Test("e", Keys(_.DownArrow, _.DownArrow, _.Enter));
// Test that we can edit after recalling the current line
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.Ctrl_o, InputAcceptedNow));
Test("eee", Keys(_.DownArrow, _.DownArrow, "ee", _.Enter));
}
[SkippableFact]
public void AcceptAndGetNextWithHistorySearch()
{
TestSetup(KeyMode.Emacs,
new KeyHandler("UpArrow", PSConsoleReadLine.HistorySearchBackward),
new KeyHandler("DownArrow", PSConsoleReadLine.HistorySearchForward));
// Test that after AcceptAndGetNext, the previous search is not applied
SetHistory("echo 1", "echo 2", "zzz");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.Ctrl_o, InputAcceptedNow));
Test("zzz", Keys(_.DownArrow, _.Enter));
}
[SkippableFact]
public void AddLine()
{
TestSetup(KeyMode.Cmd);
Test("1\n2", Keys('1', _.Shift_Enter, '2'));
}
[SkippableFact]
public void InsertLineAbove()
{
TestSetup(KeyMode.Cmd);
// Test case - start with single line, cursor at end
Test("56\n1234", Keys("1234", _.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0, 0)), "56"));
// Test case - start with single line, cursor in home position
Test("56\n1234", Keys("1234", _.Home, _.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0, 0)), "56"));
// Test case - start with single line, cursor in middle
Test("56\n1234", Keys("1234",
_.LeftArrow, _.LeftArrow, _.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
"56"));
// Test case - start with multi-line, cursor at end of second line (end of input)
Test("1234\n9ABC\n5678", Keys("1234", _.Shift_Enter, "5678",
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of second line
Test("1234\n9ABC\n5678", Keys("1234", _.Shift_Enter, "5678",
_.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"9ABC"));
// Test case - start with multi-line, cursor at end of first line
Test("9ABC\n1234\n5678", Keys("1234", _.Shift_Enter, "5678",
_.UpArrow, _.LeftArrow, _.End, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of first line - temporarily having to press Home twice to
// work around bug in home handler.
Test("9ABC\n1234\n5678", Keys("1234", _.Shift_Enter, "5678",
_.UpArrow, _.LeftArrow, _.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
"9ABC"));
// Test case - insert multiple blank lines
Test("1234\n9ABC\n\n5678", Keys("1234", _.Shift_Enter, "5678",
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"9ABC"));
// Test case - create leading blank line, cursor to stay on same line
Test("\n\n1234", Keys("1234",
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0,0)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1))));
}
[SkippableFact]
public void InsertLineBelow()
{
TestSetup(KeyMode.Cmd);
// Test case - start with single line, cursor at end
Test("1234\n56", Keys("1234",
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"56"));
// Test case - start with single line, cursor in home position
Test("1234\n56", Keys("1234",
_.Home, _.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"56"));
// Test case - start with single line, cursor in middle
Test("1234\n56", Keys("1234",
_.LeftArrow, _.LeftArrow, _.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"56"));
// Test case - start with multi-line, cursor at end of second line (end of input)
Test("1234\n5678\n9ABC", Keys("1234", _.Shift_Enter, "5678",
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 2)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of second line
Test("1234\n5678\n9ABC", Keys("1234", _.Shift_Enter, "5678",
_.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 2)),
"9ABC"));
// Test case - start with multi-line, cursor at end of first line
Test("1234\n9ABC\n5678", Keys("1234", _.Shift_Enter, "5678",
_.UpArrow, _.LeftArrow, _.End, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of first line - temporarily having to press Home twice to
// work around bug in home handler.
Test("1234\n9ABC\n5678", Keys("1234", _.Shift_Enter, "5678",
_.UpArrow, _.LeftArrow, _.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
"9ABC"));
// Test case - insert multiple blank lines
Test("1234\n5678\n\n9ABC", Keys("1234", _.Shift_Enter, "5678",
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 2)),
_.Ctrl_Shift_Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 3)),
"9ABC"));
}
[SkippableFact]
public void MultilineHomeBugFixed()
{
TestSetup(KeyMode.Cmd);
// Going from second line to first line, press left arrow and then home.
// That puts cursor in column 1 instead of 0. Bug? This could have something
// to do with BeginningOfLine testing against i > 1 in multiline edit instead
// of i > 0.
Test("1234\n9ABC", Keys("1234", _.Shift_Enter, "9ABC", _.UpArrow, _.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0))));
}
[SkippableFact]
public void Ignore()
{
TestSetup(KeyMode.Emacs);
Test("ab", Keys("a", _.VolumeDown, _.VolumeMute, _.VolumeUp, "b"));
}
}
}