]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Buffer.test.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / Buffer.test.ts
CommitLineData
cb5a452c
PK
1/**
2 * @license MIT
3 */
4import { assert } from 'chai';
5import { ITerminal } from './Interfaces';
6import { Buffer } from './Buffer';
7import { CircularList } from './utils/CircularList';
13619d42
DI
8import { MockTerminal } from './utils/TestUtils';
9
10const INIT_COLS = 80;
11const INIT_ROWS = 24;
cb5a452c
PK
12
13describe('Buffer', () => {
14 let terminal: ITerminal;
15 let buffer: Buffer;
16
17 beforeEach(() => {
13619d42
DI
18 terminal = new MockTerminal();
19 terminal.cols = INIT_COLS;
20 terminal.rows = INIT_ROWS;
21 terminal.scrollback = 1000;
cb5a452c
PK
22 buffer = new Buffer(terminal);
23 });
24
25 describe('constructor', () => {
26 it('should create a CircularList with max length equal to scrollback, for its lines', () => {
27 assert.instanceOf(buffer.lines, CircularList);
28 assert.equal(buffer.lines.maxLength, terminal.scrollback);
29 });
30 it('should set the Buffer\'s scrollBottom value equal to the terminal\'s rows -1', () => {
31 assert.equal(buffer.scrollBottom, terminal.rows - 1);
32 });
33 });
13619d42 34
d4600384
DI
35 describe('fillViewportRows', () => {
36 it('should fill the buffer with blank lines based on the size of the viewport', () => {
37 const blankLineChar = terminal.blankLine()[0];
38 buffer.fillViewportRows();
39 assert.equal(buffer.lines.length, INIT_ROWS);
40 for (let y = 0; y < INIT_ROWS; y++) {
41 assert.equal(buffer.lines.get(y).length, INIT_COLS);
42 for (let x = 0; x < INIT_COLS; x++) {
43 assert.deepEqual(buffer.lines.get(y)[x], blankLineChar);
44 }
45 }
46 });
47 });
48
13619d42
DI
49 describe('resize', () => {
50 describe('column size is reduced', () => {
51 it('should not trim the data in the buffer', () => {
52 buffer.fillViewportRows();
13619d42
DI
53 buffer.resize(INIT_COLS / 2, INIT_ROWS);
54 assert.equal(buffer.lines.length, INIT_ROWS);
55 for (let i = 0; i < INIT_ROWS; i++) {
56 assert.equal(buffer.lines.get(i).length, INIT_COLS);
57 }
58 });
59 });
60
61 describe('column size is increased', () => {
62 it('should add pad columns', () => {
63 buffer.fillViewportRows();
13619d42
DI
64 buffer.resize(INIT_COLS + 10, INIT_ROWS);
65 assert.equal(buffer.lines.length, INIT_ROWS);
66 for (let i = 0; i < INIT_ROWS; i++) {
67 assert.equal(buffer.lines.get(i).length, INIT_COLS + 10);
68 }
69 });
70 });
71
72 describe('row size reduced', () => {
73 it('should trim blank lines from the end', () => {
74 buffer.fillViewportRows();
13619d42
DI
75 buffer.resize(INIT_COLS, INIT_ROWS - 10);
76 assert.equal(buffer.lines.length, INIT_ROWS - 10);
77 });
78
79 it('should move the viewport down when it\'s at the end', () => {
80 buffer.fillViewportRows();
13619d42
DI
81 // Set cursor y to have 5 blank lines below it
82 buffer.y = INIT_ROWS - 5 - 1;
83 buffer.resize(INIT_COLS, INIT_ROWS - 10);
84 // Trim 5 rows
85 assert.equal(buffer.lines.length, INIT_ROWS - 5);
86 // Shift the viewport down 5 rows
87 assert.equal(buffer.ydisp, 5);
88 assert.equal(buffer.ybase, 5);
89 });
90 });
91
92 describe('row size increased', () => {
93 describe('empty buffer', () => {
94 it('should add blank lines to end', () => {
95 buffer.fillViewportRows();
96 assert.equal(buffer.ydisp, 0);
13619d42
DI
97 buffer.resize(INIT_COLS, INIT_ROWS + 10);
98 assert.equal(buffer.ydisp, 0);
99 assert.equal(buffer.lines.length, INIT_ROWS + 10);
100 });
101 });
102
103 describe('filled buffer', () => {
104 it('should show more of the buffer above', () => {
105 buffer.fillViewportRows();
106 // Create 10 extra blank lines
107 for (let i = 0; i < 10; i++) {
108 buffer.lines.push(terminal.blankLine());
109 }
110 // Set cursor to the bottom of the buffer
111 buffer.y = INIT_ROWS - 1;
112 // Scroll down 10 lines
113 buffer.ybase = 10;
114 buffer.ydisp = 10;
115 assert.equal(buffer.lines.length, INIT_ROWS + 10);
116 buffer.resize(INIT_COLS, INIT_ROWS + 5);
117 // Should be should 5 more lines
118 assert.equal(buffer.ydisp, 5);
119 assert.equal(buffer.ybase, 5);
120 // Should not trim the buffer
121 assert.equal(buffer.lines.length, INIT_ROWS + 10);
122 });
123
124 it('should show more of the buffer below when the viewport is at the top of the buffer', () => {
125 buffer.fillViewportRows();
126 // Create 10 extra blank lines
127 for (let i = 0; i < 10; i++) {
128 buffer.lines.push(terminal.blankLine());
129 }
130 // Set cursor to the bottom of the buffer
131 buffer.y = INIT_ROWS - 1;
132 // Scroll down 10 lines
133 buffer.ybase = 10;
134 buffer.ydisp = 0;
135 assert.equal(buffer.lines.length, INIT_ROWS + 10);
136 buffer.resize(INIT_COLS, INIT_ROWS + 5);
137 // The viewport should remain at the top
138 assert.equal(buffer.ydisp, 0);
139 // The buffer ybase should move up 5 lines
140 assert.equal(buffer.ybase, 5);
141 // Should not trim the buffer
142 assert.equal(buffer.lines.length, INIT_ROWS + 10);
143 });
144 });
145 });
bb526aaa
DI
146
147 describe('row and column increased', () => {
148 it('should resize properly', () => {
149 buffer.fillViewportRows();
150 buffer.resize(INIT_COLS + 5, INIT_ROWS + 5);
151 assert.equal(buffer.lines.length, INIT_ROWS + 5);
152 for (let i = 0; i < INIT_ROWS + 5; i++) {
153 assert.equal(buffer.lines.get(i).length, INIT_COLS + 5);
154 }
155 });
156 });
13619d42 157 });
cb5a452c 158});