-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtester.ts
161 lines (157 loc) · 4.57 KB
/
tester.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { sample } from "jsr:@std/collections@^1.0.5/sample";
import type { Denops } from "jsr:@denops/core@^7.0.0";
import type { RunMode } from "./runner.ts";
import { withDenops } from "./with.ts";
/**
* Represents the running mode for tests.
*/
export type TestMode = RunMode | "any" | "all";
/** Represents a test definition used in the `test` function. */
export interface TestDefinition extends Omit<Deno.TestDefinition, "fn"> {
fn: (denops: Denops, t: Deno.TestContext) => void | Promise<void>;
/**
* Test runner mode.
*
* - Specifying "vim" or "nvim" will run the test with the specified runner.
* - If "any" is specified, Vim or Neovim is randomly selected and executed.
* - When "all" is specified, the test is run with both Vim and Neovim.
*/
mode: TestMode;
/** The plugin name of the test target. */
pluginName?: string;
/** Prints Vim messages (echomsg). */
verbose?: boolean;
/** Vim commands to be executed before the start of Denops. */
prelude?: string[];
/** Vim commands to be executed after the start of Denops. */
postlude?: string[];
}
/**
* Registers a test for Denops to be run when `deno test` is used.
*
* To use this function, the environment variable `DENOPS_TEST_DENOPS_PATH` must be set to the
* local path to the `denops.vim` repository.
*
* The `DENOPS_TEST_VIM_EXECUTABLE` and `DENOPS_TEST_NVIM_EXECUTABLE` environment variables
* allow you to change the Vim/Neovim execution command (default is `vim` and `nvim` respectively).
*
* Note that this is a time-consuming process, especially on Windows, since this function
* internally spawns Vim/Neovim sub-process, which performs the tests.
*
* This function internally uses `Deno.test` and `withDenops` to run
* tests by passing a `denops` instance to the registered test function.
*
* ```ts
* import { assert, assertFalse } from "jsr:@std/assert";
* import { test } from "jsr:@denops/test";
*
* test("vim", "Test with Vim", async (denops) => {
* assertFalse(await denops.call("has", "nvim"));
* });
* ```
*/
export function test(
mode: TestDefinition["mode"],
name: string,
fn: TestDefinition["fn"],
): void;
/**
* Registers a test for Denops to be run when `deno test` is used.
*
* To use this function, the environment variable `DENOPS_TEST_DENOPS_PATH` must be set to the
* local path to the `denops.vim` repository.
*
* The `DENOPS_TEST_VIM_EXECUTABLE` and `DENOPS_TEST_NVIM_EXECUTABLE` environment variables
* allow you to change the Vim/Neovim execution command (default is `vim` and `nvim` respectively).
*
* Note that this is a time-consuming process, especially on Windows, since this function
* internally spawns Vim/Neovim sub-process, which performs the tests.
*
* This function internally uses `Deno.test` and `withDenops` to run
* tests by passing a `denops` instance to the registered test function.
*
* ```ts
* import { assert, assertFalse } from "jsr:@std/assert";
* import { test } from "jsr:@denops/test";
*
* test({
* mode: "nvim",
* name: "Test with Neovim",
* fn: async (denops) => {
* assert(await denops.call("has", "nvim"));
* },
* });
* ```
*/
export function test(def: TestDefinition): void;
export function test(
modeOrDefinition: TestDefinition["mode"] | TestDefinition,
name?: string,
fn?: TestDefinition["fn"],
): void {
if (typeof modeOrDefinition === "string") {
testInternal({
mode: modeOrDefinition,
name,
fn,
});
} else {
testInternal(modeOrDefinition);
}
}
function testInternal(def: Partial<TestDefinition>): void {
const {
mode,
name,
fn,
pluginName,
verbose,
prelude,
postlude,
...denoTestDef
} = def;
if (!mode) {
throw new Error("'mode' attribute is required");
}
if (!name) {
throw new Error("'name' attribute is required");
}
if (!fn) {
throw new Error("'fn' attribute is required");
}
if (mode === "all") {
testInternal({
...def,
name: `${name} (vim)`,
mode: "vim",
});
testInternal({
...def,
name: `${name} (nvim)`,
mode: "nvim",
});
} else if (mode === "any") {
const m = sample(["vim", "nvim"] as const)!;
testInternal({
...def,
name: `${name} (${m})`,
mode: m,
});
} else {
if (!["vim", "nvim"].includes(mode)) {
throw new Error(`'mode' attribute is invalid: ${mode}`);
}
Deno.test({
...denoTestDef,
name,
fn: (t) => {
return withDenops(mode, (denops) => fn.call(def, denops, t), {
pluginName,
verbose,
prelude,
postlude,
});
},
});
}
}