-
Notifications
You must be signed in to change notification settings - Fork 3
/
end_of_file_detector.test.js
162 lines (159 loc) · 7.48 KB
/
end_of_file_detector.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const assert = require('assert');
const vscode = require('vscode');
const { TestUtil } = require('./test_util.js');
const { endOfFileDetectorUtil, EndOfFileDetector } = require('../../src/end_of_file_detector.js');
describe('endOfFileDetectorUtil', () => {
describe('getCursorPosition', () => {
const getCursorPosition = endOfFileDetectorUtil.getCursorPosition;
it('should return the cursor position', () => {
const Input = { selections: [ new vscode.Selection(3, 5, 3, 5) ] };
const Expected = new vscode.Position(3, 5);
assert.strictEqual(getCursorPosition(Input).isEqual(Expected), true);
});
it('should return the active position of current selection range (1)', () => {
const Input = { selections: [ new vscode.Selection(3, 5, 6, 8) ] };
const Expected = new vscode.Position(6, 8);
assert.strictEqual(getCursorPosition(Input).isEqual(Expected), true);
});
it('should return the active position of current selection range (2)', () => {
const Input = { selections: [ new vscode.Selection(6, 8, 3, 5) ] };
const Expected = new vscode.Position(3, 5);
assert.strictEqual(getCursorPosition(Input).isEqual(Expected), true);
});
it('should return the position of the last cursor in multi-cursor (1)', () => {
const Input = { selections: [
new vscode.Selection(1, 4, 1, 4),
new vscode.Selection(2, 4, 2, 4),
new vscode.Selection(3, 4, 3, 4)
] };
const Expected = new vscode.Position(3, 4);
assert.strictEqual(getCursorPosition(Input).isEqual(Expected), true);
});
it('should return the position of the last cursor in multi-cursor (2)', () => {
const Input = { selections: [
new vscode.Selection(3, 4, 3, 4),
new vscode.Selection(2, 4, 2, 4),
new vscode.Selection(1, 4, 1, 4)
] };
const Expected = new vscode.Position(1, 4);
assert.strictEqual(getCursorPosition(Input).isEqual(Expected), true);
});
});
describe('calculateDistanceBelow', () => {
const calculateDistanceBelow = endOfFileDetectorUtil.calculateDistanceBelow;
it('should return [0, 0] if the editor is null', () => {
assert.deepStrictEqual(calculateDistanceBelow(null), [0, 0]);
});
it('should return the number of lines below the cursor and the number of characters right of the cursor', () => {
const editorMock = {
document: {
lineCount: 10,
lineAt: () => ({ text: ' '.repeat(15) })
},
selections: [
new vscode.Selection(5, 3, 5, 3)
]
};
assert.deepStrictEqual(calculateDistanceBelow(editorMock), [4, 12]);
});
it('should return [0, 0] if the cursor is at the end of the document', () => {
const editorMock = {
document: {
lineCount: 10,
lineAt: () => ({ text: ' '.repeat(15) })
},
selections: [
new vscode.Selection(9, 15, 9, 15)
]
};
assert.deepStrictEqual(calculateDistanceBelow(editorMock), [0, 0]);
});
});
describe('compareDistance', () => {
const compareDistance = endOfFileDetectorUtil.compareDistance;
it('should return 0 if two arguments are the same distance', () => {
assert.strictEqual(compareDistance([3, 4], [3, 4]), 0);
assert.strictEqual(compareDistance([0, 1], [0, 1]), 0);
assert.strictEqual(compareDistance([10, 0], [10, 0]), 0);
assert.strictEqual(compareDistance([8, 5], [8, 5]), 0);
});
it('should return a positive integer if first one is greater', () => {
assert.strictEqual(compareDistance([4, 1], [3, 1]) > 0, true);
assert.strictEqual(compareDistance([4, 5], [3, 7]) > 0, true);
assert.strictEqual(compareDistance([4, 7], [3, 5]) > 0, true);
assert.strictEqual(compareDistance([5, 8], [5, 4]) > 0, true);
});
it('should return a negative integer if first one is smaller', () => {
assert.strictEqual(compareDistance([3, 1], [4, 1]) < 0, true);
assert.strictEqual(compareDistance([3, 7], [4, 5]) < 0, true);
assert.strictEqual(compareDistance([3, 5], [4, 7]) < 0, true);
assert.strictEqual(compareDistance([5, 4], [5, 8]) < 0, true);
});
});
});
describe('EndOfFileDetector', () => {
let textEditor;
const setSelections = function(array) {
textEditor.selections = TestUtil.arrayToSelections(array);
};
before(async () => {
vscode.window.showInformationMessage('Started test for EndOfFileDetector.');
textEditor = await TestUtil.setupTextEditor({ content: '' });
});
describe('reachedEndOfFile', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'0. zero\n' +
'1. one\n' +
'2. two\n' +
'3. three\n' +
'4. four\n' +
'5. five'
));
});
it('should return true immediately if the cursor does not move at all', async () => {
setSelections([[2, 3]]);
const detector = EndOfFileDetector(textEditor);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
it('should return true immediately if the cursor moves up', async () => {
setSelections([[2, 3]]);
const detector = EndOfFileDetector(textEditor);
setSelections([[1, 3]]);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
it('should return true immediately if the cursor moves to the left', async () => {
setSelections([[2, 3]]);
const detector = EndOfFileDetector(textEditor);
setSelections([[2, 2]]);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
it('should return true if it reaches the end of the document', async () => {
setSelections([[5, 5]]);
const detector = EndOfFileDetector(textEditor);
setSelections([[5, 6]]);
assert.strictEqual(detector.reachedEndOfFile(), false);
setSelections([[5, 7]]);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
it('should return true if it reaches the last line of the document', async () => {
setSelections([[3, 0]]);
const detector = EndOfFileDetector(textEditor);
setSelections([[4, 0]]);
assert.strictEqual(detector.reachedEndOfFile(), false);
setSelections([[5, 0]]);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
it('should return true if the rest lines below the cursor stops to decline', async () => {
setSelections([[1, 0]]);
const detector = EndOfFileDetector(textEditor);
setSelections([[2, 0]]);
assert.strictEqual(detector.reachedEndOfFile(), false);
setSelections([[3, 0]]);
assert.strictEqual(detector.reachedEndOfFile(), false);
setSelections([[3, 8]]);
assert.strictEqual(detector.reachedEndOfFile(), true);
});
});
});