-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
TerminalBufferTests.cpp
635 lines (498 loc) · 20.2 KB
/
TerminalBufferTests.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include <WexTestClass.h>
#include "../renderer/inc/DummyRenderTarget.hpp"
#include "../cascadia/TerminalCore/Terminal.hpp"
#include "MockTermSettings.h"
#include "consoletaeftemplates.hpp"
#include "TestUtils.h"
using namespace winrt::Microsoft::Terminal::Core;
using namespace Microsoft::Terminal::Core;
using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
namespace TerminalCoreUnitTests
{
class TerminalBufferTests;
};
using namespace TerminalCoreUnitTests;
class TerminalCoreUnitTests::TerminalBufferTests final
{
// !!! DANGER: Many tests in this class expect the Terminal buffer
// to be 80x32. If you change these, you'll probably inadvertently break a
// bunch of tests !!!
static const SHORT TerminalViewWidth = 80;
static const SHORT TerminalViewHeight = 32;
static const SHORT TerminalHistoryLength = 100;
TEST_CLASS(TerminalBufferTests);
TEST_METHOD(TestSimpleBufferWriting);
TEST_METHOD(TestWrappingCharByChar);
TEST_METHOD(TestWrappingALongString);
TEST_METHOD(DontSnapToOutputTest);
TEST_METHOD(TestResetClearTabStops);
TEST_METHOD(TestAddTabStop);
TEST_METHOD(TestClearTabStop);
TEST_METHOD(TestGetForwardTab);
TEST_METHOD(TestGetReverseTab);
TEST_METHOD(TestCursorNotifications);
TEST_METHOD_SETUP(MethodSetup)
{
// STEP 1: Set up the Terminal
term = std::make_unique<Terminal>();
term->Create({ TerminalViewWidth, TerminalViewHeight }, TerminalHistoryLength, emptyRT);
return true;
}
TEST_METHOD_CLEANUP(MethodCleanup)
{
term = nullptr;
return true;
}
private:
void _SetTabStops(std::list<short> columns, bool replace);
std::list<short> _GetTabStops();
DummyRenderTarget emptyRT;
std::unique_ptr<Terminal> term;
};
void TerminalBufferTests::TestSimpleBufferWriting()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
VERIFY_ARE_EQUAL(0, initialView.Top());
VERIFY_ARE_EQUAL(32, initialView.BottomExclusive());
termSm.ProcessString(L"Hello World");
const auto secondView = term->GetViewport();
VERIFY_ARE_EQUAL(0, secondView.Top());
VERIFY_ARE_EQUAL(32, secondView.BottomExclusive());
TestUtils::VerifyExpectedString(termTb, L"Hello World", { 0, 0 });
}
void TerminalBufferTests::TestWrappingCharByChar()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
auto& cursor = termTb.GetCursor();
const auto charsToWrite = gsl::narrow_cast<short>(TestUtils::Test100CharsString.size());
VERIFY_ARE_EQUAL(0, initialView.Top());
VERIFY_ARE_EQUAL(32, initialView.BottomExclusive());
for (auto i = 0; i < charsToWrite; i++)
{
// This is a handy way of just printing the printable characters that
// _aren't_ the space character.
const wchar_t wch = static_cast<wchar_t>(33 + (i % 94));
termSm.ProcessCharacter(wch);
}
const auto secondView = term->GetViewport();
VERIFY_ARE_EQUAL(0, secondView.Top());
VERIFY_ARE_EQUAL(32, secondView.BottomExclusive());
// Verify the cursor wrapped to the second line
VERIFY_ARE_EQUAL(charsToWrite % initialView.Width(), cursor.GetPosition().X);
VERIFY_ARE_EQUAL(1, cursor.GetPosition().Y);
// Verify that we marked the 0th row as _wrapped_
const auto& row0 = termTb.GetRowByOffset(0);
VERIFY_IS_TRUE(row0.WasWrapForced());
const auto& row1 = termTb.GetRowByOffset(1);
VERIFY_IS_FALSE(row1.WasWrapForced());
TestUtils::VerifyExpectedString(termTb, TestUtils::Test100CharsString, { 0, 0 });
}
void TerminalBufferTests::TestWrappingALongString()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
auto& cursor = termTb.GetCursor();
const auto charsToWrite = gsl::narrow_cast<short>(TestUtils::Test100CharsString.size());
VERIFY_ARE_EQUAL(100, charsToWrite);
VERIFY_ARE_EQUAL(0, initialView.Top());
VERIFY_ARE_EQUAL(32, initialView.BottomExclusive());
termSm.ProcessString(TestUtils::Test100CharsString);
const auto secondView = term->GetViewport();
VERIFY_ARE_EQUAL(0, secondView.Top());
VERIFY_ARE_EQUAL(32, secondView.BottomExclusive());
// Verify the cursor wrapped to the second line
VERIFY_ARE_EQUAL(charsToWrite % initialView.Width(), cursor.GetPosition().X);
VERIFY_ARE_EQUAL(1, cursor.GetPosition().Y);
// Verify that we marked the 0th row as _wrapped_
const auto& row0 = termTb.GetRowByOffset(0);
VERIFY_IS_TRUE(row0.WasWrapForced());
const auto& row1 = termTb.GetRowByOffset(1);
VERIFY_IS_FALSE(row1.WasWrapForced());
TestUtils::VerifyExpectedString(termTb, TestUtils::Test100CharsString, { 0, 0 });
}
void TerminalBufferTests::DontSnapToOutputTest()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
VERIFY_ARE_EQUAL(0, initialView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight, initialView.BottomExclusive());
VERIFY_ARE_EQUAL(0, term->_scrollOffset);
// -1 so that we don't print the last \n
for (int i = 0; i < TerminalViewHeight + 8 - 1; i++)
{
termSm.ProcessString(L"x\n");
}
const auto secondView = term->GetViewport();
VERIFY_ARE_EQUAL(8, secondView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight + 8, secondView.BottomExclusive());
VERIFY_ARE_EQUAL(0, term->_scrollOffset);
Log::Comment(L"Scroll up one line");
term->_scrollOffset = 1;
const auto thirdView = term->GetViewport();
VERIFY_ARE_EQUAL(7, thirdView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight + 7, thirdView.BottomExclusive());
VERIFY_ARE_EQUAL(1, term->_scrollOffset);
Log::Comment(L"Print a few lines, to see that the viewport stays where it was");
for (int i = 0; i < 8; i++)
{
termSm.ProcessString(L"x\n");
}
const auto fourthView = term->GetViewport();
VERIFY_ARE_EQUAL(7, fourthView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight + 7, fourthView.BottomExclusive());
VERIFY_ARE_EQUAL(1 + 8, term->_scrollOffset);
Log::Comment(L"Print enough lines to get the buffer just about ready to "
L"circle (on the next newline)");
auto viewBottom = term->_mutableViewport.BottomInclusive();
do
{
termSm.ProcessString(L"x\n");
viewBottom = term->_mutableViewport.BottomInclusive();
} while (viewBottom < termTb.GetSize().BottomInclusive());
const auto fifthView = term->GetViewport();
VERIFY_ARE_EQUAL(7, fifthView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight + 7, fifthView.BottomExclusive());
VERIFY_ARE_EQUAL(TerminalHistoryLength - 7, term->_scrollOffset);
Log::Comment(L"Print 3 more lines, and see that we stick to where the old "
L"rows now are in the buffer (after circling)");
for (int i = 0; i < 3; i++)
{
termSm.ProcessString(L"x\n");
Log::Comment(NoThrowString().Format(
L"_scrollOffset: %d", term->_scrollOffset));
}
const auto sixthView = term->GetViewport();
VERIFY_ARE_EQUAL(4, sixthView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight + 4, sixthView.BottomExclusive());
VERIFY_ARE_EQUAL(TerminalHistoryLength - 4, term->_scrollOffset);
Log::Comment(L"Print 8 more lines, and see that we're now just stuck at the"
L"top of the buffer");
for (int i = 0; i < 8; i++)
{
termSm.ProcessString(L"x\n");
Log::Comment(NoThrowString().Format(
L"_scrollOffset: %d", term->_scrollOffset));
}
const auto seventhView = term->GetViewport();
VERIFY_ARE_EQUAL(0, seventhView.Top());
VERIFY_ARE_EQUAL(TerminalViewHeight, seventhView.BottomExclusive());
VERIFY_ARE_EQUAL(TerminalHistoryLength, term->_scrollOffset);
}
void TerminalBufferTests::_SetTabStops(std::list<short> columns, bool replace)
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
auto& cursor = termTb.GetCursor();
const auto clearTabStops = L"\033[3g";
const auto addTabStop = L"\033H";
if (replace)
{
termSm.ProcessString(clearTabStops);
}
for (auto column : columns)
{
cursor.SetXPosition(column);
termSm.ProcessString(addTabStop);
}
}
std::list<short> TerminalBufferTests::_GetTabStops()
{
std::list<short> columns;
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
const auto lastColumn = initialView.RightInclusive();
auto& cursor = termTb.GetCursor();
cursor.SetPosition({ 0, 0 });
for (;;)
{
termSm.ProcessCharacter(L'\t');
auto column = cursor.GetPosition().X;
if (column >= lastColumn)
{
break;
}
columns.push_back(column);
}
return columns;
}
void TerminalBufferTests::TestResetClearTabStops()
{
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
const auto clearTabStops = L"\033[3g";
const auto resetToInitialState = L"\033c";
Log::Comment(L"Default tabs every 8 columns.");
std::list<short> expectedStops{ 8, 16, 24, 32, 40, 48, 56, 64, 72 };
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Clear all tabs.");
termSm.ProcessString(clearTabStops);
expectedStops = {};
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"RIS resets tabs to defaults.");
termSm.ProcessString(resetToInitialState);
expectedStops = { 8, 16, 24, 32, 40, 48, 56, 64, 72 };
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
}
void TerminalBufferTests::TestAddTabStop()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
auto& cursor = termTb.GetCursor();
const auto clearTabStops = L"\033[3g";
const auto addTabStop = L"\033H";
Log::Comment(L"Clear all tabs.");
termSm.ProcessString(clearTabStops);
std::list<short> expectedStops{};
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Add tab to empty list.");
cursor.SetXPosition(12);
termSm.ProcessString(addTabStop);
expectedStops.push_back(12);
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Add tab to head of existing list.");
cursor.SetXPosition(4);
termSm.ProcessString(addTabStop);
expectedStops.push_front(4);
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Add tab to tail of existing list.");
cursor.SetXPosition(30);
termSm.ProcessString(addTabStop);
expectedStops.push_back(30);
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Add tab to middle of existing list.");
cursor.SetXPosition(24);
termSm.ProcessString(addTabStop);
expectedStops.push_back(24);
expectedStops.sort();
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
Log::Comment(L"Add tab that duplicates an item in the existing list.");
cursor.SetXPosition(24);
termSm.ProcessString(addTabStop);
VERIFY_ARE_EQUAL(expectedStops, _GetTabStops());
}
void TerminalBufferTests::TestClearTabStop()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
auto& cursor = termTb.GetCursor();
const auto clearTabStops = L"\033[3g";
const auto clearTabStop = L"\033[0g";
const auto addTabStop = L"\033H";
Log::Comment(L"Start with all tabs cleared.");
{
termSm.ProcessString(clearTabStops);
VERIFY_IS_TRUE(_GetTabStops().empty());
}
Log::Comment(L"Try to clear nonexistent list.");
{
cursor.SetXPosition(0);
termSm.ProcessString(clearTabStop);
VERIFY_IS_TRUE(_GetTabStops().empty(), L"List should remain empty");
}
Log::Comment(L"Allocate 1 list item and clear it.");
{
cursor.SetXPosition(0);
termSm.ProcessString(addTabStop);
termSm.ProcessString(clearTabStop);
VERIFY_IS_TRUE(_GetTabStops().empty());
}
Log::Comment(L"Allocate 1 list item and clear nonexistent.");
{
cursor.SetXPosition(1);
termSm.ProcessString(addTabStop);
Log::Comment(L"Free greater");
cursor.SetXPosition(2);
termSm.ProcessString(clearTabStop);
VERIFY_IS_FALSE(_GetTabStops().empty());
Log::Comment(L"Free less than");
cursor.SetXPosition(0);
termSm.ProcessString(clearTabStop);
VERIFY_IS_FALSE(_GetTabStops().empty());
// clear all tab stops
termSm.ProcessString(clearTabStops);
}
Log::Comment(L"Allocate many (5) list items and clear head.");
{
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, false);
cursor.SetXPosition(inputData.front());
termSm.ProcessString(clearTabStop);
inputData.pop_front();
VERIFY_ARE_EQUAL(inputData, _GetTabStops());
// clear all tab stops
termSm.ProcessString(clearTabStops);
}
Log::Comment(L"Allocate many (5) list items and clear middle.");
{
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, false);
cursor.SetXPosition(*std::next(inputData.begin()));
termSm.ProcessString(clearTabStop);
inputData.erase(std::next(inputData.begin()));
VERIFY_ARE_EQUAL(inputData, _GetTabStops());
// clear all tab stops
termSm.ProcessString(clearTabStops);
}
Log::Comment(L"Allocate many (5) list items and clear tail.");
{
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, false);
cursor.SetXPosition(inputData.back());
termSm.ProcessString(clearTabStop);
inputData.pop_back();
VERIFY_ARE_EQUAL(inputData, _GetTabStops());
// clear all tab stops
termSm.ProcessString(clearTabStops);
}
Log::Comment(L"Allocate many (5) list items and clear nonexistent item.");
{
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, false);
cursor.SetXPosition(0);
termSm.ProcessString(clearTabStop);
VERIFY_ARE_EQUAL(inputData, _GetTabStops());
// clear all tab stops
termSm.ProcessString(clearTabStops);
}
}
void TerminalBufferTests::TestGetForwardTab()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
const auto initialView = term->GetViewport();
auto& cursor = termTb.GetCursor();
const auto nextForwardTab = L"\033[I";
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, true);
const COORD coordScreenBufferSize = initialView.Dimensions();
Log::Comment(L"Find next tab from before front.");
{
cursor.SetXPosition(0);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = inputData.front();
termSm.ProcessString(nextForwardTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor advanced to first tab stop from sample list.");
}
Log::Comment(L"Find next tab from in the middle.");
{
cursor.SetXPosition(6);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = *std::next(inputData.begin(), 3);
termSm.ProcessString(nextForwardTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor advanced to middle tab stop from sample list.");
}
Log::Comment(L"Find next tab from end.");
{
cursor.SetXPosition(30);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = coordScreenBufferSize.X - 1;
termSm.ProcessString(nextForwardTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor advanced to end of screen buffer.");
}
Log::Comment(L"Find next tab from rightmost column.");
{
cursor.SetXPosition(coordScreenBufferSize.X - 1);
COORD coordCursorExpected = cursor.GetPosition();
termSm.ProcessString(nextForwardTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor remains in rightmost column.");
}
}
void TerminalBufferTests::TestGetReverseTab()
{
auto& termTb = *term->_buffer;
auto& termSm = *term->_stateMachine;
auto& cursor = termTb.GetCursor();
const auto nextReverseTab = L"\033[Z";
std::list<short> inputData = { 3, 5, 6, 10, 15, 17 };
_SetTabStops(inputData, true);
Log::Comment(L"Find previous tab from before front.");
{
cursor.SetXPosition(1);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = 0;
termSm.ProcessString(nextReverseTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor adjusted to beginning of the buffer when it started before sample list.");
}
Log::Comment(L"Find previous tab from in the middle.");
{
cursor.SetXPosition(6);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = *std::next(inputData.begin());
termSm.ProcessString(nextReverseTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor adjusted back one tab spot from middle of sample list.");
}
Log::Comment(L"Find next tab from end.");
{
cursor.SetXPosition(30);
COORD coordCursorExpected = cursor.GetPosition();
coordCursorExpected.X = inputData.back();
termSm.ProcessString(nextReverseTab);
COORD const coordCursorResult = cursor.GetPosition();
VERIFY_ARE_EQUAL(coordCursorExpected,
coordCursorResult,
L"Cursor adjusted to last item in the sample list from position beyond end.");
}
}
void TerminalBufferTests::TestCursorNotifications()
{
// Test for GH#11170
// Suppress test exceptions. If they occur in the lambda, they'll just crash
// TAEF, which is annoying.
const WEX::TestExecution::DisableVerifyExceptions disableExceptionsScope;
bool callbackWasCalled = false;
int expectedCallbacks = 0;
auto cb = [&expectedCallbacks, &callbackWasCalled]() mutable {
Log::Comment(L"Callback triggered");
callbackWasCalled = true;
expectedCallbacks--;
VERIFY_IS_GREATER_THAN_OR_EQUAL(expectedCallbacks, 0);
// VERIFY_IS_TRUE(expectedCallbacks >= 0);
};
term->_pfnCursorPositionChanged = cb;
expectedCallbacks = 1;
callbackWasCalled = false;
term->_WriteBuffer(L"Foo");
VERIFY_ARE_EQUAL(0, expectedCallbacks);
VERIFY_IS_TRUE(callbackWasCalled);
expectedCallbacks = 1;
callbackWasCalled = false;
term->_WriteBuffer(L"Foo\r\nBar");
VERIFY_ARE_EQUAL(0, expectedCallbacks);
VERIFY_IS_TRUE(callbackWasCalled);
expectedCallbacks = 2;
callbackWasCalled = false;
term->_WriteBuffer(L"Foo\r\nBar");
term->_WriteBuffer(L"Foo\r\nBar");
VERIFY_ARE_EQUAL(0, expectedCallbacks);
VERIFY_IS_TRUE(callbackWasCalled);
}