]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Add a bunch of Buffer tests :tada:
authorDaniel Imms <daimms@microsoft.com>
Sun, 6 Aug 2017 03:11:26 +0000 (20:11 -0700)
committerDaniel Imms <daimms@microsoft.com>
Sun, 6 Aug 2017 03:11:26 +0000 (20:11 -0700)
src/Buffer.test.ts

index f68baa82ac999c797e75d603098ffbbbc5e2ab94..66cf15c4157fc4cc903c41f07512599029fa69bf 100644 (file)
@@ -5,17 +5,20 @@ import { assert } from 'chai';
 import { ITerminal } from './Interfaces';
 import { Buffer } from './Buffer';
 import { CircularList } from './utils/CircularList';
+import { MockTerminal } from './utils/TestUtils';
+
+const INIT_COLS = 80;
+const INIT_ROWS = 24;
 
 describe('Buffer', () => {
   let terminal: ITerminal;
   let buffer: Buffer;
 
   beforeEach(() => {
-    terminal = <any>{
-      cols: 80,
-      rows: 24,
-      scrollback: 1000
-    };
+    terminal = new MockTerminal();
+    terminal.cols = INIT_COLS;
+    terminal.rows = INIT_ROWS;
+    terminal.scrollback = 1000;
     buffer = new Buffer(terminal);
   });
 
@@ -28,4 +31,114 @@ describe('Buffer', () => {
       assert.equal(buffer.scrollBottom, terminal.rows - 1);
     });
   });
+
+  describe('resize', () => {
+    describe('column size is reduced', () => {
+      it('should not trim the data in the buffer', () => {
+        buffer.fillViewportRows();
+        for (let i = 0; i < INIT_ROWS; i++) {
+          assert.equal(buffer.lines.length, INIT_ROWS);
+          assert.equal(buffer.lines.get(i).length, INIT_COLS);
+        }
+        buffer.resize(INIT_COLS / 2, INIT_ROWS);
+        assert.equal(buffer.lines.length, INIT_ROWS);
+        for (let i = 0; i < INIT_ROWS; i++) {
+          assert.equal(buffer.lines.get(i).length, INIT_COLS);
+        }
+      });
+    });
+
+    describe('column size is increased', () => {
+      it('should add pad columns', () => {
+        buffer.fillViewportRows();
+        assert.equal(buffer.lines.length, INIT_ROWS);
+        for (let i = 0; i < INIT_ROWS; i++) {
+          assert.equal(buffer.lines.get(i).length, INIT_COLS);
+        }
+        buffer.resize(INIT_COLS + 10, INIT_ROWS);
+        assert.equal(buffer.lines.length, INIT_ROWS);
+        for (let i = 0; i < INIT_ROWS; i++) {
+          assert.equal(buffer.lines.get(i).length, INIT_COLS + 10);
+        }
+      });
+    });
+
+    describe('row size reduced', () => {
+      it('should trim blank lines from the end', () => {
+        buffer.fillViewportRows();
+        assert.equal(buffer.lines.length, INIT_ROWS);
+        buffer.resize(INIT_COLS, INIT_ROWS - 10);
+        assert.equal(buffer.lines.length, INIT_ROWS - 10);
+      });
+
+      it('should move the viewport down when it\'s at the end', () => {
+        buffer.fillViewportRows();
+        assert.equal(buffer.lines.length, INIT_ROWS);
+        // Set cursor y to have 5 blank lines below it
+        buffer.y = INIT_ROWS - 5 - 1;
+        buffer.resize(INIT_COLS, INIT_ROWS - 10);
+        // Trim 5 rows
+        assert.equal(buffer.lines.length, INIT_ROWS - 5);
+        // Shift the viewport down 5 rows
+        assert.equal(buffer.ydisp, 5);
+        assert.equal(buffer.ybase, 5);
+      });
+    });
+
+    describe('row size increased', () => {
+      describe('empty buffer', () => {
+        it('should add blank lines to end', () => {
+          buffer.fillViewportRows();
+          assert.equal(buffer.ydisp, 0);
+          assert.equal(buffer.lines.length, INIT_ROWS);
+          buffer.resize(INIT_COLS, INIT_ROWS + 10);
+          assert.equal(buffer.ydisp, 0);
+          assert.equal(buffer.lines.length, INIT_ROWS + 10);
+        });
+      });
+
+      describe('filled buffer', () => {
+        it('should show more of the buffer above', () => {
+          buffer.fillViewportRows();
+          // Create 10 extra blank lines
+          for (let i = 0; i < 10; i++) {
+            buffer.lines.push(terminal.blankLine());
+          }
+          // Set cursor to the bottom of the buffer
+          buffer.y = INIT_ROWS - 1;
+          // Scroll down 10 lines
+          buffer.ybase = 10;
+          buffer.ydisp = 10;
+          assert.equal(buffer.lines.length, INIT_ROWS + 10);
+          buffer.resize(INIT_COLS, INIT_ROWS + 5);
+          // Should be should 5 more lines
+          assert.equal(buffer.ydisp, 5);
+          assert.equal(buffer.ybase, 5);
+          // Should not trim the buffer
+          assert.equal(buffer.lines.length, INIT_ROWS + 10);
+        });
+
+        it('should show more of the buffer below when the viewport is at the top of the buffer', () => {
+          buffer.fillViewportRows();
+          // Create 10 extra blank lines
+          for (let i = 0; i < 10; i++) {
+            buffer.lines.push(terminal.blankLine());
+          }
+          // Set cursor to the bottom of the buffer
+          buffer.y = INIT_ROWS - 1;
+          // Scroll down 10 lines
+          buffer.ybase = 10;
+          buffer.ydisp = 0;
+          assert.equal(buffer.lines.length, INIT_ROWS + 10);
+          buffer.resize(INIT_COLS, INIT_ROWS + 5);
+          // The viewport should remain at the top
+          assert.equal(buffer.ydisp, 0);
+          // The buffer ybase should move up 5 lines
+          assert.equal(buffer.ybase, 5);
+          // Should not trim the buffer
+          assert.equal(buffer.lines.length, INIT_ROWS + 10);
+        });
+      });
+    });
+  });
 });