Skip to content

Commit 9fb0d64

Browse files
committed
🌿 improve tests
1 parent 007d372 commit 9fb0d64

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

eval/stringify_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { assertSpyCallArgs, assertSpyCalls, spy } from "@std/testing/mock";
33
import { test } from "@denops/test";
44
import { expr } from "./expression.ts";
55
import { rawString } from "./string.ts";
6+
import { type VimEvaluatable, vimExpressionOf } from "./vim_evaluatable.ts";
7+
import { exprQuote } from "../helper/expr_string.ts";
68

79
import { stringify } from "./stringify.ts";
810

@@ -86,6 +88,10 @@ Deno.test("stringify()", async (t) => {
8688
const actual = stringify(rawString`\<Cmd>call Foo("bar")\<CR>`);
8789
assertEquals(actual, '"\\<Cmd>call Foo(\\"bar\\")\\<CR>"');
8890
});
91+
await t.step("stringify ExprString to Vim's expr-string", () => {
92+
const actual = stringify(exprQuote`\<Cmd>call Foo("bar")\<CR>`);
93+
assertEquals(actual, '"\\<Cmd>call Foo(\\"bar\\")\\<CR>"');
94+
});
8995
await t.step("stringify array to Vim's list", () => {
9096
const actual = stringify(["foo", 42, null, undefined]);
9197
assertEquals(actual, "['foo',42,v:null,v:null]");
@@ -153,6 +159,27 @@ Deno.test("stringify()", async (t) => {
153159
assertSpyCallArgs(s1, 0, ["a"]);
154160
assertSpyCalls(s2, 0);
155161
});
162+
await t.step("stringify Expression that returns from `toJSON`", () => {
163+
const x = {
164+
toJSON: () => expr`feedkeys("\<Cmd>call Foo(\"bar\")\<CR>")`,
165+
};
166+
const actual = stringify(x);
167+
assertEquals(actual, 'feedkeys("\\<Cmd>call Foo(\\"bar\\")\\<CR>")');
168+
});
169+
await t.step("stringify RawString that returns from `toJSON`", () => {
170+
const x = {
171+
toJSON: () => rawString`\<Cmd>call Foo("bar")\<CR>`,
172+
};
173+
const actual = stringify(x);
174+
assertEquals(actual, '"\\<Cmd>call Foo(\\"bar\\")\\<CR>"');
175+
});
176+
await t.step("stringify ExprString that returns from `toJSON`", () => {
177+
const x = {
178+
toJSON: () => exprQuote`\<Cmd>call Foo("bar")\<CR>`,
179+
};
180+
const actual = stringify(x);
181+
assertEquals(actual, '"\\<Cmd>call Foo(\\"bar\\")\\<CR>"');
182+
});
156183
await t.step("stringify object that has `toJSON` method", () => {
157184
const actual = stringify({
158185
foo: 42,
@@ -234,6 +261,19 @@ Deno.test("stringify()", async (t) => {
234261
`[v:null,v:null,42,v:true,v:null,v:null,'bar',[123,'baz'],'2007-08-31T12:34:56.000Z',{'k0':v:null,'k2':[{'key':234,'expr':foo#bar(),'rstr':"\\U0001F680"}],'k3':0z01ff007f}]`,
235262
);
236263
});
264+
await t.step("throws if @@vimExpressionOf() returns not a string", () => {
265+
const invalid: VimEvaluatable = {
266+
[vimExpressionOf](): string {
267+
return 123 as unknown as string;
268+
},
269+
};
270+
const x = { invalid };
271+
assertThrows(
272+
() => stringify(x),
273+
TypeError,
274+
"@@vimExpressionOf() returns not a string",
275+
);
276+
});
237277
await t.step("throws if value contains circular structure array", () => {
238278
const z: unknown[] = [];
239279
const x = [[z]];

0 commit comments

Comments
 (0)