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
361 lines (289 loc) · 15.3 KB
/
Copy pathBasicEditingTest.cs
File metadata and controls
361 lines (289 loc) · 15.3 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
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.PowerShell;
namespace UnitTestPSReadLine
{
// Disgusting language hack to make it easier to read a sequence of keys.
using _ = Keys;
public partial class UnitTest
{
[TestMethod]
public void TestInput()
{
TestSetup(KeyMode.Cmd);
Test("exit", Keys(
"exit",
_.Enter,
CheckThat(() => AssertCursorLeftIs(0))));
}
[TestMethod]
public void TestRevertLine()
{
// 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", _.CtrlX, _.Escape, "ls"));
TestSetup(KeyMode.Emacs);
Test("ls", Keys("di", _.Escape, _.R, "ls"));
Test("ls", Keys("di", _.AltR, "ls"));
}
[TestMethod]
public void TestCancelLine()
{
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+C", PSConsoleReadLine.CancelLine));
Test("", Keys("oops", _.CtrlC,
CheckThat(() => AssertScreenIs(1,
TokenClassification.Command, "oops",
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
Test("", Keys("exit", _.CtrlC,
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, _.CtrlC,
CheckThat(() => AssertScreenIs(1,
TokenClassification.Command, line1,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
var line2 = new string('a', width - 1);
Test("", Keys(line2, _.CtrlC,
CheckThat(() => AssertScreenIs(2,
TokenClassification.Command, line2,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
var line3 = new string('a', width);
Test("", Keys(line3, _.CtrlC,
CheckThat(() => AssertScreenIs(2,
TokenClassification.Command, line3,
Tuple.Create(ConsoleColor.Red, _console.BackgroundColor), "^C")),
InputAcceptedNow));
}
[TestMethod]
public void TestForwardDeleteLine()
{
TestSetup(KeyMode.Cmd);
// Empty input (does nothing but don't crash)
Test("", Keys("", _.CtrlEnd));
// at end of input - doesn't change anything
Test("abc", Keys("abc", _.CtrlEnd));
// More normal usage - actually delete stuff
Test("a", Keys("abc", _.LeftArrow, _.LeftArrow, _.CtrlEnd));
}
[TestMethod]
public void TestBackwardDeleteLine()
{
TestSetup(KeyMode.Cmd);
// Empty input (does nothing but don't crash)
Test("", Keys(_.CtrlHome));
// at beginning of input - doesn't change anything
Test("abc", Keys("abc", _.Home, _.CtrlHome));
// More typical usage
Test("c", Keys("abc", _.LeftArrow, _.CtrlHome));
Test("", Keys("abc", _.CtrlHome));
}
[TestMethod]
public void TestBackwardDeleteChar()
{
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));
}
[TestMethod]
public void TestDeleteChar()
{
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));
}
[TestMethod]
public void TestDeleteCharOrExit()
{
TestSetup(KeyMode.Emacs);
Test("exit", Keys(_.CtrlD, InputAcceptedNow));
Test("foo", Keys("foo", _.CtrlD));
Test("oo", Keys("foo", _.Home, _.CtrlD));
Test("exit", Keys("foo", _.Home, Enumerable.Repeat(_.CtrlD, 4), InputAcceptedNow));
}
[TestMethod]
public void TestSwapCharacters()
{
TestSetup(KeyMode.Emacs);
Test("abc", Keys(
"abc", CheckThat(() => AssertLineIs("abc")),
_.CtrlT, CheckThat(() => AssertLineIs("acb")),
_.CtrlUnderbar
));
Test("abcd", Keys(
"abcd", CheckThat(() => AssertLineIs("abcd")),
_.CtrlA, _.CtrlT, CheckThat(() => AssertLineIs("abcd")),
_.CtrlF, Enumerable.Repeat(_.CtrlT, 3), CheckThat(() => AssertLineIs("bcda")),
_.CtrlT, CheckThat(() => AssertLineIs("bcad")),
Enumerable.Repeat(_.CtrlUnderbar, 4)
));
}
[TestMethod]
public void TestAcceptAndGetNext()
{
TestSetup(KeyMode.Emacs);
// No history
Test("", Keys(_.CtrlO, InputAcceptedNow));
// One item in history
SetHistory("echo 1");
Test("", Keys(_.CtrlO, 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, _.CtrlO, 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, _.CtrlO, 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, _.CtrlO, InputAcceptedNow));
Test("eee", Keys(_.DownArrow, _.DownArrow, "ee", _.Enter));
}
[TestMethod]
public void TestAcceptAndGetNextWithHistorySearch()
{
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, _.CtrlO, InputAcceptedNow));
Test("zzz", Keys(_.DownArrow, _.Enter));
}
[TestMethod]
public void TestAddLine()
{
TestSetup(KeyMode.Cmd);
Test("1\n2", Keys('1', _.ShiftEnter, '2'));
}
[TestMethod]
public void TestInsertLineAbove()
{
TestSetup(KeyMode.Cmd);
var continutationPromptLength = PSConsoleReadlineOptions.DefaultContinuationPrompt.Length;
// Test case - start with single line, cursor at end
Test("56\n1234", Keys("1234", _.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(0, 0)), "56"));
// Test case - start with single line, cursor in home position
Test("56\n1234", Keys("1234", _.Home, _.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(0, 0)), "56"));
// Test case - start with single line, cursor in middle
Test("56\n1234", Keys("1234",
_.LeftArrow, _.LeftArrow, _.CtrlEnter, 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", _.ShiftEnter, "5678",
_.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of second line
Test("1234\n9ABC\n5678", Keys("1234", _.ShiftEnter, "5678",
_.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"9ABC"));
// Test case - start with multi-line, cursor at end of first line
Test("9ABC\n1234\n5678", Keys("1234", _.ShiftEnter, "5678",
_.UpArrow, _.LeftArrow, _.End, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.CtrlEnter, 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", _.ShiftEnter, "5678",
_.UpArrow, _.LeftArrow, _.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
"9ABC"));
// Test case - insert multiple blank lines
Test("1234\n9ABC\n\n5678", Keys("1234", _.ShiftEnter, "5678",
_.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.CtrlEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"9ABC"));
}
[TestMethod]
public void TestInsertLineBelow()
{
TestSetup(KeyMode.Cmd);
var continutationPromptLength = PSConsoleReadlineOptions.DefaultContinuationPrompt.Length;
// Test case - start with single line, cursor at end
Test("1234\n56", Keys("1234",
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"56"));
// Test case - start with single line, cursor in home position
Test("1234\n56", Keys("1234",
_.Home, _.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"56"));
// Test case - start with single line, cursor in middle
Test("1234\n56", Keys("1234",
_.LeftArrow, _.LeftArrow, _.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"56"));
// Test case - start with multi-line, cursor at end of second line (end of input)
Test("1234\n5678\n9ABC", Keys("1234", _.ShiftEnter, "5678",
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 2)),
"9ABC"));
// Test case - start with multi-line, cursor at beginning of second line
Test("1234\n5678\n9ABC", Keys("1234", _.ShiftEnter, "5678",
_.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 2)),
"9ABC"));
// Test case - start with multi-line, cursor at end of first line
Test("1234\n9ABC\n5678", Keys("1234", _.ShiftEnter, "5678",
_.UpArrow, _.LeftArrow, _.End, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 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", _.ShiftEnter, "5678",
_.UpArrow, _.LeftArrow, _.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"9ABC"));
// Test case - insert multiple blank lines
Test("1234\n5678\n\n9ABC", Keys("1234", _.ShiftEnter, "5678",
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 2)),
_.CtrlShiftEnter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 3)),
"9ABC"));
}
[TestMethod]
public void TestMultilineHomeBugFixed()
{
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", _.ShiftEnter, "9ABC", _.UpArrow, _.LeftArrow, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0))));
}
[TestMethod]
public void TestIgnore()
{
TestSetup(KeyMode.Emacs);
var volumeUp = new ConsoleKeyInfo('\0', ConsoleKey.VolumeUp, false, false, false);
var volumeDown = new ConsoleKeyInfo('\0', ConsoleKey.VolumeDown, false, false, false);
var volumeMute = new ConsoleKeyInfo('\0', ConsoleKey.VolumeMute, false, false, false);
Test("ab", Keys("a", volumeDown, volumeMute, volumeUp, "b"));
}
}
}