forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementTest.cs
More file actions
247 lines (211 loc) · 11.3 KB
/
Copy pathMovementTest.cs
File metadata and controls
247 lines (211 loc) · 11.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
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PSConsoleUtilities;
namespace UnitTestPSReadLine
{
// Disgusting language hack to make it easier to read a sequence of keys.
using _ = Keys;
public partial class UnitTest
{
[TestMethod]
public void TestEndOfLine()
{
TestSetup(KeyMode.Cmd);
Test("", Keys( _.End, CheckThat(() => AssertCursorLeftIs(0)) ));
var buffer = new string(' ', Console.BufferWidth);
Test(buffer, Keys(
buffer,
_.Home,
CheckThat(() => AssertCursorLeftIs(0)),
_.End,
CheckThat(() => AssertCursorLeftTopIs(0, 1))
));
buffer = new string(' ', Console.BufferWidth + 5);
Test(buffer, Keys(
buffer,
_.Home,
CheckThat(() => AssertCursorLeftIs(0)),
_.End,
CheckThat(() => AssertCursorLeftTopIs(5, 1))
));
}
[TestMethod]
public void TestMultilineCursorMovement()
{
TestSetup(KeyMode.Cmd);
var continutationPromptLength = PSConsoleReadlineOptions.DefaultContinuationPrompt.Length;
Test("", Keys(
"4444", _.ShiftEnter,
"666666", _.ShiftEnter,
"88888888", _.ShiftEnter,
"666666", _.ShiftEnter,
"4444", _.ShiftEnter,
// Starting at the end of the next to last line (because it's not blank)
// Verify that Home first goes to the start of the line, then the start of the input.
_.LeftArrow,
_.Home, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 4)),
_.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
// Now (because we're at the start), verify first End goes to end of the line
// and the second End goes to the end of the input.
_.End, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.End, CheckThat(() => AssertCursorLeftTopIs(0 + continutationPromptLength, 5)),
_.Home, _.Home,
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 2)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 3)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 4)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 5)),
_.LeftArrow, _.Home,
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 3)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 2)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
// Make sure that movement between lines stays at the end of a line if it starts
// at the end of a line
_.End, _.End,
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(4 + continutationPromptLength, 4)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 3)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(8 + continutationPromptLength, 2)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 1)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(4, 0)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 1)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(8 + continutationPromptLength, 2)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 3)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(4 + continutationPromptLength, 4)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(0 + continutationPromptLength, 5)),
_.Escape,
_.ShiftEnter,
"88888888", _.ShiftEnter,
"55555", _.ShiftEnter,
"22", _.ShiftEnter,
"55555", _.ShiftEnter,
"88888888",
_.LeftArrow, _.LeftArrow,
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 4)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(2 + continutationPromptLength, 3)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 2)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 1)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.UpArrow, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 1)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 2)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(2 + continutationPromptLength, 3)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 4)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 5)),
// Clear the input, we were just testing movement
_.Escape
));
}
[TestMethod]
public void TestCursorMovement()
{
TestSetup(KeyMode.Cmd);
Test("abcde", Keys(
// Left arrow at start of line.
_.LeftArrow, CheckThat(() => AssertCursorLeftIs(0)),
"ace",
_.LeftArrow, CheckThat(() => AssertCursorLeftIs(2)),
'd',
_.LeftArrow, CheckThat(() => AssertCursorLeftIs(2)),
_.LeftArrow, CheckThat(() => AssertCursorLeftIs(1)),
'b'
));
// Test with digit arguments
var input = "0123456789";
Test(input, Keys(
_.Alt9, _.LeftArrow, CheckThat(() => AssertCursorLeftIs(0)),
_.Alt9, _.RightArrow, CheckThat(() => AssertCursorLeftIs(0)),
input,
_.Alt5, _.LeftArrow, CheckThat(() => AssertCursorLeftIs(5)),
_.AltMinus, _.Alt2, _.LeftArrow, CheckThat(() => AssertCursorLeftIs(7)),
_.Alt2, _.RightArrow, CheckThat(() => AssertCursorLeftIs(9)),
_.AltMinus, _.Alt7, _.RightArrow, CheckThat(() => AssertCursorLeftIs(2))));
}
[TestMethod]
public void TestGotoBrace()
{
TestSetup(KeyMode.Cmd);
// Test empty input
Test("", Keys(_.CtrlRBracket));
Test("(11)", Keys("(11)", _.LeftArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(0))));
Test("$a[11]", Keys("$a[11]", _.LeftArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(2))));
Test("{11}", Keys("{11}", _.LeftArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(0))));
Test("(11)", Keys("(11)", _.Home, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(3))));
Test("$a[11]", Keys("$a[11]", _.Home, _.RightArrow, _.RightArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(5))));
Test("{11}", Keys("{11}", _.Home, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(3))));
// Test multiples, make sure we go to the right one.
Test("((11))", Keys("((11))", _.LeftArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(0))));
Test("((11))", Keys("((11))", _.LeftArrow, _.LeftArrow, _.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(1))));
// Make sure we don't match inside a string
TestMustDing("", Keys(
"'()'", _.LeftArrow, _.LeftArrow,
_.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(2)),
_.CtrlC, InputAcceptedNow));
foreach (var c in new[] {'(', ')', '{', '}', '[', ']'})
{
TestMustDing("", Keys(
'a', c, _.LeftArrow,
_.CtrlRBracket, CheckThat(() => AssertCursorLeftIs(1)),
_.CtrlC, InputAcceptedNow));
}
}
[TestMethod]
public void TestCharacterSearch()
{
TestSetup(KeyMode.Cmd);
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3", _.Home,
_.F3, '|', CheckThat(() => AssertCursorLeftIs(5)),
_.F3, '|', CheckThat(() => AssertCursorLeftIs(12))));
TestSetup(KeyMode.Emacs);
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3", _.Home,
_.CtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(5)),
_.CtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(12)),
_.End,
_.AltMinus, _.Alt2, _.CtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(5)),
_.Home,
_.Alt2, _.CtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(12))));
TestMustDing("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.CtrlRBracket, 'z'));
int i = 0;
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+z",
(key, count) => PSConsoleReadLine.CharacterSearch(null, i++ == 0 ? (object)'|' : "|")));
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.Home,
_.CtrlZ, CheckThat(() => AssertCursorLeftIs(5)),
_.CtrlZ, CheckThat(() => AssertCursorLeftIs(12))));
}
[TestMethod]
public void TestCharacterSearchBackward()
{
TestSetup(KeyMode.Cmd);
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.ShiftF3, '|', CheckThat(() => AssertCursorLeftIs(12)),
_.ShiftF3, '|', CheckThat(() => AssertCursorLeftIs(5))));
TestSetup(KeyMode.Emacs);
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.AltCtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(12)),
_.AltCtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(5)),
_.Home,
_.AltMinus, _.Alt2, _.AltCtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(12)),
_.End,
_.Alt2, _.AltCtrlRBracket, '|', CheckThat(() => AssertCursorLeftIs(5))));
TestMustDing("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.AltCtrlRBracket, 'z'));
int i = 0;
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+z",
(key, count) => PSConsoleReadLine.CharacterSearchBackward(null, i++ == 0 ? (object)'|' : "|")));
Test("cmd1 | cmd2 | cmd3", Keys(
"cmd1 | cmd2 | cmd3",
_.CtrlZ, CheckThat(() => AssertCursorLeftIs(12)),
_.CtrlZ, CheckThat(() => AssertCursorLeftIs(5))));
}
}
}