]>
Commit | Line | Data |
---|---|---|
d3865ad2 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
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'; | |
71477874 | 10 | import { SelectionModel } from './SelectionModel'; |
de82bd00 | 11 | import {BufferSet} from './BufferSet'; |
d3865ad2 DI |
12 | |
13 | class TestSelectionManager extends SelectionManager { | |
14 | constructor( | |
15 | terminal: ITerminal, | |
16 | buffer: CircularList<any>, | |
17 | rowContainer: HTMLElement, | |
18 | charMeasure: CharMeasure | |
19 | ) { | |
20 | super(terminal, buffer, rowContainer, charMeasure); | |
21 | } | |
22 | ||
fd91c5e1 DI |
23 | public get model(): SelectionModel { return this._model; } |
24 | ||
25 | public selectLineAt(line: number): void { this._selectLineAt(line); } | |
d3865ad2 DI |
26 | public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); } |
27 | ||
28 | // Disable DOM interaction | |
29 | public enable(): void {} | |
30 | public disable(): void {} | |
31 | public refresh(): void {} | |
32 | } | |
33 | ||
34 | describe('SelectionManager', () => { | |
0ea2e9cf | 35 | let dom: jsdom.JSDOM; |
d3865ad2 DI |
36 | let window: Window; |
37 | let document: Document; | |
38 | ||
fd91c5e1 | 39 | let terminal: ITerminal; |
83efc9af | 40 | let bufferLines: CircularList<any>; |
d3865ad2 DI |
41 | let rowContainer: HTMLElement; |
42 | let selectionManager: TestSelectionManager; | |
43 | ||
0ea2e9cf PK |
44 | beforeEach(() => { |
45 | dom = new jsdom.JSDOM(''); | |
46 | window = dom.window; | |
28062884 | 47 | document = window.document; |
0ea2e9cf | 48 | terminal = <any>{ cols: 80, rows: 2 }; |
de82bd00 OA |
49 | terminal.scrollback = 100; |
50 | terminal.buffers = new BufferSet(terminal); | |
51 | terminal.buffer = terminal.buffers.active; | |
28062884 | 52 | bufferLines = terminal.buffer.lines; |
83efc9af | 53 | selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null); |
d3865ad2 DI |
54 | }); |
55 | ||
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]); | |
60 | } | |
61 | return result; | |
62 | } | |
63 | ||
64 | describe('_selectWordAt', () => { | |
65 | it('should expand selection for normal width chars', () => { | |
83efc9af | 66 | bufferLines.push(stringToRow('foo bar')); |
d3865ad2 DI |
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'); | |
81 | }); | |
82 | it('should expand selection for whitespace', () => { | |
83efc9af | 83 | bufferLines.push(stringToRow('a b')); |
d3865ad2 DI |
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'); | |
94 | }); | |
95 | it('should expand selection for wide characters', () => { | |
96 | // Wide characters use a special format | |
83efc9af | 97 | bufferLines.push([ |
d3865ad2 DI |
98 | [null, '中', 2], |
99 | [null, '', 0], | |
100 | [null, '文', 2], | |
101 | [null, '', 0], | |
102 | [null, ' ', 1], | |
103 | [null, 'a', 1], | |
104 | [null, '中', 2], | |
105 | [null, '', 0], | |
106 | [null, '文', 2], | |
107 | [null, '', 0], | |
108 | [null, 'b', 1], | |
109 | [null, ' ', 1], | |
110 | [null, 'f', 1], | |
111 | [null, 'o', 1], | |
112 | [null, 'o', 1] | |
113 | ]); | |
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'); | |
147 | }); | |
7f2e94c3 | 148 | it('should select up to non-path characters that are commonly adjacent to paths', () => { |
83efc9af | 149 | bufferLines.push(stringToRow('(cd)[ef]{gh}\'ij"')); |
7f2e94c3 | 150 | selectionManager.selectWordAt([0, 0]); |
7f2e94c3 | 151 | assert.equal(selectionManager.selectionText, '(cd'); |
7463166a | 152 | selectionManager.selectWordAt([1, 0]); |
7f2e94c3 | 153 | assert.equal(selectionManager.selectionText, 'cd'); |
7463166a | 154 | selectionManager.selectWordAt([2, 0]); |
7f2e94c3 | 155 | assert.equal(selectionManager.selectionText, 'cd'); |
7463166a | 156 | selectionManager.selectWordAt([3, 0]); |
7f2e94c3 | 157 | assert.equal(selectionManager.selectionText, 'cd)'); |
7463166a | 158 | selectionManager.selectWordAt([4, 0]); |
7f2e94c3 | 159 | assert.equal(selectionManager.selectionText, '[ef'); |
7463166a | 160 | selectionManager.selectWordAt([5, 0]); |
7f2e94c3 | 161 | assert.equal(selectionManager.selectionText, 'ef'); |
7463166a | 162 | selectionManager.selectWordAt([6, 0]); |
7f2e94c3 | 163 | assert.equal(selectionManager.selectionText, 'ef'); |
7463166a | 164 | selectionManager.selectWordAt([7, 0]); |
7f2e94c3 | 165 | assert.equal(selectionManager.selectionText, 'ef]'); |
7463166a | 166 | selectionManager.selectWordAt([8, 0]); |
7f2e94c3 | 167 | assert.equal(selectionManager.selectionText, '{gh'); |
7463166a | 168 | selectionManager.selectWordAt([9, 0]); |
7f2e94c3 | 169 | assert.equal(selectionManager.selectionText, 'gh'); |
7463166a | 170 | selectionManager.selectWordAt([10, 0]); |
7f2e94c3 | 171 | assert.equal(selectionManager.selectionText, 'gh'); |
7463166a | 172 | selectionManager.selectWordAt([11, 0]); |
7f2e94c3 | 173 | assert.equal(selectionManager.selectionText, 'gh}'); |
7463166a | 174 | selectionManager.selectWordAt([12, 0]); |
07882c42 | 175 | assert.equal(selectionManager.selectionText, '\'ij'); |
7463166a | 176 | selectionManager.selectWordAt([13, 0]); |
07882c42 | 177 | assert.equal(selectionManager.selectionText, 'ij'); |
7463166a | 178 | selectionManager.selectWordAt([14, 0]); |
07882c42 | 179 | assert.equal(selectionManager.selectionText, 'ij'); |
7463166a | 180 | selectionManager.selectWordAt([15, 0]); |
07882c42 | 181 | assert.equal(selectionManager.selectionText, 'ij"'); |
7f2e94c3 | 182 | }); |
d3865ad2 | 183 | }); |
fd91c5e1 DI |
184 | |
185 | describe('_selectLineAt', () => { | |
186 | it('should select the entire line', () => { | |
83efc9af | 187 | bufferLines.push(stringToRow('foo bar')); |
fd91c5e1 DI |
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'); | |
192 | }); | |
193 | }); | |
2b243182 DI |
194 | |
195 | describe('selectAll', () => { | |
196 | it('should select the entire buffer, beyond the viewport', () => { | |
83efc9af PK |
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')); | |
2b243182 | 202 | selectionManager.selectAll(); |
83efc9af | 203 | terminal.buffer.ybase = bufferLines.length - terminal.rows; |
2b243182 DI |
204 | assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5'); |
205 | }); | |
206 | }); | |
7b469407 DI |
207 | |
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); | |
221 | }); | |
222 | }); | |
d3865ad2 | 223 | }); |