-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathRenderTest.cs
More file actions
412 lines (373 loc) · 16.7 KB
/
RenderTest.cs
File metadata and controls
412 lines (373 loc) · 16.7 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
using System;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell;
using Xunit;
namespace Test
{
public partial class ReadLine
{
[SkippableFact]
public void ClearScreen()
{
TestSetup(KeyMode.Emacs);
Test("echo 1\necho 2\necho 3", Keys(
"echo 1",
_.Shift_Enter,
"echo 2",
_.Shift_Enter,
"echo 3"));
AssertCursorTopIs(3);
Test("echo foo", Keys(
"echo foo"
), resetCursor: false);
AssertCursorTopIs(4);
Test("echo zed", Keys(
"echo zed",
_.Ctrl_l,
CheckThat(() => AssertCursorTopIs(0))
), resetCursor: false);
}
[SkippableFact]
public void Render()
{
TestSetup(KeyMode.Cmd);
Test("", Keys(
"abc -def <#123#> \"hello $name\" 1 + (1-2)",
_.Home,
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.Command, "abc",
TokenClassification.None, " ",
TokenClassification.Parameter, "-def",
TokenClassification.None, " ",
TokenClassification.Comment, "<#123#>",
TokenClassification.None, " ",
TokenClassification.String, "\"hello ",
TokenClassification.Variable, "$name",
TokenClassification.String, "\"",
TokenClassification.None, " ",
TokenClassification.Number, "1",
TokenClassification.None, " + (",
TokenClassification.Number, "1",
TokenClassification.Operator, "-",
TokenClassification.Number, "2",
TokenClassification.None, ")")),
_.Ctrl_c,
InputAcceptedNow
));
// This tests for priority to highlight a command regardless of token kind and nested tokens potential to bleed the parent token color to the next token
Test("", Keys(
". -abc def;. abc$name -def",
_.Home,
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.None, ". ",
TokenClassification.Command, "-abc",
TokenClassification.None, " def;. ",
TokenClassification.Command, "abc",
TokenClassification.Variable, "$name",
TokenClassification.None, " ",
TokenClassification.Parameter, "-def")),
_.Ctrl_c,
InputAcceptedNow
));
// Additional test for priority to highlight a command regardless of token kind and nested tokens potential to bleed the parent token color to the next token
Test("", Keys(
". ++ abc$name -def",
_.Home,
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.None, ". ",
TokenClassification.Command, "++",
TokenClassification.None, " abc",
TokenClassification.Variable, "$name",
TokenClassification.None, " ",
TokenClassification.Parameter, "-def")),
_.Ctrl_c,
InputAcceptedNow
));
// test that rendering doesn't cause an exception in a potential missing "EOS" token case.
// this case could be a moving target, if the PowerShell parser is changed such as to eliminate the case.
Test("", Keys(
"process $abc\\name | def",
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.Keyword, "process",
TokenClassification.None, " ",
TokenClassification.Variable, "$abc",
TokenClassification.None, "\\name | def")),
_.Ctrl_c,
InputAcceptedNow
));
Test("", Keys(
"process out put",
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.Keyword, "process",
TokenClassification.None, " out put")),
_.Ctrl_c,
InputAcceptedNow
));
Test("", Keys(
"\"$([int];\"_$(1+2)\")\"",
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.String, "\"",
TokenClassification.None, "$(",
TokenClassification.None, "[",
TokenClassification.Type, "int",
TokenClassification.None, "];",
TokenClassification.String, "\"_",
TokenClassification.None, "$(",
TokenClassification.Number, "1",
TokenClassification.Operator, "+",
TokenClassification.Number, "2",
TokenClassification.None, ")",
TokenClassification.String, "\"",
TokenClassification.None, ")",
TokenClassification.String, "\"")),
_.Ctrl_c,
InputAcceptedNow
));
Test("", Keys(
"\"a $b c $d e\"",
CheckThat(() =>
AssertScreenIs(1,
TokenClassification.String, "\"a ",
TokenClassification.Variable, "$b",
TokenClassification.String, " c ",
TokenClassification.Variable, "$d",
TokenClassification.String, " e\"")),
_.Ctrl_c,
InputAcceptedNow
));
Test("{}", Keys(
'{', _.Enter,
_.Backspace, CheckThat(() => AssertScreenIs(2, TokenClassification.None, '{', NextLine)),
'}'));
_console.Clear();
string promptLine = "PS> ";
Test("\"\"", Keys(
'"',
CheckThat(() => AssertScreenIs(1,
TokenClassification.None,
promptLine.Substring(0, promptLine.IndexOf('>')),
Tuple.Create(ConsoleColor.Red, ConsoleColor.DarkRed), "> ",
TokenClassification.String, "\"")),
'"'), prompt: promptLine);
}
[SkippableFact]
public void MultiLine()
{
TestSetup(KeyMode.Cmd);
Test("d|\nd", Keys(
"d|",
_.Enter, CheckThat(() => AssertCursorTopIs(1)),
'd'));
// Make sure <ENTER> when input is incomplete actually puts a newline
// wherever the cursor is.
Test("{\n\nd\n}", Keys(
'{',
_.Enter, CheckThat(() => AssertCursorTopIs(1)),
'd',
_.Enter, CheckThat(() => AssertCursorTopIs(2)),
_.Home,
_.RightArrow, CheckThat(() => AssertCursorLeftTopIs(1, 0)),
_.Enter, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 1)),
_.End, CheckThat(() => AssertCursorLeftTopIs(ContinuationPromptLength, 3)),
'}'));
// Make sure <ENTER> when input successfully parses accepts the input regardless
// of where the cursor is, plus it moves the cursor to the end (so the new prompt
// doesn't overwrite the end of the previous long/multi-line command line.)
Test("{\n}", Keys(
"{\n}",
_.Home,
_.Enter, CheckThat(() => AssertCursorLeftTopIs(0, 2))));
}
[SkippableFact]
public void MultiLine_ScreenCheck()
{
Skip.If(ScreenReaderModeEnabled, "Continuation prompt is not supported in screen reader mode.");
TestSetup(KeyMode.Cmd);
var defaultContinuationPrompt = PSConsoleReadLineOptions.DefaultContinuationPrompt;
var defaultContinuationPromptColors = Tuple.Create(_console.ForegroundColor, _console.BackgroundColor);
Test("@'\n\n hello\n\n world\n'@",
Keys(
"@'", _.Enter, _.Enter,
" hello", _.Enter, _.Enter,
" world", _.Enter, "'@",
CheckThat(() => AssertScreenIs(6,
TokenClassification.String, "@'",
NextLine,
defaultContinuationPromptColors, defaultContinuationPrompt,
NextLine,
defaultContinuationPromptColors, defaultContinuationPrompt,
TokenClassification.String, " hello",
NextLine,
defaultContinuationPromptColors, defaultContinuationPrompt,
NextLine,
defaultContinuationPromptColors, defaultContinuationPrompt,
TokenClassification.String, " world",
NextLine,
defaultContinuationPromptColors, defaultContinuationPrompt,
TokenClassification.String, "'@"))
));
// Set the continuation prompt to be an empty string.
var setOption = new SetPSReadLineOption { ContinuationPrompt = string.Empty };
PSConsoleReadLine.SetOptions(setOption);
try
{
Test("@'\n\n hello\n\n world\n'@",
Keys(
"@'", _.Enter, _.Enter,
" hello", _.Enter, _.Enter,
" world", _.Enter, "'@",
CheckThat(() => AssertScreenIs(6,
TokenClassification.String, "@'",
NextLine,
NextLine,
TokenClassification.String, " hello",
NextLine,
NextLine,
TokenClassification.String, " world",
NextLine,
TokenClassification.String, "'@"))
));
}
finally
{
// Restore to the default continuation prompt.
setOption.ContinuationPrompt = defaultContinuationPrompt;
PSConsoleReadLine.SetOptions(setOption);
}
}
[SkippableFact]
public void LongLine()
{
TestSetup(KeyMode.Cmd);
var sb = new StringBuilder();
sb.Append('"');
sb.Append('z', _console.BufferWidth);
sb.Append('"');
var input = sb.ToString();
Test(input, Keys(input));
}
[SkippableFact]
public void InvokePrompt()
{
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+z", PSConsoleReadLine.InvokePrompt));
// Test dumb prompt that doesn't return anything.
using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
ps.AddScript(@"function prompt {}");
ps.Invoke();
}
Test("dir", Keys(
"dir", _.Ctrl_z,
CheckThat(() => AssertScreenIs(1,
Tuple.Create(_console.ForegroundColor, _console.BackgroundColor), "PS>",
TokenClassification.Command, "dir"))));
// Test a boring prompt function
using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
ps.AddScript(@"function prompt { 'PSREADLINE> ' }");
ps.Invoke();
}
Test("dir", Keys(
"dir", _.Ctrl_z,
CheckThat(() => AssertScreenIs(1,
Tuple.Create(_console.ForegroundColor, _console.BackgroundColor), "PSREADLINE> ",
TokenClassification.Command, "dir"))));
}
[SkippableFact]
public void InvokeTrickyPrompt()
{
Skip.If(ScreenReaderModeEnabled, "We can't test the colors written in this prompt in screen reader mode.");
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+z", PSConsoleReadLine.InvokePrompt));
// Tricky prompt - writes to console directly with colors, uses ^H trick to eliminate trailing space.
using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
ps.AddCommand("New-Variable").AddParameter("Name", "__console").AddParameter("Value", _console).Invoke();
ps.Commands.Clear();
ps.AddScript(@"
function prompt {
$fg = $__console.ForegroundColor
$bg = $__console.BackgroundColor
$__console.ForegroundColor = [ConsoleColor]::Blue
$__console.BackgroundColor = [ConsoleColor]::Magenta
$__console.Write('PSREADLINE>')
$__console.ForegroundColor = $fg
$__console.BackgroundColor = $bg
return ' ' + ([char]8)
}");
ps.Invoke();
}
Test("dir", Keys(
"dir", _.Ctrl_z,
CheckThat(() => AssertScreenIs(1,
Tuple.Create(ConsoleColor.Blue, ConsoleColor.Magenta), "PSREADLINE>",
TokenClassification.Command, "dir"))));
}
[SkippableFact]
public void LengthInBufferCells_SurrogatePair()
{
// 👉 is U+1F449, encoded as surrogate pair \uD83D\uDC49
// A surrogate pair should count as 1 buffer cell.
Assert.Equal(1, PSConsoleReadLine.LengthInBufferCells("\uD83D\uDC49"));
}
[SkippableFact]
public void LengthInBufferCells_MultipleSurrogatePairs()
{
// "👉❌" = two surrogate pairs, should be 2 buffer cells
Assert.Equal(2, PSConsoleReadLine.LengthInBufferCells("👉❌"));
}
[SkippableFact]
public void LengthInBufferCells_SurrogatePairWithText()
{
// "👉 " = surrogate pair + space = 2 buffer cells
Assert.Equal(2, PSConsoleReadLine.LengthInBufferCells("👉 "));
// "❌ " = surrogate pair + space = 2 buffer cells
Assert.Equal(2, PSConsoleReadLine.LengthInBufferCells("❌ "));
// "abc👉def" = 3 + 1 + 3 = 7
Assert.Equal(7, PSConsoleReadLine.LengthInBufferCells("abc👉def"));
}
[SkippableFact]
public void LengthInBufferCells_SurrogatePairWithEscapeSequence()
{
// ESC[31m (red color) + surrogate pair + ESC[0m (reset)
// Only the surrogate pair should count: 1 buffer cell
Assert.Equal(1, PSConsoleReadLine.LengthInBufferCells("\x1b[31m\uD83D\uDC49\x1b[0m"));
}
[SkippableFact]
public void LengthInBufferCells_NerdFontGlyph()
{
// U+F015A (nf-md-home) = \uDB80\uDD5A - a supplementary character in the private use area
// "╰ " = 1 + 1 + 1 = 3 buffer cells (the issue reported this as 4 before the fix)
Assert.Equal(3, PSConsoleReadLine.LengthInBufferCells("╰\uDB80\uDD5A "));
}
[SkippableFact]
public void LengthInBufferCells_LoneHighSurrogate()
{
// A lone high surrogate without a following low surrogate should still
// be handled without error (falls through to LengthInBufferCells(char))
var str = "a\uD83Db";
int expected = 1 + PSConsoleReadLine.LengthInBufferCells('\uD83D') + 1;
Assert.Equal(expected, PSConsoleReadLine.LengthInBufferCells(str));
}
[SkippableFact]
public void LengthInBufferCells_StringBuilder_SurrogatePair()
{
var sb = new StringBuilder("👉 ");
// surrogate pair + space = 2 buffer cells
Assert.Equal(2, PSConsoleReadLine.LengthInBufferCells(sb, 0, sb.Length));
}
[SkippableFact]
public void LengthInBufferCells_StringBuilder_SurrogatePairSubstring()
{
var sb = new StringBuilder("abc👉def");
// Just the surrogate pair portion (indices 3 and 4) = 1 buffer cell
Assert.Equal(1, PSConsoleReadLine.LengthInBufferCells(sb, 3, 5));
}
}
}