Skip to content

Commit

Permalink
Type check for Buffer.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gjf7 committed Oct 9, 2024
1 parent 3700a9b commit 5a3ca59
Showing 1 changed file with 73 additions and 52 deletions.
125 changes: 73 additions & 52 deletions packages/neovim/src/api/Buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */
import * as testUtil from '../testUtil';
import type { Buffer } from './Buffer';

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

// utility to allow each test to be run in its
// own buffer
function withBuffer(lines, test) {
function withBuffer(
lines: string[],
test: (buffer: Buffer) => Promise<void>
) {
return async () => {
await nvim.command('new!');

const buffer = await nvim.buffer;

if (lines) {
await buffer;
await buffer.replace(lines, 0);
}

Expand Down Expand Up @@ -151,7 +154,7 @@ describe('Buffer API', () => {
it(
'removes last 2 lines',
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async buffer => {
buffer.remove(-3, -1);
buffer.remove(-3, -1, true);
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
})
);
Expand All @@ -178,7 +181,8 @@ describe('Buffer API', () => {
it('returns -1 for byte offset of unloaded buffer', async () => {
await nvim.command('new');
await nvim.command('bunload!');
expect(await nvim.buffer.getOffset(0)).toEqual(-1);
const buffer = await nvim.buffer;
expect(await buffer.getOffset(0)).toEqual(-1);
});

it(
Expand All @@ -199,7 +203,7 @@ describe('Buffer API', () => {
it(
'can clear the buffer',
withBuffer(['foo'], async buffer => {
buffer.remove(0, -1);
buffer.remove(0, -1, true);
// One empty line
expect(await buffer.length).toEqual(1);
expect(await buffer.lines).toEqual(['']);
Expand All @@ -216,7 +220,7 @@ describe('Buffer API', () => {
expect(await buffer.getOption('copyindent')).toBe(false);

// Restore option
buffer.setOption('copyindent', initial);
buffer.setOption('copyindent', initial!);
expect(await buffer.getOption('copyindent')).toBe(initial);
})
);
Expand Down Expand Up @@ -253,7 +257,7 @@ describe('Buffer API', () => {
'sets virtual text and clears namespace',
withBuffer(['test'], async buffer => {
const ns = await nvim.createNamespace();
await buffer.setVirtualText(ns, 0, [['annotation']]);
await buffer.setVirtualText(ns, 0, [['annotation', '']]);
await buffer.clearNamespace({ nsId: ns });
})
);
Expand All @@ -263,35 +267,39 @@ describe('Buffer API', () => {

describe('Chainable API calls', () => {
it('sets/gets the current buffer name using api chaining', async () => {
nvim.buffer.name = 'goodbye.txt';
const buffer = await nvim.buffer;
buffer.name = 'goodbye.txt';
expect(await nvim.buffer.name).toMatch('goodbye.txt');
});

it('can chain calls from Base class i.e. getOption', async () => {
const initial = await nvim.buffer.getOption('copyindent');
nvim.buffer.setOption('copyindent', true);
expect(await nvim.buffer.getOption('copyindent')).toBe(true);
nvim.buffer.setOption('copyindent', false);
expect(await nvim.buffer.getOption('copyindent')).toBe(false);
const buffer = await nvim.buffer;
const initial = await buffer.getOption('copyindent');
buffer.setOption('copyindent', true);
expect(await buffer.getOption('copyindent')).toBe(true);
buffer.setOption('copyindent', false);
expect(await buffer.getOption('copyindent')).toBe(false);

// Restore option
nvim.buffer.setOption('copyindent', initial);
expect(await nvim.buffer.getOption('copyindent')).toBe(initial);
buffer.setOption('copyindent', initial!);
expect(await buffer.getOption('copyindent')).toBe(initial);
});

it('sets current buffer name to "bar.js" using api chaining', async () => {
await (nvim.buffer.name = 'bar.js');
expect(await nvim.buffer.name).toMatch('bar.js');
const buffer = await nvim.buffer;
buffer.name = 'bar.js';
expect(await buffer.name).toMatch('bar.js');

await (nvim.buffer.name = 'test2.js');
expect(await nvim.buffer.name).toMatch('test2.js');
buffer.name = 'test2.js';
expect(await buffer.name).toMatch('test2.js');
});

it(
'can replace first line of nvim.buffer with a string',
withBuffer([], async () => {
await nvim.buffer.replace('test', 0);
expect(await nvim.buffer.lines).toEqual(['test']);
const buffer = await nvim.buffer;
await buffer.replace('test', 0);
expect(await buffer.lines).toEqual(['test']);
})
);

Expand All @@ -300,9 +308,10 @@ describe('Buffer API', () => {
withBuffer(
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
async () => {
await nvim.buffer.replace(['a', 'b', 'c'], 2);
const buffer = await nvim.buffer;
await buffer.replace(['a', 'b', 'c'], 2);

expect(await nvim.buffer.lines).toEqual([
expect(await buffer.lines).toEqual([
'0',
'1',
'a',
Expand All @@ -321,24 +330,27 @@ describe('Buffer API', () => {
it(
'can insert lines at beginning of buffer',
withBuffer(['test'], async () => {
await nvim.buffer.insert(['test', 'foo'], 0);
expect(await nvim.buffer.lines).toEqual(['test', 'foo', 'test']);
const buffer = await nvim.buffer;
await buffer.insert(['test', 'foo'], 0);
expect(await buffer.lines).toEqual(['test', 'foo', 'test']);
})
);

it(
'can replace nvim.buffer starting at line 1',
withBuffer(['test', 'foo'], async () => {
await nvim.buffer.replace(['bar', 'bar', 'bar'], 1);
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
const buffer = await nvim.buffer;
await buffer.replace(['bar', 'bar', 'bar'], 1);
expect(await buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
})
);

it(
'inserts line at index 2',
withBuffer(['test', 'bar', 'bar', 'bar'], async () => {
await nvim.buffer.insert(['foo'], 2);
expect(await nvim.buffer.lines).toEqual([
const buffer = await nvim.buffer;
await buffer.insert(['foo'], 2);
expect(await buffer.lines).toEqual([
'test',
'bar',
'foo',
Expand All @@ -351,16 +363,18 @@ describe('Buffer API', () => {
it(
'removes last 2 lines',
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async () => {
await nvim.buffer.remove(-3, -1);
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'foo']);
const buffer = await nvim.buffer;
await buffer.remove(-3, -1, true);
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
})
);

it(
'append lines to end of buffer',
withBuffer(['test', 'bar', 'foo'], async () => {
await nvim.buffer.append(['test', 'test']);
expect(await nvim.buffer.lines).toEqual([
const buffer = await nvim.buffer;
await buffer.append(['test', 'test']);
expect(await buffer.lines).toEqual([
'test',
'bar',
'foo',
Expand All @@ -373,10 +387,11 @@ describe('Buffer API', () => {
it(
'can clear the buffer',
withBuffer(['foo'], async () => {
await nvim.buffer.remove(0, -1);
const buffer = await nvim.buffer;
await buffer.remove(0, -1, true);
// One empty line
expect(await nvim.buffer.length).toEqual(1);
expect(await nvim.buffer.lines).toEqual(['']);
expect(await buffer.length).toEqual(1);
expect(await buffer.lines).toEqual(['']);
})
);
});
Expand All @@ -394,17 +409,17 @@ describe('Buffer event updates', () => {
});

beforeEach(async () => {
await nvim.buffer.remove(0, -1);
await (await nvim.buffer).remove(0, -1, true);
});

it('can listen and unlisten', async () => {
const buffer = await nvim.buffer;
const mock = jest.fn();
const unlisten = buffer.listen('lines', mock);
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
unlisten();
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
});

Expand All @@ -415,7 +430,7 @@ describe('Buffer event updates', () => {
await wait(10);
const mock = jest.fn();
unlisten = buffer.listen('lines', mock);
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
unlisten();
});
Expand All @@ -437,18 +452,18 @@ describe('Buffer event updates', () => {
const mock = jest.fn();
buffer.listen('lines', mock);
buffer.listen('lines', mock);
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
});

it('can use `buffer.unlisten` to unlisten', async () => {
const buffer = await nvim.buffer;
const mock = jest.fn();
buffer.listen('lines', mock);
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
buffer.unlisten('lines', mock);
await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(mock).toHaveBeenCalledTimes(1);
});

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

const promise = new Promise(resolve => {
const promise = new Promise<void>(resolve => {
const unlisten = buffer.listen(
'lines',
async (currentBuffer, tick, start, end, data) => {
async (
currentBuffer: Buffer,
tick: number,
start: number,
end: number,
data: string[]
) => {
expect(await currentBuffer.name).toBe(bufferName);
expect(start).toBe(1);
expect(end).toBe(1);
Expand All @@ -471,7 +492,7 @@ describe('Buffer event updates', () => {
);
});

await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
await promise;
});

Expand All @@ -484,12 +505,12 @@ describe('Buffer event updates', () => {
buffers[0].listen('lines', foo);
buffers[1].listen('lines', bar);

await nvim.buffer.insert(['bar'], 1);
await (await nvim.buffer).insert(['bar'], 1);
expect(foo).toHaveBeenCalledTimes(0);
expect(bar).toHaveBeenCalledTimes(1);
await nvim.command('q!');

await nvim.buffer.insert(['foo'], 0);
await (await nvim.buffer).insert(['foo'], 0);
expect(foo).toHaveBeenCalledTimes(1);
expect(bar).toHaveBeenCalledTimes(1);

Expand All @@ -507,13 +528,13 @@ describe('Buffer event updates', () => {
const unlisten1 = buffer.listen('lines', foo);
const unlisten2 = buffer.listen('lines', bar);

await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(foo).toHaveBeenCalledTimes(1);
expect(bar).toHaveBeenCalledTimes(1);

unlisten2();

await nvim.buffer.insert(['foo'], 0);
await buffer.insert(['foo'], 0);
expect(foo).toHaveBeenCalledTimes(2);
expect(bar).toHaveBeenCalledTimes(1);

Expand All @@ -531,13 +552,13 @@ describe('Buffer event updates', () => {
const unlisten1 = buffer.listen('lines', foo);
const unlisten2 = buffer.listen('changedtick', bar);

await nvim.buffer.insert(['bar'], 1);
await buffer.insert(['bar'], 1);
expect(foo).toHaveBeenCalledTimes(1);
expect(bar).toHaveBeenCalledTimes(1);

unlisten2();

await nvim.buffer.insert(['foo'], 0);
await buffer.insert(['foo'], 0);
expect(foo).toHaveBeenCalledTimes(2);
expect(bar).toHaveBeenCalledTimes(1);

Expand Down

0 comments on commit 5a3ca59

Please sign in to comment.