Skip to content

Commit a334f3d

Browse files
authored
build(tests): type check Buffer.test.ts #410
1 parent edc5011 commit a334f3d

File tree

2 files changed

+76
-54
lines changed

2 files changed

+76
-54
lines changed

packages/neovim/src/api/Buffer.test.ts

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-env jest */
2+
import assert from 'node:assert';
23
import * as testUtil from '../testUtil';
4+
import type { Buffer } from './Buffer';
35

46
function wait(ms: number): Promise<void> {
57
return new Promise(resolve => {
@@ -14,14 +16,16 @@ describe('Buffer API', () => {
1416

1517
// utility to allow each test to be run in its
1618
// own buffer
17-
function withBuffer(lines, test) {
19+
function withBuffer(
20+
lines: string[],
21+
test: (buffer: Buffer) => Promise<void>
22+
) {
1823
return async () => {
1924
await nvim.command('new!');
2025

2126
const buffer = await nvim.buffer;
2227

2328
if (lines) {
24-
await buffer;
2529
await buffer.replace(lines, 0);
2630
}
2731

@@ -151,7 +155,7 @@ describe('Buffer API', () => {
151155
it(
152156
'removes last 2 lines',
153157
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async buffer => {
154-
buffer.remove(-3, -1);
158+
buffer.remove(-3, -1, true);
155159
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
156160
})
157161
);
@@ -178,7 +182,8 @@ describe('Buffer API', () => {
178182
it('returns -1 for byte offset of unloaded buffer', async () => {
179183
await nvim.command('new');
180184
await nvim.command('bunload!');
181-
expect(await nvim.buffer.getOffset(0)).toEqual(-1);
185+
const buffer = await nvim.buffer;
186+
expect(await buffer.getOffset(0)).toEqual(-1);
182187
});
183188

184189
it(
@@ -199,7 +204,7 @@ describe('Buffer API', () => {
199204
it(
200205
'can clear the buffer',
201206
withBuffer(['foo'], async buffer => {
202-
buffer.remove(0, -1);
207+
buffer.remove(0, -1, true);
203208
// One empty line
204209
expect(await buffer.length).toEqual(1);
205210
expect(await buffer.lines).toEqual(['']);
@@ -214,7 +219,7 @@ describe('Buffer API', () => {
214219
expect(await buffer.getOption('copyindent')).toBe(true);
215220
buffer.setOption('copyindent', false);
216221
expect(await buffer.getOption('copyindent')).toBe(false);
217-
222+
assert(initial !== undefined);
218223
// Restore option
219224
buffer.setOption('copyindent', initial);
220225
expect(await buffer.getOption('copyindent')).toBe(initial);
@@ -253,7 +258,7 @@ describe('Buffer API', () => {
253258
'sets virtual text and clears namespace',
254259
withBuffer(['test'], async buffer => {
255260
const ns = await nvim.createNamespace();
256-
await buffer.setVirtualText(ns, 0, [['annotation']]);
261+
await buffer.setVirtualText(ns, 0, [['annotation', '']]);
257262
await buffer.clearNamespace({ nsId: ns });
258263
})
259264
);
@@ -263,35 +268,39 @@ describe('Buffer API', () => {
263268

264269
describe('Chainable API calls', () => {
265270
it('sets/gets the current buffer name using api chaining', async () => {
266-
nvim.buffer.name = 'goodbye.txt';
271+
const buffer = await nvim.buffer;
272+
buffer.name = 'goodbye.txt';
267273
expect(await nvim.buffer.name).toMatch('goodbye.txt');
268274
});
269275

270276
it('can chain calls from Base class i.e. getOption', async () => {
271-
const initial = await nvim.buffer.getOption('copyindent');
272-
nvim.buffer.setOption('copyindent', true);
273-
expect(await nvim.buffer.getOption('copyindent')).toBe(true);
274-
nvim.buffer.setOption('copyindent', false);
275-
expect(await nvim.buffer.getOption('copyindent')).toBe(false);
276-
277+
const buffer = await nvim.buffer;
278+
const initial = await buffer.getOption('copyindent');
279+
buffer.setOption('copyindent', true);
280+
expect(await buffer.getOption('copyindent')).toBe(true);
281+
buffer.setOption('copyindent', false);
282+
expect(await buffer.getOption('copyindent')).toBe(false);
283+
assert(initial !== undefined);
277284
// Restore option
278-
nvim.buffer.setOption('copyindent', initial);
279-
expect(await nvim.buffer.getOption('copyindent')).toBe(initial);
285+
buffer.setOption('copyindent', initial);
286+
expect(await buffer.getOption('copyindent')).toBe(initial);
280287
});
281288

282289
it('sets current buffer name to "bar.js" using api chaining', async () => {
283-
await (nvim.buffer.name = 'bar.js');
284-
expect(await nvim.buffer.name).toMatch('bar.js');
290+
const buffer = await nvim.buffer;
291+
buffer.name = 'bar.js';
292+
expect(await buffer.name).toMatch('bar.js');
285293

286-
await (nvim.buffer.name = 'test2.js');
287-
expect(await nvim.buffer.name).toMatch('test2.js');
294+
buffer.name = 'test2.js';
295+
expect(await buffer.name).toMatch('test2.js');
288296
});
289297

290298
it(
291299
'can replace first line of nvim.buffer with a string',
292300
withBuffer([], async () => {
293-
await nvim.buffer.replace('test', 0);
294-
expect(await nvim.buffer.lines).toEqual(['test']);
301+
const buffer = await nvim.buffer;
302+
await buffer.replace('test', 0);
303+
expect(await buffer.lines).toEqual(['test']);
295304
})
296305
);
297306

@@ -300,9 +309,10 @@ describe('Buffer API', () => {
300309
withBuffer(
301310
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
302311
async () => {
303-
await nvim.buffer.replace(['a', 'b', 'c'], 2);
312+
const buffer = await nvim.buffer;
313+
await buffer.replace(['a', 'b', 'c'], 2);
304314

305-
expect(await nvim.buffer.lines).toEqual([
315+
expect(await buffer.lines).toEqual([
306316
'0',
307317
'1',
308318
'a',
@@ -321,24 +331,27 @@ describe('Buffer API', () => {
321331
it(
322332
'can insert lines at beginning of buffer',
323333
withBuffer(['test'], async () => {
324-
await nvim.buffer.insert(['test', 'foo'], 0);
325-
expect(await nvim.buffer.lines).toEqual(['test', 'foo', 'test']);
334+
const buffer = await nvim.buffer;
335+
await buffer.insert(['test', 'foo'], 0);
336+
expect(await buffer.lines).toEqual(['test', 'foo', 'test']);
326337
})
327338
);
328339

329340
it(
330341
'can replace nvim.buffer starting at line 1',
331342
withBuffer(['test', 'foo'], async () => {
332-
await nvim.buffer.replace(['bar', 'bar', 'bar'], 1);
333-
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
343+
const buffer = await nvim.buffer;
344+
await buffer.replace(['bar', 'bar', 'bar'], 1);
345+
expect(await buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
334346
})
335347
);
336348

337349
it(
338350
'inserts line at index 2',
339351
withBuffer(['test', 'bar', 'bar', 'bar'], async () => {
340-
await nvim.buffer.insert(['foo'], 2);
341-
expect(await nvim.buffer.lines).toEqual([
352+
const buffer = await nvim.buffer;
353+
await buffer.insert(['foo'], 2);
354+
expect(await buffer.lines).toEqual([
342355
'test',
343356
'bar',
344357
'foo',
@@ -351,16 +364,18 @@ describe('Buffer API', () => {
351364
it(
352365
'removes last 2 lines',
353366
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async () => {
354-
await nvim.buffer.remove(-3, -1);
355-
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'foo']);
367+
const buffer = await nvim.buffer;
368+
await buffer.remove(-3, -1, true);
369+
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
356370
})
357371
);
358372

359373
it(
360374
'append lines to end of buffer',
361375
withBuffer(['test', 'bar', 'foo'], async () => {
362-
await nvim.buffer.append(['test', 'test']);
363-
expect(await nvim.buffer.lines).toEqual([
376+
const buffer = await nvim.buffer;
377+
await buffer.append(['test', 'test']);
378+
expect(await buffer.lines).toEqual([
364379
'test',
365380
'bar',
366381
'foo',
@@ -373,10 +388,11 @@ describe('Buffer API', () => {
373388
it(
374389
'can clear the buffer',
375390
withBuffer(['foo'], async () => {
376-
await nvim.buffer.remove(0, -1);
391+
const buffer = await nvim.buffer;
392+
await buffer.remove(0, -1, true);
377393
// One empty line
378-
expect(await nvim.buffer.length).toEqual(1);
379-
expect(await nvim.buffer.lines).toEqual(['']);
394+
expect(await buffer.length).toEqual(1);
395+
expect(await buffer.lines).toEqual(['']);
380396
})
381397
);
382398
});
@@ -394,17 +410,17 @@ describe('Buffer event updates', () => {
394410
});
395411

396412
beforeEach(async () => {
397-
await nvim.buffer.remove(0, -1);
413+
await (await nvim.buffer).remove(0, -1, true);
398414
});
399415

400416
it('can listen and unlisten', async () => {
401417
const buffer = await nvim.buffer;
402418
const mock = jest.fn();
403419
const unlisten = buffer.listen('lines', mock);
404-
await nvim.buffer.insert(['bar'], 1);
420+
await buffer.insert(['bar'], 1);
405421
expect(mock).toHaveBeenCalledTimes(1);
406422
unlisten();
407-
await nvim.buffer.insert(['bar'], 1);
423+
await buffer.insert(['bar'], 1);
408424
expect(mock).toHaveBeenCalledTimes(1);
409425
});
410426

@@ -415,7 +431,7 @@ describe('Buffer event updates', () => {
415431
await wait(10);
416432
const mock = jest.fn();
417433
unlisten = buffer.listen('lines', mock);
418-
await nvim.buffer.insert(['bar'], 1);
434+
await buffer.insert(['bar'], 1);
419435
expect(mock).toHaveBeenCalledTimes(1);
420436
unlisten();
421437
});
@@ -437,18 +453,18 @@ describe('Buffer event updates', () => {
437453
const mock = jest.fn();
438454
buffer.listen('lines', mock);
439455
buffer.listen('lines', mock);
440-
await nvim.buffer.insert(['bar'], 1);
456+
await buffer.insert(['bar'], 1);
441457
expect(mock).toHaveBeenCalledTimes(1);
442458
});
443459

444460
it('can use `buffer.unlisten` to unlisten', async () => {
445461
const buffer = await nvim.buffer;
446462
const mock = jest.fn();
447463
buffer.listen('lines', mock);
448-
await nvim.buffer.insert(['bar'], 1);
464+
await buffer.insert(['bar'], 1);
449465
expect(mock).toHaveBeenCalledTimes(1);
450466
buffer.unlisten('lines', mock);
451-
await nvim.buffer.insert(['bar'], 1);
467+
await buffer.insert(['bar'], 1);
452468
expect(mock).toHaveBeenCalledTimes(1);
453469
});
454470

@@ -457,10 +473,16 @@ describe('Buffer event updates', () => {
457473
const bufferName = await buffer.name;
458474
await buffer.insert(['test', 'foo'], 0);
459475

460-
const promise = new Promise(resolve => {
476+
const promise = new Promise<void>(resolve => {
461477
const unlisten = buffer.listen(
462478
'lines',
463-
async (currentBuffer, tick, start, end, data) => {
479+
async (
480+
currentBuffer: Buffer,
481+
tick: number,
482+
start: number,
483+
end: number,
484+
data: string[]
485+
) => {
464486
expect(await currentBuffer.name).toBe(bufferName);
465487
expect(start).toBe(1);
466488
expect(end).toBe(1);
@@ -471,7 +493,7 @@ describe('Buffer event updates', () => {
471493
);
472494
});
473495

474-
await nvim.buffer.insert(['bar'], 1);
496+
await buffer.insert(['bar'], 1);
475497
await promise;
476498
});
477499

@@ -484,12 +506,12 @@ describe('Buffer event updates', () => {
484506
buffers[0].listen('lines', foo);
485507
buffers[1].listen('lines', bar);
486508

487-
await nvim.buffer.insert(['bar'], 1);
509+
await (await nvim.buffer).insert(['bar'], 1);
488510
expect(foo).toHaveBeenCalledTimes(0);
489511
expect(bar).toHaveBeenCalledTimes(1);
490512
await nvim.command('q!');
491513

492-
await nvim.buffer.insert(['foo'], 0);
514+
await (await nvim.buffer).insert(['foo'], 0);
493515
expect(foo).toHaveBeenCalledTimes(1);
494516
expect(bar).toHaveBeenCalledTimes(1);
495517

@@ -507,13 +529,13 @@ describe('Buffer event updates', () => {
507529
const unlisten1 = buffer.listen('lines', foo);
508530
const unlisten2 = buffer.listen('lines', bar);
509531

510-
await nvim.buffer.insert(['bar'], 1);
532+
await buffer.insert(['bar'], 1);
511533
expect(foo).toHaveBeenCalledTimes(1);
512534
expect(bar).toHaveBeenCalledTimes(1);
513535

514536
unlisten2();
515537

516-
await nvim.buffer.insert(['foo'], 0);
538+
await buffer.insert(['foo'], 0);
517539
expect(foo).toHaveBeenCalledTimes(2);
518540
expect(bar).toHaveBeenCalledTimes(1);
519541

@@ -531,13 +553,13 @@ describe('Buffer event updates', () => {
531553
const unlisten1 = buffer.listen('lines', foo);
532554
const unlisten2 = buffer.listen('changedtick', bar);
533555

534-
await nvim.buffer.insert(['bar'], 1);
556+
await buffer.insert(['bar'], 1);
535557
expect(foo).toHaveBeenCalledTimes(1);
536558
expect(bar).toHaveBeenCalledTimes(1);
537559

538560
unlisten2();
539561

540-
await nvim.buffer.insert(['foo'], 0);
562+
await buffer.insert(['foo'], 0);
541563
expect(foo).toHaveBeenCalledTimes(2);
542564
expect(bar).toHaveBeenCalledTimes(1);
543565

packages/neovim/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"compilerOptions": {
44
"outDir": "./lib"
55
},
6-
"include": ["src"],
6+
"include": ["src", "**/api/Buffer.test.ts"],
77
"exclude": ["node_modules", "__tests__", "**/*.test.ts", "examples", "lib"]
88
}

0 commit comments

Comments
 (0)