Skip to content

Commit

Permalink
test: improve denops_test handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed Nov 28, 2023
1 parent 40f777a commit 893a226
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 43 deletions.
1 change: 1 addition & 0 deletions denops/skkeleton/deps/denops_test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type { TestDefinition } from "https://deno.land/x/[email protected]/tester.ts";
export { test } from "https://deno.land/x/[email protected]/tester.ts";
6 changes: 1 addition & 5 deletions denops/skkeleton/function/disable_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Denops } from "../deps.ts";
import { test } from "../deps/denops_test.ts";
import { assertEquals } from "../deps/std/assert.ts";
import { currentContext } from "../store.ts";
import { initDenops } from "../testutil.ts";
import { test } from "../testutil.ts";
import { dispatch } from "./testutil.ts";

// deno-lint-ignore no-explicit-any
Expand All @@ -13,10 +12,7 @@ async function getResult(x: Promise<any>): Promise<string> {
test({
mode: "all",
name: "kakutei at disable",
pluginName: "skkeleton",
async fn(denops: Denops) {
await initDenops(denops);

await denops.dispatch("skkeleton", "enable");
await dispatch(currentContext.get(), " ");
assertEquals(
Expand Down
6 changes: 1 addition & 5 deletions denops/skkeleton/function/input_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { config } from "../config.ts";
import { Context } from "../context.ts";
import { Denops, op } from "../deps.ts";
import { test } from "../deps/denops_test.ts";
import { assertEquals } from "../deps/std/assert.ts";
import { initDenops } from "../testutil.ts";
import { test } from "../testutil.ts";
import { kakutei } from "./common.ts";
import { deleteChar, henkanPoint } from "./input.ts";
import { hankatakana, katakana } from "./mode.ts";
Expand Down Expand Up @@ -132,10 +131,7 @@ config.userJisyo = "";
test({
mode: "nvim",
name: "new line",
pluginName: "skkeleton",
async fn(denops: Denops) {
await initDenops(denops);

await op.autoindent.setLocal(denops, true);
await denops.cmd("startinsert");
await denops.cmd(
Expand Down
6 changes: 2 additions & 4 deletions denops/skkeleton/function/jisyo_test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Denops } from "../deps.ts";
import { test } from "../deps/denops_test.ts";
import { assertEquals } from "../deps/std/assert.ts";
import { currentKanaTable, getKanaTable } from "../kana.ts";
import { currentContext } from "../store.ts";
import { HenkanState } from "../state.ts";
import { initDenops } from "../testutil.ts";
import { currentContext } from "../store.ts";
import { test } from "../testutil.ts";
import { jisyoTouroku } from "./jisyo.ts";

test({
mode: "all",
name: "Don't put string when register dictionary was cancelled",
async fn(denops: Denops) {
await initDenops(denops);
currentKanaTable.get();
const state: HenkanState = {
type: "henkan",
Expand Down
11 changes: 2 additions & 9 deletions denops/skkeleton/function/mode_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { autocmd, Denops, vars } from "../deps.ts";
import { test } from "../deps/denops_test.ts";
import { assertEquals } from "../deps/std/assert.ts";
import { currentLibrary } from "../store.ts";
import { currentKanaTable } from "../kana.ts";
import { currentLibrary } from "../store.ts";
import { currentContext } from "../store.ts";
import { initDenops } from "../testutil.ts";
import { test } from "../testutil.ts";
import { kakutei } from "./common.ts";
import { deleteChar, kanaInput } from "./input.ts";
import { abbrev, hankatakana, katakana, zenkaku } from "./mode.ts";
Expand All @@ -13,9 +12,7 @@ import { dispatch } from "./testutil.ts";
test({
mode: "all",
name: "Can get skkeleton mode",
pluginName: "skkeleton",
async fn(d: Denops) {
await initDenops(d);
assertEquals(await d.call("skkeleton#mode"), "");
await d.dispatch("skkeleton", "enable");
assertEquals(await d.call("skkeleton#mode"), "hira");
Expand All @@ -36,9 +33,7 @@ test({
test({
mode: "all",
name: "Fire autocmd for mode changed",
pluginName: "skkeleton",
async fn(d: Denops) {
await initDenops(d);
await autocmd.define(
d,
"User",
Expand Down Expand Up @@ -108,9 +103,7 @@ Deno.test({
test({
mode: "all",
name: "mode change at enable",
pluginName: "skkeleton",
async fn(d: Denops) {
await initDenops(d);
await autocmd.define(
d,
"User",
Expand Down
5 changes: 1 addition & 4 deletions denops/skkeleton/kana_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { config } from "./config.ts";
import { Context } from "./context.ts";
import { test } from "./deps/denops_test.ts";
import { assertEquals } from "./deps/std/assert.ts";
import { dispatch } from "./function/testutil.ts";
import { registerKanaTable } from "./kana.ts";
import { currentContext } from "./store.ts";
import { initDenops } from "./testutil.ts";
import { test } from "./testutil.ts";

Deno.test({
name: "customize kanatable",
Expand All @@ -28,13 +27,11 @@ Deno.test({
test({
mode: "all",
name: "create kanatable",
pluginName: "skkeleton",
async fn(denops) {
registerKanaTable("test", {
a: ["hoge", ""],
}, true);
config.kanaTable = "test";
await initDenops(denops);
await denops.call("skkeleton#request", "enable", []);
const context = currentContext.get();
await dispatch(context, "a");
Expand Down
7 changes: 1 addition & 6 deletions denops/skkeleton/keymap_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { test } from "./deps/denops_test.ts";
import { test } from "./testutil.ts";
import { assertEquals } from "./deps/std/assert.ts";
import { currentLibrary } from "./store.ts";
import { currentContext } from "./store.ts";
import { initDenops } from "./testutil.ts";

test({
mode: "nvim", // can input mode test only in nvim
name: "registerKeyMap",
pluginName: "skkeleton",
async fn(denops) {
await initDenops(denops);
const lib = await currentLibrary.get();
lib.registerHenkanResult("okurinasi", "あ", "亜");
await denops.cmd('call skkeleton#register_keymap("henkan", "x", "")');
Expand Down Expand Up @@ -59,9 +56,7 @@ test({
test({
mode: "all",
name: "send multiple keys into handleKey",
pluginName: "skkeleton",
async fn(denops) {
await initDenops(denops);
const lib = await currentLibrary.get();
lib.registerHenkanResult("okurinasi", "われ", "我");
lib.registerHenkanResult("okuriari", "おもu", "思");
Expand Down
36 changes: 26 additions & 10 deletions denops/skkeleton/testutil.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { fromFileUrl } from "./deps/std/path.ts";
import { autocmd, Denops } from "./deps.ts";
import * as DenopsTest from "./deps/denops_test.ts";
import * as path from "./deps/std/path.ts";
import { main } from "./main.ts";
import { currentContext } from "./store.ts";

export async function initDenops(denops: Denops): Promise<void> {
const cfile = fromFileUrl(new URL(import.meta.url));
const path = cfile.slice(0, cfile.lastIndexOf("denops"));
await denops.cmd(`set runtimepath^=${path.replaceAll("\\", "/")}`);
await denops.cmd("source " + path + "plugin/skkeleton.vim");
await main(denops);
await autocmd.emit(denops, "User", "DenopsSystemPluginPost:skkeleton", {
nomodeline: true,
const runtimepath = path.resolve(
path.fromFileUrl(new URL("../..", import.meta.url)),
);

// It is inspired from https://github.com/lambdalisue/gin.vim/blob/e737a4b59a9d391c49aaa07f7c4d91e4a29ae09c/denops/gin/util/testutil.ts
// Copyright 2021 Alisue <[email protected]>
export function test(def: DenopsTest.TestDefinition): void {
const fn = def.fn;
DenopsTest.test({
...def,
async fn(denops: Denops, t: Deno.TestContext) {
await main(denops);
await autocmd.emit(denops, "User", "DenopsSystemPluginPost:skkeleton", {
nomodeline: true,
});
currentContext.init().denops = denops;
await fn(denops, t);
},
pluginName: "skkeleton",
prelude: [
`set runtimepath^=${runtimepath}`,
"runtime! plugin/skkeleton.vim",
...(def.prelude ?? []),
],
});
currentContext.init().denops = denops;
}

0 comments on commit 893a226

Please sign in to comment.