4 import jsdom = require('jsdom');
5 import { assert } from 'chai';
6 import { ITerminal } 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: CircularList<any>,
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 buffer: CircularList<any>;
41 let rowContainer: HTMLElement;
42 let selectionManager: TestSelectionManager;
46 dom = new jsdom.JSDOM('');
48 document = window.document
49 terminal = <any>{ cols: 80, rows: 2 };
50 terminal.scrollback = 100;
51 terminal.buffers = new BufferSet(terminal);
52 terminal.buffer = terminal.buffers.active;
53 selectionManager = new TestSelectionManager(terminal, buffer, rowContainer, null);
56 jsdom.env('', (err, w) => {
58 document = window.document;
59 buffer = new CircularList<any>(100);
60 terminal = <any>{ cols: 80, rows: 2 };
61 terminal.scrollback = 10;
62 terminal.buffers = new BufferSet(terminal);
63 terminal.buffer = terminal.buffers.active;
65 selectionManager = new TestSelectionManager(terminal, buffer, rowContainer, null);
68 >>>>>>> Fix some tests and docs, little code fix up.
71 function stringToRow(text: string): [number, string, number][] {
72 let result: [number, string, number][] = [];
73 for (let i = 0; i < text.length; i++) {
74 result.push([0, text.charAt(i), 1]);
79 describe('_selectWordAt', () => {
80 it('should expand selection for normal width chars', () => {
81 buffer.push(stringToRow('foo bar'));
82 selectionManager.selectWordAt([0, 0]);
83 assert.equal(selectionManager.selectionText, 'foo');
84 selectionManager.selectWordAt([1, 0]);
85 assert.equal(selectionManager.selectionText, 'foo');
86 selectionManager.selectWordAt([2, 0]);
87 assert.equal(selectionManager.selectionText, 'foo');
88 selectionManager.selectWordAt([3, 0]);
89 assert.equal(selectionManager.selectionText, ' ');
90 selectionManager.selectWordAt([4, 0]);
91 assert.equal(selectionManager.selectionText, 'bar');
92 selectionManager.selectWordAt([5, 0]);
93 assert.equal(selectionManager.selectionText, 'bar');
94 selectionManager.selectWordAt([6, 0]);
95 assert.equal(selectionManager.selectionText, 'bar');
97 it('should expand selection for whitespace', () => {
98 buffer.push(stringToRow('a b'));
99 selectionManager.selectWordAt([0, 0]);
100 assert.equal(selectionManager.selectionText, 'a');
101 selectionManager.selectWordAt([1, 0]);
102 assert.equal(selectionManager.selectionText, ' ');
103 selectionManager.selectWordAt([2, 0]);
104 assert.equal(selectionManager.selectionText, ' ');
105 selectionManager.selectWordAt([3, 0]);
106 assert.equal(selectionManager.selectionText, ' ');
107 selectionManager.selectWordAt([4, 0]);
108 assert.equal(selectionManager.selectionText, 'b');
110 it('should expand selection for wide characters', () => {
111 // Wide characters use a special format
129 // Ensure wide characters take up 2 columns
130 selectionManager.selectWordAt([0, 0]);
131 assert.equal(selectionManager.selectionText, '中文');
132 selectionManager.selectWordAt([1, 0]);
133 assert.equal(selectionManager.selectionText, '中文');
134 selectionManager.selectWordAt([2, 0]);
135 assert.equal(selectionManager.selectionText, '中文');
136 selectionManager.selectWordAt([3, 0]);
137 assert.equal(selectionManager.selectionText, '中文');
138 selectionManager.selectWordAt([4, 0]);
139 assert.equal(selectionManager.selectionText, ' ');
140 // Ensure wide characters work when wrapped in normal width characters
141 selectionManager.selectWordAt([5, 0]);
142 assert.equal(selectionManager.selectionText, 'a中文b');
143 selectionManager.selectWordAt([6, 0]);
144 assert.equal(selectionManager.selectionText, 'a中文b');
145 selectionManager.selectWordAt([7, 0]);
146 assert.equal(selectionManager.selectionText, 'a中文b');
147 selectionManager.selectWordAt([8, 0]);
148 assert.equal(selectionManager.selectionText, 'a中文b');
149 selectionManager.selectWordAt([9, 0]);
150 assert.equal(selectionManager.selectionText, 'a中文b');
151 selectionManager.selectWordAt([10, 0]);
152 assert.equal(selectionManager.selectionText, 'a中文b');
153 selectionManager.selectWordAt([11, 0]);
154 assert.equal(selectionManager.selectionText, ' ');
155 // Ensure normal width characters work fine in a line containing wide characters
156 selectionManager.selectWordAt([12, 0]);
157 assert.equal(selectionManager.selectionText, 'foo');
158 selectionManager.selectWordAt([13, 0]);
159 assert.equal(selectionManager.selectionText, 'foo');
160 selectionManager.selectWordAt([14, 0]);
161 assert.equal(selectionManager.selectionText, 'foo');
163 it('should select up to non-path characters that are commonly adjacent to paths', () => {
164 buffer.push(stringToRow('(cd)[ef]{gh}\'ij"'));
165 selectionManager.selectWordAt([0, 0]);
166 assert.equal(selectionManager.selectionText, '(cd');
167 selectionManager.selectWordAt([1, 0]);
168 assert.equal(selectionManager.selectionText, 'cd');
169 selectionManager.selectWordAt([2, 0]);
170 assert.equal(selectionManager.selectionText, 'cd');
171 selectionManager.selectWordAt([3, 0]);
172 assert.equal(selectionManager.selectionText, 'cd)');
173 selectionManager.selectWordAt([4, 0]);
174 assert.equal(selectionManager.selectionText, '[ef');
175 selectionManager.selectWordAt([5, 0]);
176 assert.equal(selectionManager.selectionText, 'ef');
177 selectionManager.selectWordAt([6, 0]);
178 assert.equal(selectionManager.selectionText, 'ef');
179 selectionManager.selectWordAt([7, 0]);
180 assert.equal(selectionManager.selectionText, 'ef]');
181 selectionManager.selectWordAt([8, 0]);
182 assert.equal(selectionManager.selectionText, '{gh');
183 selectionManager.selectWordAt([9, 0]);
184 assert.equal(selectionManager.selectionText, 'gh');
185 selectionManager.selectWordAt([10, 0]);
186 assert.equal(selectionManager.selectionText, 'gh');
187 selectionManager.selectWordAt([11, 0]);
188 assert.equal(selectionManager.selectionText, 'gh}');
189 selectionManager.selectWordAt([12, 0]);
190 assert.equal(selectionManager.selectionText, '\'ij');
191 selectionManager.selectWordAt([13, 0]);
192 assert.equal(selectionManager.selectionText, 'ij');
193 selectionManager.selectWordAt([14, 0]);
194 assert.equal(selectionManager.selectionText, 'ij');
195 selectionManager.selectWordAt([15, 0]);
196 assert.equal(selectionManager.selectionText, 'ij"');
200 describe('_selectLineAt', () => {
201 it('should select the entire line', () => {
202 buffer.push(stringToRow('foo bar'));
203 selectionManager.selectLineAt(0);
204 assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
205 assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
206 assert.deepEqual(selectionManager.model.finalSelectionEnd, [terminal.cols, 0], 'The actual selection spans the entire column');
210 describe('selectAll', () => {
211 it('should select the entire buffer, beyond the viewport', () => {
212 buffer.push(stringToRow('1'));
213 buffer.push(stringToRow('2'));
214 buffer.push(stringToRow('3'));
215 buffer.push(stringToRow('4'));
216 buffer.push(stringToRow('5'));
217 selectionManager.selectAll();
218 terminal.buffer.ybase = buffer.length - terminal.rows;
219 assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5');
223 describe('hasSelection', () => {
224 it('should return whether there is a selection', () => {
225 selectionManager.model.selectionStart = [0, 0];
226 selectionManager.model.selectionStartLength = 0;
227 assert.equal(selectionManager.hasSelection, false);
228 selectionManager.model.selectionEnd = [0, 0];
229 assert.equal(selectionManager.hasSelection, false);
230 selectionManager.model.selectionEnd = [1, 0];
231 assert.equal(selectionManager.hasSelection, true);
232 selectionManager.model.selectionEnd = [0, 1];
233 assert.equal(selectionManager.hasSelection, true);
234 selectionManager.model.selectionEnd = [1, 1];
235 assert.equal(selectionManager.hasSelection, true);