Skip to content

Commit 59281d5

Browse files
authored
Merge pull request #12 from vim-denops/fix-batch-error
Refactor and add tests for `BatchError`
2 parents c4b41ce + bad30fe commit 59281d5

File tree

5 files changed

+80
-29
lines changed

5 files changed

+80
-29
lines changed

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"update:commit": "deno task -q update --commit --pre-commit=fmt,lint"
1212
},
1313
"imports": {
14+
"@std/assert": "jsr:@std/assert@^1.0.1",
1415
"jsr:@denops/core": "./mod.ts"
1516
}
1617
}

errors.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This module provides error classes for [denops.vim].
3+
*
4+
* [denops.vim]: https://github.com/vim-denops/denops.vim
5+
*
6+
* @module
7+
*/
8+
9+
/**
10+
* Batch error raised when one of the functions fails during batch process.
11+
*/
12+
export class BatchError extends Error {
13+
static {
14+
this.prototype.name = "BatchError";
15+
}
16+
17+
/**
18+
* A result list that is successfully completed prior to the error.
19+
*/
20+
readonly results: unknown[];
21+
22+
constructor(message: string, results: unknown[]) {
23+
super(message);
24+
25+
this.results = results;
26+
}
27+
}

errors_test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
assert,
3+
assertEquals,
4+
assertInstanceOf,
5+
assertMatch,
6+
} from "@std/assert";
7+
8+
import { BatchError } from "./errors.ts";
9+
10+
Deno.test("BatchError", async (t) => {
11+
await t.step(".constructor()", async (t) => {
12+
await t.step("constructs an instance", () => {
13+
const actual = new BatchError("foo", ["bar", 1, true]);
14+
assertInstanceOf(actual, BatchError);
15+
});
16+
});
17+
await t.step(".name getter", async (t) => {
18+
await t.step("returns 'BatchError'", () => {
19+
const actual = new BatchError("foo", ["bar", 1, true]);
20+
assertEquals(actual.name, "BatchError");
21+
});
22+
});
23+
await t.step(".message getter", async (t) => {
24+
await t.step("returns an error message", () => {
25+
const actual = new BatchError("foo", ["bar", 1, true]);
26+
assertEquals(actual.message, "foo");
27+
});
28+
});
29+
await t.step(".stack getter", async (t) => {
30+
await t.step("returns an error stack trace", () => {
31+
const actual = new BatchError("foo", ["bar", 1, true]);
32+
assert(actual.stack);
33+
assertMatch(actual.stack, /\bat .*errors_test\.ts:\d+:\d+\n/);
34+
});
35+
});
36+
await t.step(".results getter", async (t) => {
37+
await t.step("returns a results array", () => {
38+
const actual = new BatchError("foo", ["bar", 1, true]);
39+
assertEquals(actual.results, ["bar", 1, true]);
40+
});
41+
});
42+
});

mod.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,5 @@
2323
* @module
2424
*/
2525

26-
export { BatchError } from "./denops.ts";
27-
export type {
28-
Context,
29-
Denops,
30-
Dispatcher,
31-
Entrypoint,
32-
Meta,
33-
} from "./denops.ts";
26+
export * from "./errors.ts";
27+
export type * from "./types.ts";

denops.ts renamed to types.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* This module provides type interfaces for [denops.vim].
3+
*
4+
* [denops.vim]: https://github.com/vim-denops/denops.vim
5+
*
6+
* @module
7+
*/
8+
19
/**
210
* API dispatcher
311
*/
@@ -34,27 +42,6 @@ export interface Meta {
3442
readonly platform: "windows" | "mac" | "linux";
3543
}
3644

37-
/**
38-
* Batch error raised when one of the functions fails during batch process.
39-
*/
40-
export class BatchError extends Error {
41-
/**
42-
* A result list that is successfully completed prior to the error.
43-
*/
44-
readonly results: unknown[];
45-
46-
constructor(message: string, results: unknown[]) {
47-
super(message);
48-
49-
if (Error.captureStackTrace) {
50-
Error.captureStackTrace(this, BatchError);
51-
}
52-
53-
this.name = this.constructor.name;
54-
this.results = results;
55-
}
56-
}
57-
5845
/**
5946
* Denops is a facade instance visible from each denops plugin.
6047
*/

0 commit comments

Comments
 (0)