4 import jsdom = require('jsdom');
5 import { assert } from 'chai';
6 import { ITerminal, ICircularList } from './Interfaces';
7 import { CharMeasure } from './utils/CharMeasure';
8 import { CircularList } from './utils/CircularList';
9 import { SelectionManager } from './SelectionManager';
10 import { SelectionModel } from './SelectionModel';
11 import { BufferSet } from './BufferSet';
13 class TestSelectionManager extends SelectionManager {
16 buffer: ICircularList<[number, string, number][]>,
17 rowContainer: HTMLElement,
18 charMeasure: CharMeasure
20 super(terminal, buffer, rowContainer, charMeasure);
23 public get model(): SelectionModel { return this._model; }
25 public selectLineAt(line: number): void { this._selectLineAt(line); }
26 public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); }
28 // Disable DOM interaction
29 public enable(): void {}
30 public disable(): void {}
31 public refresh(): void {}
34 describe('SelectionManager', () => {
37 let document: Document;
39 let terminal: ITerminal;
40 let bufferLines: ICircularList<[number, string, number][]>;
41 let rowContainer: HTMLElement;
42 let selectionManager: TestSelectionManager;
45 dom = new jsdom.JSDOM('');
47 document = window.document;
48 terminal = <any>{ cols: 80, rows: 2 };
49 terminal.scrollback = 100;
50 terminal.buffers = new BufferSet(terminal);
51 terminal.buffer = terminal.buffers.active;
52 bufferLines = terminal.buffer.lines;
53 selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null);
56 function stringToRow(text: string): [number, string, number][] {
57 let result: [number, string, number][] = [];
58 for (let i = 0; i < text.length; i++) {
59 result.push([0, text.charAt(i), 1]);
64 describe('_selectWordAt', () => {
65 it('should expand selection for normal width chars', () => {
66 bufferLines.push(stringToRow('foo bar'));
67 selectionManager.selectWordAt([0, 0]);
68 assert.equal(selectionManager.selectionText, 'foo');
69 selectionManager.selectWordAt([1, 0]);
70 assert.equal(selectionManager.selectionText, 'foo');
71 selectionManager.selectWordAt([2, 0]);
72 assert.equal(selectionManager.selectionText, 'foo');
73 selectionManager.selectWordAt([3, 0]);
74 assert.equal(selectionManager.selectionText, ' ');
75 selectionManager.selectWordAt([4, 0]);
76 assert.equal(selectionManager.selectionText, 'bar');
77 selectionManager.selectWordAt([5, 0]);
78 assert.equal(selectionManager.selectionText, 'bar');
79 selectionManager.selectWordAt([6, 0]);
80 assert.equal(selectionManager.selectionText, 'bar');
82 it('should expand selection for whitespace', () => {
83 bufferLines.push(stringToRow('a b'));
84 selectionManager.selectWordAt([0, 0]);
85 assert.equal(selectionManager.selectionText, 'a');
86 selectionManager.selectWordAt([1, 0]);
87 assert.equal(selectionManager.selectionText, ' ');
88 selectionManager.selectWordAt([2, 0]);
89 assert.equal(selectionManager.selectionText, ' ');
90 selectionManager.selectWordAt([3, 0]);
91 assert.equal(selectionManager.selectionText, ' ');
92 selectionManager.selectWordAt([4, 0]);
93 assert.equal(selectionManager.selectionText, 'b');
95 it('should expand selection for wide characters', () => {
96 // Wide characters use a special format
114 // Ensure wide characters take up 2 columns
115 selectionManager.selectWordAt([0, 0]);
116 assert.equal(selectionManager.selectionText, '中文');
117 selectionManager.selectWordAt([1, 0]);
118 assert.equal(selectionManager.selectionText, '中文');
119 selectionManager.selectWordAt([2, 0]);
120 assert.equal(selectionManager.selectionText, '中文');
121 selectionManager.selectWordAt([3, 0]);
122 assert.equal(selectionManager.selectionText, '中文');
123 selectionManager.selectWordAt([4, 0]);
124 assert.equal(selectionManager.selectionText, ' ');
125 // Ensure wide characters work when wrapped in normal width characters
126 selectionManager.selectWordAt([5, 0]);
127 assert.equal(selectionManager.selectionText, 'a中文b');
128 selectionManager.selectWordAt([6, 0]);
129 assert.equal(selectionManager.selectionText, 'a中文b');
130 selectionManager.selectWordAt([7, 0]);
131 assert.equal(selectionManager.selectionText, 'a中文b');
132 selectionManager.selectWordAt([8, 0]);
133 assert.equal(selectionManager.selectionText, 'a中文b');
134 selectionManager.selectWordAt([9, 0]);
135 assert.equal(selectionManager.selectionText, 'a中文b');
136 selectionManager.selectWordAt([10, 0]);
137 assert.equal(selectionManager.selectionText, 'a中文b');
138 selectionManager.selectWordAt([11, 0]);
139 assert.equal(selectionManager.selectionText, ' ');
140 // Ensure normal width characters work fine in a line containing wide characters
141 selectionManager.selectWordAt([12, 0]);
142 assert.equal(selectionManager.selectionText, 'foo');
143 selectionManager.selectWordAt([13, 0]);
144 assert.equal(selectionManager.selectionText, 'foo');
145 selectionManager.selectWordAt([14, 0]);
146 assert.equal(selectionManager.selectionText, 'foo');
148 it('should select up to non-path characters that are commonly adjacent to paths', () => {
149 bufferLines.push(stringToRow('(cd)[ef]{gh}\'ij"'));
150 selectionManager.selectWordAt([0, 0]);
151 assert.equal(selectionManager.selectionText, '(cd');
152 selectionManager.selectWordAt([1, 0]);
153 assert.equal(selectionManager.selectionText, 'cd');
154 selectionManager.selectWordAt([2, 0]);
155 assert.equal(selectionManager.selectionText, 'cd');
156 selectionManager.selectWordAt([3, 0]);
157 assert.equal(selectionManager.selectionText, 'cd)');
158 selectionManager.selectWordAt([4, 0]);
159 assert.equal(selectionManager.selectionText, '[ef');
160 selectionManager.selectWordAt([5, 0]);
161 assert.equal(selectionManager.selectionText, 'ef');
162 selectionManager.selectWordAt([6, 0]);
163 assert.equal(selectionManager.selectionText, 'ef');
164 selectionManager.selectWordAt([7, 0]);
165 assert.equal(selectionManager.selectionText, 'ef]');
166 selectionManager.selectWordAt([8, 0]);
167 assert.equal(selectionManager.selectionText, '{gh');
168 selectionManager.selectWordAt([9, 0]);
169 assert.equal(selectionManager.selectionText, 'gh');
170 selectionManager.selectWordAt([10, 0]);
171 assert.equal(selectionManager.selectionText, 'gh');
172 selectionManager.selectWordAt([11, 0]);
173 assert.equal(selectionManager.selectionText, 'gh}');
174 selectionManager.selectWordAt([12, 0]);
175 assert.equal(selectionManager.selectionText, '\'ij');
176 selectionManager.selectWordAt([13, 0]);
177 assert.equal(selectionManager.selectionText, 'ij');
178 selectionManager.selectWordAt([14, 0]);
179 assert.equal(selectionManager.selectionText, 'ij');
180 selectionManager.selectWordAt([15, 0]);
181 assert.equal(selectionManager.selectionText, 'ij"');
185 describe('_selectLineAt', () => {
186 it('should select the entire line', () => {
187 bufferLines.push(stringToRow('foo bar'));
188 selectionManager.selectLineAt(0);
189 assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
190 assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
191 assert.deepEqual(selectionManager.model.finalSelectionEnd, [terminal.cols, 0], 'The actual selection spans the entire column');
195 describe('selectAll', () => {
196 it('should select the entire buffer, beyond the viewport', () => {
197 bufferLines.push(stringToRow('1'));
198 bufferLines.push(stringToRow('2'));
199 bufferLines.push(stringToRow('3'));
200 bufferLines.push(stringToRow('4'));
201 bufferLines.push(stringToRow('5'));
202 selectionManager.selectAll();
203 terminal.buffer.ybase = bufferLines.length - terminal.rows;
204 assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5');
208 describe('hasSelection', () => {
209 it('should return whether there is a selection', () => {
210 selectionManager.model.selectionStart = [0, 0];
211 selectionManager.model.selectionStartLength = 0;
212 assert.equal(selectionManager.hasSelection, false);
213 selectionManager.model.selectionEnd = [0, 0];
214 assert.equal(selectionManager.hasSelection, false);
215 selectionManager.model.selectionEnd = [1, 0];
216 assert.equal(selectionManager.hasSelection, true);
217 selectionManager.model.selectionEnd = [0, 1];
218 assert.equal(selectionManager.hasSelection, true);
219 selectionManager.model.selectionEnd = [1, 1];
220 assert.equal(selectionManager.hasSelection, true);