]>
Commit | Line | Data |
---|---|---|
d3865ad2 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | import jsdom = require('jsdom'); | |
5 | import { assert } from 'chai'; | |
59f75555 | 6 | import { ITerminal, ICircularList } from './Interfaces'; |
d3865ad2 DI |
7 | import { CharMeasure } from './utils/CharMeasure'; |
8 | import { CircularList } from './utils/CircularList'; | |
9 | import { SelectionManager } from './SelectionManager'; | |
71477874 | 10 | import { SelectionModel } from './SelectionModel'; |
cb5a452c | 11 | import { BufferSet } from './BufferSet'; |
f03d00a4 | 12 | import { MockTerminal } from './utils/TestUtils'; |
d3865ad2 DI |
13 | |
14 | class TestSelectionManager extends SelectionManager { | |
15 | constructor( | |
16 | terminal: ITerminal, | |
59f75555 | 17 | buffer: ICircularList<[number, string, number][]>, |
d3865ad2 DI |
18 | rowContainer: HTMLElement, |
19 | charMeasure: CharMeasure | |
20 | ) { | |
21 | super(terminal, buffer, rowContainer, charMeasure); | |
22 | } | |
23 | ||
fd91c5e1 DI |
24 | public get model(): SelectionModel { return this._model; } |
25 | ||
26 | public selectLineAt(line: number): void { this._selectLineAt(line); } | |
d3865ad2 DI |
27 | public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); } |
28 | ||
29 | // Disable DOM interaction | |
30 | public enable(): void {} | |
31 | public disable(): void {} | |
32 | public refresh(): void {} | |
33 | } | |
34 | ||
35 | describe('SelectionManager', () => { | |
0ea2e9cf | 36 | let dom: jsdom.JSDOM; |
d3865ad2 DI |
37 | let window: Window; |
38 | let document: Document; | |
39 | ||
fd91c5e1 | 40 | let terminal: ITerminal; |
59f75555 | 41 | let bufferLines: ICircularList<[number, string, number][]>; |
d3865ad2 DI |
42 | let rowContainer: HTMLElement; |
43 | let selectionManager: TestSelectionManager; | |
44 | ||
0ea2e9cf PK |
45 | beforeEach(() => { |
46 | dom = new jsdom.JSDOM(''); | |
47 | window = dom.window; | |
28062884 | 48 | document = window.document; |
9e80a124 | 49 | rowContainer = document.createElement('div'); |
f03d00a4 DI |
50 | terminal = new MockTerminal(); |
51 | terminal.cols = 80; | |
52 | terminal.rows = 2; | |
de82bd00 OA |
53 | terminal.scrollback = 100; |
54 | terminal.buffers = new BufferSet(terminal); | |
55 | terminal.buffer = terminal.buffers.active; | |
28062884 | 56 | bufferLines = terminal.buffer.lines; |
83efc9af | 57 | selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null); |
d3865ad2 DI |
58 | }); |
59 | ||
60 | function stringToRow(text: string): [number, string, number][] { | |
61 | let result: [number, string, number][] = []; | |
62 | for (let i = 0; i < text.length; i++) { | |
63 | result.push([0, text.charAt(i), 1]); | |
64 | } | |
65 | return result; | |
66 | } | |
67 | ||
68 | describe('_selectWordAt', () => { | |
69 | it('should expand selection for normal width chars', () => { | |
f03d00a4 | 70 | bufferLines.set(0, stringToRow('foo bar')); |
d3865ad2 DI |
71 | selectionManager.selectWordAt([0, 0]); |
72 | assert.equal(selectionManager.selectionText, 'foo'); | |
73 | selectionManager.selectWordAt([1, 0]); | |
74 | assert.equal(selectionManager.selectionText, 'foo'); | |
75 | selectionManager.selectWordAt([2, 0]); | |
76 | assert.equal(selectionManager.selectionText, 'foo'); | |
77 | selectionManager.selectWordAt([3, 0]); | |
78 | assert.equal(selectionManager.selectionText, ' '); | |
79 | selectionManager.selectWordAt([4, 0]); | |
80 | assert.equal(selectionManager.selectionText, 'bar'); | |
81 | selectionManager.selectWordAt([5, 0]); | |
82 | assert.equal(selectionManager.selectionText, 'bar'); | |
83 | selectionManager.selectWordAt([6, 0]); | |
84 | assert.equal(selectionManager.selectionText, 'bar'); | |
85 | }); | |
86 | it('should expand selection for whitespace', () => { | |
f03d00a4 | 87 | bufferLines.set(0, stringToRow('a b')); |
d3865ad2 DI |
88 | selectionManager.selectWordAt([0, 0]); |
89 | assert.equal(selectionManager.selectionText, 'a'); | |
90 | selectionManager.selectWordAt([1, 0]); | |
91 | assert.equal(selectionManager.selectionText, ' '); | |
92 | selectionManager.selectWordAt([2, 0]); | |
93 | assert.equal(selectionManager.selectionText, ' '); | |
94 | selectionManager.selectWordAt([3, 0]); | |
95 | assert.equal(selectionManager.selectionText, ' '); | |
96 | selectionManager.selectWordAt([4, 0]); | |
97 | assert.equal(selectionManager.selectionText, 'b'); | |
98 | }); | |
99 | it('should expand selection for wide characters', () => { | |
100 | // Wide characters use a special format | |
f03d00a4 | 101 | bufferLines.set(0, [ |
d3865ad2 DI |
102 | [null, '中', 2], |
103 | [null, '', 0], | |
104 | [null, '文', 2], | |
105 | [null, '', 0], | |
106 | [null, ' ', 1], | |
107 | [null, 'a', 1], | |
108 | [null, '中', 2], | |
109 | [null, '', 0], | |
110 | [null, '文', 2], | |
111 | [null, '', 0], | |
112 | [null, 'b', 1], | |
113 | [null, ' ', 1], | |
114 | [null, 'f', 1], | |
115 | [null, 'o', 1], | |
116 | [null, 'o', 1] | |
117 | ]); | |
118 | // Ensure wide characters take up 2 columns | |
119 | selectionManager.selectWordAt([0, 0]); | |
120 | assert.equal(selectionManager.selectionText, '中文'); | |
121 | selectionManager.selectWordAt([1, 0]); | |
122 | assert.equal(selectionManager.selectionText, '中文'); | |
123 | selectionManager.selectWordAt([2, 0]); | |
124 | assert.equal(selectionManager.selectionText, '中文'); | |
125 | selectionManager.selectWordAt([3, 0]); | |
126 | assert.equal(selectionManager.selectionText, '中文'); | |
127 | selectionManager.selectWordAt([4, 0]); | |
128 | assert.equal(selectionManager.selectionText, ' '); | |
129 | // Ensure wide characters work when wrapped in normal width characters | |
130 | selectionManager.selectWordAt([5, 0]); | |
131 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
132 | selectionManager.selectWordAt([6, 0]); | |
133 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
134 | selectionManager.selectWordAt([7, 0]); | |
135 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
136 | selectionManager.selectWordAt([8, 0]); | |
137 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
138 | selectionManager.selectWordAt([9, 0]); | |
139 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
140 | selectionManager.selectWordAt([10, 0]); | |
141 | assert.equal(selectionManager.selectionText, 'a中文b'); | |
142 | selectionManager.selectWordAt([11, 0]); | |
143 | assert.equal(selectionManager.selectionText, ' '); | |
144 | // Ensure normal width characters work fine in a line containing wide characters | |
145 | selectionManager.selectWordAt([12, 0]); | |
146 | assert.equal(selectionManager.selectionText, 'foo'); | |
147 | selectionManager.selectWordAt([13, 0]); | |
148 | assert.equal(selectionManager.selectionText, 'foo'); | |
149 | selectionManager.selectWordAt([14, 0]); | |
150 | assert.equal(selectionManager.selectionText, 'foo'); | |
151 | }); | |
7f2e94c3 | 152 | it('should select up to non-path characters that are commonly adjacent to paths', () => { |
f03d00a4 | 153 | bufferLines.set(0, stringToRow('(cd)[ef]{gh}\'ij"')); |
7f2e94c3 | 154 | selectionManager.selectWordAt([0, 0]); |
7f2e94c3 | 155 | assert.equal(selectionManager.selectionText, '(cd'); |
7463166a | 156 | selectionManager.selectWordAt([1, 0]); |
7f2e94c3 | 157 | assert.equal(selectionManager.selectionText, 'cd'); |
7463166a | 158 | selectionManager.selectWordAt([2, 0]); |
7f2e94c3 | 159 | assert.equal(selectionManager.selectionText, 'cd'); |
7463166a | 160 | selectionManager.selectWordAt([3, 0]); |
7f2e94c3 | 161 | assert.equal(selectionManager.selectionText, 'cd)'); |
7463166a | 162 | selectionManager.selectWordAt([4, 0]); |
7f2e94c3 | 163 | assert.equal(selectionManager.selectionText, '[ef'); |
7463166a | 164 | selectionManager.selectWordAt([5, 0]); |
7f2e94c3 | 165 | assert.equal(selectionManager.selectionText, 'ef'); |
7463166a | 166 | selectionManager.selectWordAt([6, 0]); |
7f2e94c3 | 167 | assert.equal(selectionManager.selectionText, 'ef'); |
7463166a | 168 | selectionManager.selectWordAt([7, 0]); |
7f2e94c3 | 169 | assert.equal(selectionManager.selectionText, 'ef]'); |
7463166a | 170 | selectionManager.selectWordAt([8, 0]); |
7f2e94c3 | 171 | assert.equal(selectionManager.selectionText, '{gh'); |
7463166a | 172 | selectionManager.selectWordAt([9, 0]); |
7f2e94c3 | 173 | assert.equal(selectionManager.selectionText, 'gh'); |
7463166a | 174 | selectionManager.selectWordAt([10, 0]); |
7f2e94c3 | 175 | assert.equal(selectionManager.selectionText, 'gh'); |
7463166a | 176 | selectionManager.selectWordAt([11, 0]); |
7f2e94c3 | 177 | assert.equal(selectionManager.selectionText, 'gh}'); |
7463166a | 178 | selectionManager.selectWordAt([12, 0]); |
07882c42 | 179 | assert.equal(selectionManager.selectionText, '\'ij'); |
7463166a | 180 | selectionManager.selectWordAt([13, 0]); |
07882c42 | 181 | assert.equal(selectionManager.selectionText, 'ij'); |
7463166a | 182 | selectionManager.selectWordAt([14, 0]); |
07882c42 | 183 | assert.equal(selectionManager.selectionText, 'ij'); |
7463166a | 184 | selectionManager.selectWordAt([15, 0]); |
07882c42 | 185 | assert.equal(selectionManager.selectionText, 'ij"'); |
7f2e94c3 | 186 | }); |
d3865ad2 | 187 | }); |
fd91c5e1 DI |
188 | |
189 | describe('_selectLineAt', () => { | |
190 | it('should select the entire line', () => { | |
f03d00a4 | 191 | bufferLines.set(0, stringToRow('foo bar')); |
fd91c5e1 DI |
192 | selectionManager.selectLineAt(0); |
193 | assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct'); | |
194 | assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]); | |
195 | assert.deepEqual(selectionManager.model.finalSelectionEnd, [terminal.cols, 0], 'The actual selection spans the entire column'); | |
196 | }); | |
197 | }); | |
2b243182 DI |
198 | |
199 | describe('selectAll', () => { | |
200 | it('should select the entire buffer, beyond the viewport', () => { | |
f03d00a4 DI |
201 | bufferLines.length = 5; |
202 | bufferLines.set(0, stringToRow('1')); | |
203 | bufferLines.set(1, stringToRow('2')); | |
204 | bufferLines.set(2, stringToRow('3')); | |
205 | bufferLines.set(3, stringToRow('4')); | |
206 | bufferLines.set(4, stringToRow('5')); | |
2b243182 | 207 | selectionManager.selectAll(); |
83efc9af | 208 | terminal.buffer.ybase = bufferLines.length - terminal.rows; |
2b243182 DI |
209 | assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5'); |
210 | }); | |
211 | }); | |
7b469407 DI |
212 | |
213 | describe('hasSelection', () => { | |
214 | it('should return whether there is a selection', () => { | |
215 | selectionManager.model.selectionStart = [0, 0]; | |
216 | selectionManager.model.selectionStartLength = 0; | |
217 | assert.equal(selectionManager.hasSelection, false); | |
218 | selectionManager.model.selectionEnd = [0, 0]; | |
219 | assert.equal(selectionManager.hasSelection, false); | |
220 | selectionManager.model.selectionEnd = [1, 0]; | |
221 | assert.equal(selectionManager.hasSelection, true); | |
222 | selectionManager.model.selectionEnd = [0, 1]; | |
223 | assert.equal(selectionManager.hasSelection, true); | |
224 | selectionManager.model.selectionEnd = [1, 1]; | |
225 | assert.equal(selectionManager.hasSelection, true); | |
226 | }); | |
227 | }); | |
d3865ad2 | 228 | }); |