@@ -3,6 +3,8 @@ import { assertSpyCallArgs, assertSpyCalls, spy } from "@std/testing/mock";
3
3
import { test } from "@denops/test" ;
4
4
import { expr } from "./expression.ts" ;
5
5
import { rawString } from "./string.ts" ;
6
+ import { type VimEvaluatable , vimExpressionOf } from "./vim_evaluatable.ts" ;
7
+ import { exprQuote } from "../helper/expr_string.ts" ;
6
8
7
9
import { stringify } from "./stringify.ts" ;
8
10
@@ -86,6 +88,10 @@ Deno.test("stringify()", async (t) => {
86
88
const actual = stringify ( rawString `\<Cmd>call Foo("bar")\<CR>` ) ;
87
89
assertEquals ( actual , '"\\<Cmd>call Foo(\\"bar\\")\\<CR>"' ) ;
88
90
} ) ;
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
+ } ) ;
89
95
await t . step ( "stringify array to Vim's list" , ( ) => {
90
96
const actual = stringify ( [ "foo" , 42 , null , undefined ] ) ;
91
97
assertEquals ( actual , "['foo',42,v:null,v:null]" ) ;
@@ -153,6 +159,27 @@ Deno.test("stringify()", async (t) => {
153
159
assertSpyCallArgs ( s1 , 0 , [ "a" ] ) ;
154
160
assertSpyCalls ( s2 , 0 ) ;
155
161
} ) ;
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
+ } ) ;
156
183
await t . step ( "stringify object that has `toJSON` method" , ( ) => {
157
184
const actual = stringify ( {
158
185
foo : 42 ,
@@ -234,6 +261,19 @@ Deno.test("stringify()", async (t) => {
234
261
`[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}]` ,
235
262
) ;
236
263
} ) ;
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
+ } ) ;
237
277
await t . step ( "throws if value contains circular structure array" , ( ) => {
238
278
const z : unknown [ ] = [ ] ;
239
279
const x = [ [ z ] ] ;
0 commit comments