]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionManager.test.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / SelectionManager.test.ts
index eb9322b6c0168bcf993d505f4e5825ca5de44999..7beafd977fda939f39ca1d594da15aa011b10667 100644 (file)
@@ -3,16 +3,18 @@
  */
 import jsdom = require('jsdom');
 import { assert } from 'chai';
-import { ITerminal } from './Interfaces';
+import { ITerminal, ICircularList } from './Interfaces';
 import { CharMeasure } from './utils/CharMeasure';
 import { CircularList } from './utils/CircularList';
 import { SelectionManager } from './SelectionManager';
 import { SelectionModel } from './SelectionModel';
+import { BufferSet } from './BufferSet';
+import { MockTerminal } from './utils/TestUtils';
 
 class TestSelectionManager extends SelectionManager {
   constructor(
     terminal: ITerminal,
-    buffer: CircularList<any>,
+    buffer: ICircularList<[number, string, number][]>,
     rowContainer: HTMLElement,
     charMeasure: CharMeasure
   ) {
@@ -31,23 +33,28 @@ class TestSelectionManager extends SelectionManager {
 }
 
 describe('SelectionManager', () => {
+  let dom: jsdom.JSDOM;
   let window: Window;
   let document: Document;
 
   let terminal: ITerminal;
-  let buffer: CircularList<any>;
+  let bufferLines: ICircularList<[number, string, number][]>;
   let rowContainer: HTMLElement;
   let selectionManager: TestSelectionManager;
 
-  beforeEach(done => {
-    jsdom.env('', (err, w) => {
-      window = w;
-      document = window.document;
-      buffer = new CircularList<any>(100);
-      terminal = <any>{ cols: 80, rows: 2 };
-      selectionManager = new TestSelectionManager(terminal, buffer, rowContainer, null);
-      done();
-    });
+  beforeEach(() => {
+    dom = new jsdom.JSDOM('');
+    window = dom.window;
+    document = window.document;
+    rowContainer = document.createElement('div');
+    terminal = new MockTerminal();
+    terminal.cols = 80;
+    terminal.rows = 2;
+    terminal.scrollback = 100;
+    terminal.buffers = new BufferSet(terminal);
+    terminal.buffer = terminal.buffers.active;
+    bufferLines = terminal.buffer.lines;
+    selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null);
   });
 
   function stringToRow(text: string): [number, string, number][] {
@@ -60,7 +67,7 @@ describe('SelectionManager', () => {
 
   describe('_selectWordAt', () => {
     it('should expand selection for normal width chars', () => {
-      buffer.push(stringToRow('foo bar'));
+      bufferLines.set(0, stringToRow('foo bar'));
       selectionManager.selectWordAt([0, 0]);
       assert.equal(selectionManager.selectionText, 'foo');
       selectionManager.selectWordAt([1, 0]);
@@ -77,7 +84,7 @@ describe('SelectionManager', () => {
       assert.equal(selectionManager.selectionText, 'bar');
     });
     it('should expand selection for whitespace', () => {
-      buffer.push(stringToRow('a   b'));
+      bufferLines.set(0, stringToRow('a   b'));
       selectionManager.selectWordAt([0, 0]);
       assert.equal(selectionManager.selectionText, 'a');
       selectionManager.selectWordAt([1, 0]);
@@ -91,7 +98,7 @@ describe('SelectionManager', () => {
     });
     it('should expand selection for wide characters', () => {
       // Wide characters use a special format
-      buffer.push([
+      bufferLines.set(0, [
         [null, '中', 2],
         [null, '', 0],
         [null, '文', 2],
@@ -143,53 +150,45 @@ describe('SelectionManager', () => {
       assert.equal(selectionManager.selectionText, 'foo');
     });
     it('should select up to non-path characters that are commonly adjacent to paths', () => {
-      buffer.push(stringToRow(':ab:(cd)[ef]{gh}\'ij"'));
+      bufferLines.set(0, stringToRow('(cd)[ef]{gh}\'ij"'));
       selectionManager.selectWordAt([0, 0]);
-      assert.equal(selectionManager.selectionText, ':ab');
-      selectionManager.selectWordAt([1, 0]);
-      assert.equal(selectionManager.selectionText, 'ab');
-      selectionManager.selectWordAt([2, 0]);
-      assert.equal(selectionManager.selectionText, 'ab');
-      selectionManager.selectWordAt([3, 0]);
-      assert.equal(selectionManager.selectionText, 'ab:');
-      selectionManager.selectWordAt([4, 0]);
       assert.equal(selectionManager.selectionText, '(cd');
-      selectionManager.selectWordAt([5, 0]);
+      selectionManager.selectWordAt([1, 0]);
       assert.equal(selectionManager.selectionText, 'cd');
-      selectionManager.selectWordAt([6, 0]);
+      selectionManager.selectWordAt([2, 0]);
       assert.equal(selectionManager.selectionText, 'cd');
-      selectionManager.selectWordAt([7, 0]);
+      selectionManager.selectWordAt([3, 0]);
       assert.equal(selectionManager.selectionText, 'cd)');
-      selectionManager.selectWordAt([8, 0]);
+      selectionManager.selectWordAt([4, 0]);
       assert.equal(selectionManager.selectionText, '[ef');
-      selectionManager.selectWordAt([9, 0]);
+      selectionManager.selectWordAt([5, 0]);
       assert.equal(selectionManager.selectionText, 'ef');
-      selectionManager.selectWordAt([10, 0]);
+      selectionManager.selectWordAt([6, 0]);
       assert.equal(selectionManager.selectionText, 'ef');
-      selectionManager.selectWordAt([11, 0]);
+      selectionManager.selectWordAt([7, 0]);
       assert.equal(selectionManager.selectionText, 'ef]');
-      selectionManager.selectWordAt([12, 0]);
+      selectionManager.selectWordAt([8, 0]);
       assert.equal(selectionManager.selectionText, '{gh');
-      selectionManager.selectWordAt([13, 0]);
+      selectionManager.selectWordAt([9, 0]);
       assert.equal(selectionManager.selectionText, 'gh');
-      selectionManager.selectWordAt([14, 0]);
+      selectionManager.selectWordAt([10, 0]);
       assert.equal(selectionManager.selectionText, 'gh');
-      selectionManager.selectWordAt([15, 0]);
+      selectionManager.selectWordAt([11, 0]);
       assert.equal(selectionManager.selectionText, 'gh}');
-      selectionManager.selectWordAt([16, 0]);
+      selectionManager.selectWordAt([12, 0]);
       assert.equal(selectionManager.selectionText, '\'ij');
-      selectionManager.selectWordAt([17, 0]);
+      selectionManager.selectWordAt([13, 0]);
       assert.equal(selectionManager.selectionText, 'ij');
-      selectionManager.selectWordAt([18, 0]);
+      selectionManager.selectWordAt([14, 0]);
       assert.equal(selectionManager.selectionText, 'ij');
-      selectionManager.selectWordAt([19, 0]);
+      selectionManager.selectWordAt([15, 0]);
       assert.equal(selectionManager.selectionText, 'ij"');
     });
   });
 
   describe('_selectLineAt', () => {
     it('should select the entire line', () => {
-      buffer.push(stringToRow('foo bar'));
+      bufferLines.set(0, stringToRow('foo bar'));
       selectionManager.selectLineAt(0);
       assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
       assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
@@ -199,14 +198,31 @@ describe('SelectionManager', () => {
 
   describe('selectAll', () => {
     it('should select the entire buffer, beyond the viewport', () => {
-      buffer.push(stringToRow('1'));
-      buffer.push(stringToRow('2'));
-      buffer.push(stringToRow('3'));
-      buffer.push(stringToRow('4'));
-      buffer.push(stringToRow('5'));
+      bufferLines.length = 5;
+      bufferLines.set(0, stringToRow('1'));
+      bufferLines.set(1, stringToRow('2'));
+      bufferLines.set(2, stringToRow('3'));
+      bufferLines.set(3, stringToRow('4'));
+      bufferLines.set(4, stringToRow('5'));
       selectionManager.selectAll();
-      terminal.ybase = buffer.length - terminal.rows;
+      terminal.buffer.ybase = bufferLines.length - terminal.rows;
       assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5');
     });
   });
+
+  describe('hasSelection', () => {
+    it('should return whether there is a selection', () => {
+      selectionManager.model.selectionStart = [0, 0];
+      selectionManager.model.selectionStartLength = 0;
+      assert.equal(selectionManager.hasSelection, false);
+      selectionManager.model.selectionEnd = [0, 0];
+      assert.equal(selectionManager.hasSelection, false);
+      selectionManager.model.selectionEnd = [1, 0];
+      assert.equal(selectionManager.hasSelection, true);
+      selectionManager.model.selectionEnd = [0, 1];
+      assert.equal(selectionManager.hasSelection, true);
+      selectionManager.model.selectionEnd = [1, 1];
+      assert.equal(selectionManager.hasSelection, true);
+    });
+  });
 });