Skip to content

Commit

Permalink
🌿 add tests for denops#interrupt()
Browse files Browse the repository at this point in the history
  • Loading branch information
Milly committed Sep 17, 2024
1 parent faf4cd2 commit ef6fd3f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/denops/runtime/functions/denops/interrupt_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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("eval", "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:")
),
`Expected event 'DummyInterruptPlugin:Interrupted:*', but: ${
JSON.stringify(events)
}`,
);
});

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("eval", "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 ef6fd3f

Please sign in to comment.