Skip to content

Commit

Permalink
Merge pull request #421 from vim-denops/interrupt-test
Browse files Browse the repository at this point in the history
🌿 add/improve tests for interrupt
  • Loading branch information
lambdalisue authored Sep 17, 2024
2 parents c0035db + db8b371 commit 8b02b1b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
22 changes: 19 additions & 3 deletions denops/@denops-private/denops_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BatchError, type Meta } from "jsr:@denops/core@^7.0.0";
import {
assert,
assertEquals,
assertFalse,
assertInstanceOf,
assertRejects,
assertStrictEquals,
Expand Down Expand Up @@ -29,15 +31,29 @@ Deno.test("DenopsImpl", async (t) => {
call: () => unimplemented(),
batch: () => unimplemented(),
};
const serviceInterrupt = new AbortController();
const service: Service = {
dispatch: () => unimplemented(),
waitLoaded: () => unimplemented(),
interrupted: new AbortController().signal,
interrupted: serviceInterrupt.signal,
};
const denops = new DenopsImpl("dummy", meta, host, service);

await t.step("interrupted returns AbortSignal instance", () => {
assertInstanceOf(denops.interrupted, AbortSignal);
await t.step(".interrupted property", async (t) => {
await t.step("returns AbortSignal instance", () => {
assertInstanceOf(denops.interrupted, AbortSignal);
});

await t.step("aborts when `Service.interrupted` aborts", () => {
const signal = denops.interrupted;

assertFalse(signal.aborted);

serviceInterrupt.abort("test");

assert(signal.aborted);
assertEquals(signal.reason, "test");
});
});

await t.step(".redraw()", async (t) => {
Expand Down
83 changes: 83 additions & 0 deletions tests/denops/runtime/functions/denops/interrupt_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { assert, assertEquals } from "jsr:@std/assert@^1.0.1";
import { resolveTestDataPath } from "/denops-testdata/resolve.ts";
import { testHost } from "/denops-testutil/host.ts";
import { wait } from "/denops-testutil/wait.ts";

const scriptInterrupt = resolveTestDataPath("dummy_interrupt_plugin.ts");

testHost({
name: "denops#interrupt()",
mode: "all",
postlude: [
"let g:__test_denops_events = []",
"autocmd User DenopsPlugin* call add(g:__test_denops_events, expand('<amatch>'))",
"autocmd User DummyInterruptPlugin:* call add(g:__test_denops_events, expand('<amatch>'))",
"runtime plugin/denops.vim",
// NOTE: Disable startup on VimEnter.
"autocmd! denops_plugin_internal_startup VimEnter",
],
fn: async ({ host, t }) => {
await t.step("if the server is not yet running", async (t) => {
await t.step("returns immediately", async () => {
await host.call("denops#interrupt");
});
});

// Start the server and wait.
await host.call("denops#server#start");
await wait(() => host.call("eval", "denops#server#status() ==# 'running'"));

await t.step("if the plugin is not yet loaded", async (t) => {
await t.step("returns immediately", async () => {
await host.call("denops#interrupt");
});
});

await t.step("if the plugin is loaded", async (t) => {
// Load plugin and wait.
await host.call("execute", [
"let g:__test_denops_events = []",
`call denops#plugin#load('dummy', '${scriptInterrupt}')`,
], "");
await wait(async () =>
(await host.call("eval", "g:__test_denops_events") as string[])
.includes("DenopsPluginPost:dummy")
);
await host.call("execute", [
"let g:__test_denops_events = []",
], "");

await t.step("returns immediately", async () => {
await host.call("denops#interrupt");
});

await t.step("sends signal to the plugin", async () => {
await wait(() => host.call("len", "g:__test_denops_events"));
const events = await host.call(
"eval",
"g:__test_denops_events",
) as string[];
assert(
events.some((event) =>
event.startsWith("DummyInterruptPlugin:Interrupted:")
),
);
});

await host.call("denops#request", "dummy", "reset", []);

await t.step("sends signal to the plugin with reason", async () => {
await host.call("execute", [
"let g:__test_denops_events = []",
], "");

await host.call("denops#interrupt", "test");

await wait(() => host.call("len", "g:__test_denops_events"));
assertEquals(await host.call("eval", "g:__test_denops_events"), [
"DummyInterruptPlugin:Interrupted:test",
]);
});
});
},
});
17 changes: 17 additions & 0 deletions tests/denops/testdata/dummy_interrupt_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Entrypoint } from "jsr:@denops/core@^7.0.0";

export const main: Entrypoint = (denops) => {
function reset(): void {
const signal = denops.interrupted;
signal?.addEventListener("abort", async () => {
await denops.cmd(
`doautocmd <nomodeline> User DummyInterruptPlugin:Interrupted:${signal.reason}`,
);
}, { once: true });
}
reset();

denops.dispatcher = {
reset,
};
};

0 comments on commit 8b02b1b

Please sign in to comment.