Skip to content

Commit

Permalink
Merge pull request #323 from 4513ECHO/refactor/using-declaration
Browse files Browse the repository at this point in the history
💪 use using declaration instead of deno-disposable
  • Loading branch information
lambdalisue authored Feb 25, 2024
2 parents 113c492 + 08cdde4 commit 2d6c0a1
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 261 deletions.
3 changes: 1 addition & 2 deletions denops/@denops-private/host.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Disposable } from "https://deno.land/x/disposable@v1.2.0/mod.ts";
import { ensure, is } from "https://deno.land/x/unknownutil@v3.16.3/mod.ts";

/**
* Host (Vim/Neovim) which is visible from Service
*/
export interface Host extends Disposable {
export interface Host extends AsyncDisposable {
/**
* Redraw text and cursor on Vim but Neovim.
*/
Expand Down
2 changes: 1 addition & 1 deletion denops/@denops-private/host/nvim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Neovim implements Host {
return this.#session.wait();
}

async dispose(): Promise<void> {
async [Symbol.asyncDispose](): Promise<void> {
try {
await this.#session.shutdown();
} catch {
Expand Down
226 changes: 112 additions & 114 deletions denops/@denops-private/host/nvim_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
stub,
} from "https://deno.land/std@0.217.0/testing/mock.ts";
import { promiseState } from "https://deno.land/x/async@v2.1.0/mod.ts";
import { usingResource } from "https://deno.land/x/disposable@v1.2.0/mod.ts";
import { withNeovim } from "../testutil/with.ts";
import { Service } from "../host.ts";
import { Neovim } from "./nvim.ts";
Expand All @@ -26,137 +25,136 @@ Deno.test("Neovim", async (t) => {
dispatchAsync: () => unimplemented(),
};

await usingResource(new Neovim(reader, writer), async (host) => {
await t.step(
"'invoke' message before init throws error",
async () => {
await assertRejects(
() =>
host.call(
"denops#_internal#test#request",
"invoke",
["reload", ["dummy"]],
),
Error,
"Failed to call",
);
},
);
await using host = new Neovim(reader, writer);
await t.step(
"'invoke' message before init throws error",
async () => {
await assertRejects(
() =>
host.call(
"denops#_internal#test#request",
"invoke",
["reload", ["dummy"]],
),
Error,
"Failed to call",
);
},
);

await t.step("init() calls Service.bind()", async () => {
const s = stub(service, "bind");
try {
await host.init(service);
assertSpyCall(s, 0, { args: [host] });
} finally {
s.restore();
}
});
await t.step("init() calls Service.bind()", async () => {
const s = stub(service, "bind");
try {
await host.init(service);
assertSpyCall(s, 0, { args: [host] });
} finally {
s.restore();
}
});

await t.step("redraw() does nothing", async () => {
await host.redraw();
});
await t.step("redraw() does nothing", async () => {
await host.redraw();
});

await t.step("call() returns a result of the function", async () => {
const result = await host.call("abs", -4);
assertEquals(result, 4);
});
await t.step("call() returns a result of the function", async () => {
const result = await host.call("abs", -4);
assertEquals(result, 4);
});

await t.step(
"call() throws an error when failed to call the function",
async () => {
await assertRejects(
() => host.call("@@@@@", -4),
Error,
"Failed to call @@@@@(-4): Vim:E117: Unknown function: @@@@@ (code: 0)",
);
},
await t.step(
"call() throws an error when failed to call the function",
async () => {
await assertRejects(
() => host.call("@@@@@", -4),
Error,
"Failed to call @@@@@(-4): Vim:E117: Unknown function: @@@@@ (code: 0)",
);
},
);

await t.step("batch() returns results of the functions", async () => {
const [ret, err] = await host.batch(
["abs", -4],
["abs", 10],
["abs", -9],
);
assertEquals(ret, [4, 10, 9]);
assertEquals(err, "");
});

await t.step("batch() returns results of the functions", async () => {
await t.step(
"batch() returns resutls with an error when failed to call the function",
async () => {
const [ret, err] = await host.batch(
["abs", -4],
["abs", 10],
["abs", -9],
["@@@@@", -9],
);
assertEquals(ret, [4, 10, 9]);
assertEquals(err, "");
});
assertEquals(ret, [4, 10]);
assertMatch(
err,
/Failed to call @@@@@\(-9\): Vim:E117: Unknown function: @@@@@/,
);
},
);

await t.step(
"batch() returns resutls with an error when failed to call the function",
async () => {
const [ret, err] = await host.batch(
["abs", -4],
["abs", 10],
["@@@@@", -9],
);
assertEquals(ret, [4, 10]);
assertMatch(
err,
/Failed to call @@@@@\(-9\): Vim:E117: Unknown function: @@@@@/,
);
},
);
await t.step("notify() calls the function", () => {
host.notify("abs", -4);
host.notify("@@@@@", -4); // should not throw
});

await t.step("notify() calls the function", () => {
host.notify("abs", -4);
host.notify("@@@@@", -4); // should not throw
});
await t.step(
"'void' message does nothing",
async () => {
await host.call(
"denops#_internal#test#request",
"void",
[],
);
},
);

await t.step(
"'void' message does nothing",
async () => {
await t.step(
"'invoke' message calls Service method",
async () => {
const s = stub(service, "reload");
try {
await host.call(
"denops#_internal#test#request",
"void",
[],
"invoke",
["reload", ["dummy"]],
);
},
);

await t.step(
"'invoke' message calls Service method",
async () => {
const s = stub(service, "reload");
try {
await host.call(
"denops#_internal#test#request",
"invoke",
["reload", ["dummy"]],
);
assertSpyCall(s, 0, { args: ["dummy"] });
} finally {
s.restore();
}
},
);
assertSpyCall(s, 0, { args: ["dummy"] });
} finally {
s.restore();
}
},
);

await t.step(
"'nvim_error_event' message shows error message",
async () => {
const s = stub(console, "error");
try {
await host.call(
"denops#_internal#test#request",
"nvim_error_event",
[0, "message"],
);
assertSpyCall(s, 0, { args: ["nvim_error_event(0)", "message"] });
} finally {
s.restore();
}
},
);
await t.step(
"'nvim_error_event' message shows error message",
async () => {
const s = stub(console, "error");
try {
await host.call(
"denops#_internal#test#request",
"nvim_error_event",
[0, "message"],
);
assertSpyCall(s, 0, { args: ["nvim_error_event(0)", "message"] });
} finally {
s.restore();
}
},
);

await t.step(
"waitClosed() returns a promise that is pending when the session is not closed",
async () => {
waitClosed = host.waitClosed();
assertEquals(await promiseState(waitClosed), "pending");
},
);
});
await t.step(
"waitClosed() returns a promise that is pending when the session is not closed",
async () => {
waitClosed = host.waitClosed();
assertEquals(await promiseState(waitClosed), "pending");
},
);
},
});
await t.step(
Expand Down
2 changes: 1 addition & 1 deletion denops/@denops-private/host/vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Vim implements Host {
return this.#session.wait();
}

async dispose(): Promise<void> {
async [Symbol.asyncDispose](): Promise<void> {
try {
await this.#session.shutdown();
} catch {
Expand Down
Loading

0 comments on commit 2d6c0a1

Please sign in to comment.