-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathHoverProcessor.spec.ts
More file actions
280 lines (241 loc) · 10.7 KB
/
Copy pathHoverProcessor.spec.ts
File metadata and controls
280 lines (241 loc) · 10.7 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { expect } from '../../chai-config.spec';
import { Program } from '../../Program';
import { util } from '../../util';
import { createSandbox } from 'sinon';
import { rootDir } from '../../testHelpers.spec';
let sinon = createSandbox();
const fence = (code: string) => util.mdFence(code, 'brightscript');
describe('HoverProcessor', () => {
let program: Program;
beforeEach(() => {
program = new Program({ rootDir: rootDir, sourceMap: true });
});
afterEach(() => {
sinon.restore();
program.dispose();
});
it('does not short-circuit the event since our plugin is the base plugin', () => {
const mock = sinon.mock();
program.plugins.add({
name: 'test-plugin',
provideHover: mock
});
const file = program.setFile('source/main.brs', `
sub main()
end sub
`);
//get the hover
program.getHover(file.pathAbsolute, util.createPosition(1, 20));
//the onGetHover function from `test-plugin` should always get called because
//BscPlugin should never short-circuit the event
expect(mock.called).to.be.true;
});
describe('BrsFile', () => {
it('works for param types', () => {
const file = program.setFile('source/main.brs', `
sub DoSomething(name as string)
name = 1
sayMyName = function(name as string)
end function
end sub
`);
//hover over the `name = 1` line
let hover = program.getHover(file.pathAbsolute, util.createPosition(2, 24))[0];
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(2, 20, 2, 24));
//hover over the `name` parameter declaration
hover = program.getHover(file.pathAbsolute, util.createPosition(1, 34))[0];
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(1, 32, 1, 36));
});
//ignore this for now...it's not a huge deal
it('does not match on keywords or data types', () => {
let file = program.setFile('source/main.brs', `
sub Main(name as string)
end sub
sub as()
end sub
`);
//hover over the `as`
expect(program.getHover(file.pathAbsolute, util.createPosition(1, 31))).to.be.empty;
//hover over the `string`
expect(program.getHover(file.pathAbsolute, util.createPosition(1, 36))).to.be.empty;
});
it('finds declared function', () => {
let file = program.setFile('source/main.brs', `
function Main(count = 1)
firstName = "bob"
age = 21
shoeSize = 10
end function
`);
let hover = program.getHover(file.pathAbsolute, util.createPosition(1, 28))[0];
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(1, 25, 1, 29));
expect(hover.contents).to.eql(fence('function Main(count? as dynamic) as dynamic'));
});
it('finds variable function hover in same scope', () => {
let file = program.setFile('source/main.brs', `
sub Main()
sayMyName = sub(name as string)
end sub
sayMyName()
end sub
`);
let hover = program.getHover(file.pathAbsolute, util.createPosition(5, 24))[0];
expect(hover.range).to.eql(util.createRange(5, 20, 5, 29));
expect(hover.contents).to.eql(fence('sub sayMyName(name as string) as void'));
});
it('finds function hover in file scope', () => {
let file = program.setFile('source/main.brs', `
sub Main()
sayMyName()
end sub
sub sayMyName()
end sub
`);
let hover = program.getHover(file.pathAbsolute, util.createPosition(2, 25))[0];
expect(hover.range).to.eql(util.createRange(2, 20, 2, 29));
expect(hover.contents).to.eql(fence('sub sayMyName() as void'));
});
it('finds function hover in scope', () => {
let rootDir = process.cwd();
program = new Program({
rootDir: rootDir
});
let mainFile = program.setFile('source/main.brs', `
sub Main()
sayMyName()
end sub
`);
program.setFile('source/lib.brs', `
sub sayMyName(name as string)
end sub
`);
program.validate();
let hover = program.getHover(mainFile.pathAbsolute, util.createPosition(2, 25))[0];
expect(hover?.range).to.eql(util.createRange(2, 20, 2, 29));
expect(hover?.contents).to.eql(fence('sub sayMyName(name as string) as void'));
});
it('finds top-level constant value', () => {
program.setFile('source/main.bs', `
sub main()
print SOME_VALUE
end sub
const SOME_VALUE = true
`);
program.validate();
// print SOM|E_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(2, 29))[0];
expect(hover?.range).to.eql(util.createRange(2, 26, 2, 36));
expect(hover?.contents).to.eql(fence('const SOME_VALUE = true'));
});
it('finds top-level constant in assignment expression', () => {
program.setFile('source/main.bs', `
sub main()
value = ""
value += SOME_VALUE
end sub
const SOME_VALUE = "value"
`);
program.validate();
// value += SOME|_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(3, 33))[0];
expect(hover?.range).to.eql(util.createRange(3, 29, 3, 39));
expect(hover?.contents).to.eql(fence('const SOME_VALUE = "value"'));
});
it('finds namespaced constant in assignment expression', () => {
program.setFile('source/main.bs', `
sub main()
value = ""
value += someNamespace.SOME_VALUE
end sub
namespace someNamespace
const SOME_VALUE = "value"
end namespace
`);
program.validate();
// value += SOME|_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(3, 47))[0];
expect(hover?.range).to.eql(util.createRange(3, 43, 3, 53));
expect(hover?.contents).to.eql(fence('const someNamespace.SOME_VALUE = "value"'));
});
it('finds namespaced constant value', () => {
program.setFile('source/main.bs', `
sub main()
print name.SOME_VALUE
end sub
namespace name
const SOME_VALUE = true
end namespace
`);
program.validate();
// print name.SOM|E_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(2, 34))[0];
expect(hover?.range).to.eql(util.createRange(2, 31, 2, 41));
expect(hover?.contents).to.eql(fence('const name.SOME_VALUE = true'));
});
it('finds deep namespaced constant value', () => {
program.setFile('source/main.bs', `
sub main()
print name.sp.a.c.e.SOME_VALUE
end sub
namespace name.sp.a.c.e
const SOME_VALUE = true
end namespace
`);
program.validate();
// print name.sp.a.c.e.SOM|E_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(2, 43))[0];
expect(hover?.range).to.eql(util.createRange(2, 40, 2, 50));
expect(hover?.contents).to.eql(fence('const name.sp.a.c.e.SOME_VALUE = true'));
});
it('finds hover for global function', () => {
const file = program.setFile('source/main.brs', `
sub main()
result = Abs(-5)
end sub
`);
program.validate();
// hover over the `Abs` function call
let hover = program.getHover(file.pathAbsolute, util.createPosition(2, 29))[0];
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(2, 29, 2, 32));
expect(hover.contents).to.contain('function Abs(x as float) as float');
expect(hover.contents).to.contain('Returns the absolute value of the argument.');
});
it('finds hover for global function when file has no scopes', () => {
// Create a program with no manifest and no scopes to ensure global scope fallback
const testProgram = new Program({ rootDir: rootDir });
const file = testProgram.setFile('standalone/main.brs', `
sub main()
result = Abs(-5)
end sub
`);
// Don't validate - this simulates a file that might not be in any scopes
// hover over the `Abs` function call
let hover = testProgram.getHover(file.pathAbsolute, util.createPosition(2, 29))[0];
testProgram.dispose();
// After the fix, this should now work even when file has no scopes
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(2, 29, 2, 32));
expect(hover.contents).to.contain('function Abs(x as float) as float');
expect(hover.contents).to.contain('Returns the absolute value of the argument.');
});
it('finds hover for global function with both shortDescription and documentation', () => {
const file = program.setFile('source/main.brs', `
sub main()
result = Atn(-5)
end sub
`);
program.validate();
// hover over the `Atn` function call
let hover = program.getHover(file.pathAbsolute, util.createPosition(2, 29))[0];
expect(hover).to.exist;
expect(hover.range).to.eql(util.createRange(2, 29, 2, 32));
expect(hover.contents).to.contain('function Atn(x as float) as float');
expect(hover.contents).to.contain('Returns the arctangent (in radians) of the argument.');
expect(hover.contents).to.contain('ATN(X)` returns "the angle whose tangent is X"');
});
});
});