]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/SelectionModel.test.ts
6da3874bd40ed0e3ade296f0bdf3079bc1add443
[mirror_xterm.js.git] / src / SelectionModel.test.ts
1 /**
2 * @license MIT
3 */
4 import { assert } from 'chai';
5 import { ITerminal } from './Interfaces';
6 import { SelectionModel } from './SelectionModel';
7 import {BufferSet} from './BufferSet';
8
9 class TestSelectionModel extends SelectionModel {
10 constructor(
11 terminal: ITerminal
12 ) {
13 super(terminal);
14 }
15 }
16
17 describe('SelectionManager', () => {
18 let window: Window;
19 let document: Document;
20
21 let terminal: ITerminal;
22 let model: TestSelectionModel;
23
24 beforeEach(() => {
25 terminal = <any>{ cols: 80, rows: 2, ybase: 0 };
26 terminal.scrollback = 10;
27 terminal.buffers = new BufferSet(terminal);
28 terminal.buffer = terminal.buffers.active;
29
30 model = new TestSelectionModel(terminal);
31 });
32
33 describe('clearSelection', () => {
34 it('should clear the final selection', () => {
35 model.selectionStart = [0, 0];
36 model.selectionEnd = [10, 2];
37 assert.deepEqual(model.finalSelectionStart, [0, 0]);
38 assert.deepEqual(model.finalSelectionEnd, [10, 2]);
39 model.clearSelection();
40 assert.deepEqual(model.finalSelectionStart, null);
41 assert.deepEqual(model.finalSelectionEnd, null);
42 });
43 });
44
45 describe('areSelectionValuesReversed', () => {
46 it('should return true when the selection end is before selection start', () => {
47 model.selectionStart = [1, 0];
48 model.selectionEnd = [0, 0];
49 assert.equal(model.areSelectionValuesReversed(), true);
50 model.selectionStart = [10, 2];
51 model.selectionEnd = [0, 0];
52 assert.equal(model.areSelectionValuesReversed(), true);
53 });
54 it('should return false when the selection end is after selection start', () => {
55 model.selectionStart = [0, 0];
56 model.selectionEnd = [1, 0];
57 assert.equal(model.areSelectionValuesReversed(), false);
58 model.selectionStart = [0, 0];
59 model.selectionEnd = [10, 2];
60 assert.equal(model.areSelectionValuesReversed(), false);
61 });
62 });
63
64 describe('onTrim', () => {
65 it('should trim a portion of the selection when a part of it is trimmed', () => {
66 model.selectionStart = [0, 0];
67 model.selectionEnd = [10, 2];
68 model.onTrim(1);
69 assert.deepEqual(model.finalSelectionStart, [0, 0]);
70 assert.deepEqual(model.finalSelectionEnd, [10, 1]);
71 model.onTrim(1);
72 assert.deepEqual(model.finalSelectionStart, [0, 0]);
73 assert.deepEqual(model.finalSelectionEnd, [10, 0]);
74 });
75 it('should clear selection when it is trimmed in its entirety', () => {
76 model.selectionStart = [0, 0];
77 model.selectionEnd = [10, 0];
78 model.onTrim(1);
79 assert.deepEqual(model.finalSelectionStart, null);
80 assert.deepEqual(model.finalSelectionEnd, null);
81 });
82 });
83
84 describe('finalSelectionStart', () => {
85 it('should return the start of the buffer if select all is active', () => {
86 model.isSelectAllActive = true;
87 assert.deepEqual(model.finalSelectionStart, [0, 0]);
88 });
89 it('should return selection start if there is no selection end', () => {
90 model.selectionStart = [2, 2];
91 assert.deepEqual(model.finalSelectionStart, [2, 2]);
92 });
93 it('should return selection end if values are reversed', () => {
94 model.selectionStart = [2, 2];
95 model.selectionEnd = [3, 2];
96 assert.deepEqual(model.finalSelectionStart, [2, 2]);
97 model.selectionEnd = [1, 2];
98 assert.deepEqual(model.finalSelectionStart, [1, 2]);
99 });
100 });
101
102 describe('finalSelectionEnd', () => {
103 it('should return the end of the buffer if select all is active', () => {
104 model.isSelectAllActive = true;
105 assert.deepEqual(model.finalSelectionEnd, [80, 1]);
106 });
107 it('should return null if there is no selection start', () => {
108 assert.equal(model.finalSelectionEnd, null);
109 model.selectionEnd = [1, 2];
110 assert.equal(model.finalSelectionEnd, null);
111 });
112 it('should return selection start + length if there is no selection end', () => {
113 model.selectionStart = [2, 2];
114 model.selectionStartLength = 2;
115 assert.deepEqual(model.finalSelectionEnd, [4, 2]);
116 });
117 it('should return selection start + length if values are reversed', () => {
118 model.selectionStart = [2, 2];
119 model.selectionStartLength = 2;
120 model.selectionEnd = [2, 1];
121 assert.deepEqual(model.finalSelectionEnd, [4, 2]);
122 });
123 it('should return selection start + length if selection end is inside the start selection', () => {
124 model.selectionStart = [2, 2];
125 model.selectionStartLength = 2;
126 model.selectionEnd = [3, 2];
127 assert.deepEqual(model.finalSelectionEnd, [4, 2]);
128 });
129 it('should return selection end if selection end is after selection start + length', () => {
130 model.selectionStart = [2, 2];
131 model.selectionStartLength = 2;
132 model.selectionEnd = [5, 2];
133 assert.deepEqual(model.finalSelectionEnd, [5, 2]);
134 });
135 });
136 });