]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionManager.test.ts
Create `terminal.buffer` convenience attribute
[mirror_xterm.js.git] / src / SelectionManager.test.ts
index e35a0576ff76f2d3e66b4f8610fc6b7f747fb0cf..71505a294be64c4b3686d74fefabd9232f0cecc5 100644 (file)
@@ -7,6 +7,7 @@ import { ITerminal } from './Interfaces';
 import { CharMeasure } from './utils/CharMeasure';
 import { CircularList } from './utils/CircularList';
 import { SelectionManager } from './SelectionManager';
+import { SelectionModel } from './SelectionModel';
 
 class TestSelectionManager extends SelectionManager {
   constructor(
@@ -18,6 +19,9 @@ class TestSelectionManager extends SelectionManager {
     super(terminal, buffer, rowContainer, charMeasure);
   }
 
+  public get model(): SelectionModel { return this._model; }
+
+  public selectLineAt(line: number): void { this._selectLineAt(line); }
   public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); }
 
   // Disable DOM interaction
@@ -27,21 +31,22 @@ class TestSelectionManager extends SelectionManager {
 }
 
 describe('SelectionManager', () => {
+  let dom: jsdom.JSDOM;
   let window: Window;
   let document: Document;
 
+  let terminal: ITerminal;
   let buffer: CircularList<any>;
   let rowContainer: HTMLElement;
   let selectionManager: TestSelectionManager;
 
-  beforeEach(done => {
-    jsdom.env('', (err, w) => {
-      window = w;
-      document = window.document;
-      buffer = new CircularList<any>(100);
-      selectionManager = new TestSelectionManager(null, buffer, rowContainer, null);
-      done();
-    });
+  beforeEach(() => {
+    dom = new jsdom.JSDOM('');
+    window = dom.window;
+    document = window.document;
+    buffer = new CircularList<any>(100);
+    terminal = <any>{ cols: 80, rows: 2 };
+    selectionManager = new TestSelectionManager(terminal, buffer, rowContainer, null);
   });
 
   function stringToRow(text: string): [number, string, number][] {
@@ -136,5 +141,79 @@ describe('SelectionManager', () => {
       selectionManager.selectWordAt([14, 0]);
       assert.equal(selectionManager.selectionText, 'foo');
     });
+    it('should select up to non-path characters that are commonly adjacent to paths', () => {
+      buffer.push(stringToRow('(cd)[ef]{gh}\'ij"'));
+      selectionManager.selectWordAt([0, 0]);
+      assert.equal(selectionManager.selectionText, '(cd');
+      selectionManager.selectWordAt([1, 0]);
+      assert.equal(selectionManager.selectionText, 'cd');
+      selectionManager.selectWordAt([2, 0]);
+      assert.equal(selectionManager.selectionText, 'cd');
+      selectionManager.selectWordAt([3, 0]);
+      assert.equal(selectionManager.selectionText, 'cd)');
+      selectionManager.selectWordAt([4, 0]);
+      assert.equal(selectionManager.selectionText, '[ef');
+      selectionManager.selectWordAt([5, 0]);
+      assert.equal(selectionManager.selectionText, 'ef');
+      selectionManager.selectWordAt([6, 0]);
+      assert.equal(selectionManager.selectionText, 'ef');
+      selectionManager.selectWordAt([7, 0]);
+      assert.equal(selectionManager.selectionText, 'ef]');
+      selectionManager.selectWordAt([8, 0]);
+      assert.equal(selectionManager.selectionText, '{gh');
+      selectionManager.selectWordAt([9, 0]);
+      assert.equal(selectionManager.selectionText, 'gh');
+      selectionManager.selectWordAt([10, 0]);
+      assert.equal(selectionManager.selectionText, 'gh');
+      selectionManager.selectWordAt([11, 0]);
+      assert.equal(selectionManager.selectionText, 'gh}');
+      selectionManager.selectWordAt([12, 0]);
+      assert.equal(selectionManager.selectionText, '\'ij');
+      selectionManager.selectWordAt([13, 0]);
+      assert.equal(selectionManager.selectionText, 'ij');
+      selectionManager.selectWordAt([14, 0]);
+      assert.equal(selectionManager.selectionText, 'ij');
+      selectionManager.selectWordAt([15, 0]);
+      assert.equal(selectionManager.selectionText, 'ij"');
+    });
+  });
+
+  describe('_selectLineAt', () => {
+    it('should select the entire line', () => {
+      buffer.push(stringToRow('foo bar'));
+      selectionManager.selectLineAt(0);
+      assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
+      assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
+      assert.deepEqual(selectionManager.model.finalSelectionEnd, [terminal.cols, 0], 'The actual selection spans the entire column');
+    });
+  });
+
+  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'));
+      selectionManager.selectAll();
+      terminal.ybase = buffer.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);
+    });
   });
 });