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