]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * @license MIT | |
3 | */ | |
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'; | |
12 | import { MockTerminal } from './utils/TestUtils'; | |
13 | ||
14 | class TestSelectionManager extends SelectionManager { | |
15 | constructor( | |
16 | terminal: ITerminal, | |
17 | buffer: ICircularList<[number, string, number][]>, | |
18 | rowContainer: HTMLElement, | |
19 | charMeasure: CharMeasure | |
20 | ) { | |
21 | super(terminal, buffer, rowContainer, charMeasure); | |
22 | } | |
23 | ||
24 | public get model(): SelectionModel { return this._model; } | |
25 | ||
26 | public selectLineAt(line: number): void { this._selectLineAt(line); } | |
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', () => { | |
36 | let dom: jsdom.JSDOM; | |
37 | let window: Window; | |
38 | let document: Document; | |
39 | ||
40 | let terminal: ITerminal; | |
41 | let bufferLines: ICircularList<[number, string, number][]>; | |
42 | let rowContainer: HTMLElement; | |
43 | let selectionManager: TestSelectionManager; | |
44 | ||
45 | beforeEach(() => { | |
46 | dom = new jsdom.JSDOM(''); | |
47 | window = dom.window; | |
48 | document = window.document; | |
49 | rowContainer = document.createElement('div'); | |
50 | terminal = new MockTerminal(); | |
51 | terminal.cols = 80; | |
52 | terminal.rows = 2; | |
53 | terminal.scrollback = 100; | |
54 | terminal.buffers = new BufferSet(terminal); | |
55 | terminal.buffer = terminal.buffers.active; | |
56 | bufferLines = terminal.buffer.lines; | |
57 | selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null); | |
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', () => { | |
70 | bufferLines.set(0, stringToRow('foo bar')); | |
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', () => { | |
87 | bufferLines.set(0, stringToRow('a b')); | |
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 | |
101 | bufferLines.set(0, [ | |
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 | }); | |
152 | it('should select up to non-path characters that are commonly adjacent to paths', () => { | |
153 | bufferLines.set(0, stringToRow('(cd)[ef]{gh}\'ij"')); | |
154 | selectionManager.selectWordAt([0, 0]); | |
155 | assert.equal(selectionManager.selectionText, '(cd'); | |
156 | selectionManager.selectWordAt([1, 0]); | |
157 | assert.equal(selectionManager.selectionText, 'cd'); | |
158 | selectionManager.selectWordAt([2, 0]); | |
159 | assert.equal(selectionManager.selectionText, 'cd'); | |
160 | selectionManager.selectWordAt([3, 0]); | |
161 | assert.equal(selectionManager.selectionText, 'cd)'); | |
162 | selectionManager.selectWordAt([4, 0]); | |
163 | assert.equal(selectionManager.selectionText, '[ef'); | |
164 | selectionManager.selectWordAt([5, 0]); | |
165 | assert.equal(selectionManager.selectionText, 'ef'); | |
166 | selectionManager.selectWordAt([6, 0]); | |
167 | assert.equal(selectionManager.selectionText, 'ef'); | |
168 | selectionManager.selectWordAt([7, 0]); | |
169 | assert.equal(selectionManager.selectionText, 'ef]'); | |
170 | selectionManager.selectWordAt([8, 0]); | |
171 | assert.equal(selectionManager.selectionText, '{gh'); | |
172 | selectionManager.selectWordAt([9, 0]); | |
173 | assert.equal(selectionManager.selectionText, 'gh'); | |
174 | selectionManager.selectWordAt([10, 0]); | |
175 | assert.equal(selectionManager.selectionText, 'gh'); | |
176 | selectionManager.selectWordAt([11, 0]); | |
177 | assert.equal(selectionManager.selectionText, 'gh}'); | |
178 | selectionManager.selectWordAt([12, 0]); | |
179 | assert.equal(selectionManager.selectionText, '\'ij'); | |
180 | selectionManager.selectWordAt([13, 0]); | |
181 | assert.equal(selectionManager.selectionText, 'ij'); | |
182 | selectionManager.selectWordAt([14, 0]); | |
183 | assert.equal(selectionManager.selectionText, 'ij'); | |
184 | selectionManager.selectWordAt([15, 0]); | |
185 | assert.equal(selectionManager.selectionText, 'ij"'); | |
186 | }); | |
187 | }); | |
188 | ||
189 | describe('_selectLineAt', () => { | |
190 | it('should select the entire line', () => { | |
191 | bufferLines.set(0, stringToRow('foo bar')); | |
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 | }); | |
198 | ||
199 | describe('selectAll', () => { | |
200 | it('should select the entire buffer, beyond the viewport', () => { | |
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')); | |
207 | selectionManager.selectAll(); | |
208 | terminal.buffer.ybase = bufferLines.length - terminal.rows; | |
209 | assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5'); | |
210 | }); | |
211 | }); | |
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 | }); | |
228 | }); |