robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local assert_alive =n . assert_alive 7local clear =n . clear 8local feed =n . feed 9local eq =t . eq 10local exec_lua =n . exec_lua 11local next_msg =n . next_msg 12local NIL =vim . NIL 13local pcall_err =t . pcall_err 14 15describe ( 'thread' , function () 16local screen 17 18before_each ( function () 19clear () 20screen =Screen . new ( 50 , 10 ) 21end ) 22 23it ( 'non-string error()' , function () 24exec_lua [[ 25local thread = vim.uv.new_thread(function() 26error() 27end) 28vim.uv.thread_join(thread) 29]] 30 31screen : expect ( [[ 32| 33{1:~ }|*5 34{3: }| 35{9:Luv thread:} | 36{9:nil} | 37{6:Press ENTER or type command to continue}^ | 38]] ) 39feed ( '<cr>' ) 40assert_alive () 41end ) 42 43it ( 'entry func is executed in protected mode' , function () 44exec_lua [[ 45local thread = vim.uv.new_thread(function() 46error('Error in thread entry func') 47end) 48vim.uv.thread_join(thread) 49]] 50 51screen : expect ( [[ 52| 53{1:~ }|*5 54{3: }| 55{9:Luv thread:} | 56{9:[string "<nvim>"]:2: Error in thread entry func} | 57{6:Press ENTER or type command to continue}^ | 58]] ) 59feed ( '<cr>' ) 60assert_alive () 61end ) 62 63it ( 'callback is executed in protected mode' , function () 64exec_lua [[ 65local thread = vim.uv.new_thread(function() 66local timer = vim.uv.new_timer() 67local function ontimeout() 68timer:stop() 69timer:close() 70error('Error in thread callback') 71end 72timer:start(10, 0, ontimeout) 73vim.uv.run() 74end) 75vim.uv.thread_join(thread) 76]] 77 78screen : expect ( [[ 79| 80{1:~ }|*5 81{3: }| 82{9:Luv callback, thread:} | 83{9:[string "<nvim>"]:6: Error in thread callback} | 84{6:Press ENTER or type command to continue}^ | 85]] ) 86feed ( '<cr>' ) 87assert_alive () 88end ) 89 90describe ( 'print' , function () 91it ( 'works' , function () 92exec_lua [[ 93local thread = vim.uv.new_thread(function() 94print('print in thread') 95end) 96vim.uv.thread_join(thread) 97]] 98 99screen : expect ( [[ 100^ | 101{1:~ }|*8 102print in thread | 103]] ) 104end ) 105 106it ( 'vim.inspect' , function () 107exec_lua [[ 108local thread = vim.uv.new_thread(function() 109print(vim.inspect({1,2})) 110end) 111vim.uv.thread_join(thread) 112]] 113 114screen : expect ( [[ 115^ | 116{1:~ }|*8 117{ 1, 2 } | 118]] ) 119end ) 120end ) 121 122describe ( 'vim.*' , function () 123before_each ( function () 124clear () 125exec_lua [[ 126Thread_Test = {} 127 128Thread_Test.entry_func = function(async, entry_str, args) 129local decoded_args = vim.mpack.decode(args) 130assert(loadstring(entry_str))(async, decoded_args) 131end 132 133function Thread_Test:do_test() 134local async 135local on_async = self.on_async 136async = vim.uv.new_async(function(ret) 137on_async(ret) 138async:close() 139end) 140local thread = 141vim.uv.new_thread(self.entry_func, async, self.entry_str, self.args) 142vim.uv.thread_join(thread) 143end 144 145Thread_Test.new = function(entry, on_async, ...) 146self = {} 147setmetatable(self, {__index = Thread_Test}) 148self.args = vim.mpack.encode({...}) 149self.entry_str = string.dump(entry) 150self.on_async = on_async 151return self 152end 153]] 154end ) 155 156it ( 'is_thread' , function () 157exec_lua [[ 158local entry = function(async) 159async:send(vim.is_thread()) 160end 161local on_async = function(ret) 162vim.rpcnotify(1, 'result', ret) 163end 164local thread_test = Thread_Test.new(entry, on_async) 165thread_test:do_test() 166]] 167 168eq ( { 'notification' , 'result' , { true } } , next_msg ()) 169end ) 170 171it ( 'uv' , function () 172exec_lua [[ 173local entry = function(async) 174async:send(vim.uv.version()) 175end 176local on_async = function(ret) 177vim.rpcnotify(1, ret) 178end 179local thread_test = Thread_Test.new(entry, on_async) 180thread_test:do_test() 181]] 182 183local msg =next_msg () 184eq ( 'notification' , msg [ 1 ]) 185assert ( tonumber ( msg [ 2 ]) >= 72961 ) 186end ) 187 188it ( 'mpack' , function () 189exec_lua [[ 190local entry = function(async) 191async:send(vim.mpack.encode({33, vim.NIL, 'text'})) 192end 193local on_async = function(ret) 194vim.rpcnotify(1, 'result', vim.mpack.decode(ret)) 195end 196local thread_test = Thread_Test.new(entry, on_async) 197thread_test:do_test() 198]] 199 200eq ( { 'notification' , 'result' , { { 33 , NIL , 'text' } } } , next_msg ()) 201end ) 202 203it ( 'json' , function () 204exec_lua [[ 205local entry = function(async) 206async:send(vim.json.encode({33, vim.NIL, 'text'})) 207end 208local on_async = function(ret) 209vim.rpcnotify(1, 'result', vim.json.decode(ret)) 210end 211local thread_test = Thread_Test.new(entry, on_async) 212thread_test:do_test() 213]] 214 215eq ( { 'notification' , 'result' , { { 33 , NIL , 'text' } } } , next_msg ()) 216end ) 217 218it ( 'diff' , function () 219exec_lua [[ 220local entry = function(async) 221async:send(vim.diff('Hello\n', 'Helli\n')) 222end 223local on_async = function(ret) 224vim.rpcnotify(1, 'result', ret) 225end 226local thread_test = Thread_Test.new(entry, on_async) 227thread_test:do_test() 228]] 229 230eq ( { 231'notification' , 232'result' , 233{ 234table . concat ( { 235'@@ -1 +1 @@' , 236'-Hello' , 237'+Helli' , 238'' , 239} , '\n' ), 240} , 241} , next_msg ()) 242end ) 243end ) 244end ) 245 246describe ( 'threadpool' , function () 247before_each ( clear ) 248 249it ( 'is_thread' , function () 250eq ( false , exec_lua [[return vim.is_thread()]] ) 251 252exec_lua [[ 253local work_fn = function() 254return vim.is_thread() 255end 256local after_work_fn = function(ret) 257vim.rpcnotify(1, 'result', ret) 258end 259local work = vim.uv.new_work(work_fn, after_work_fn) 260work:queue() 261]] 262 263eq ( { 'notification' , 'result' , { true } } , next_msg ()) 264end ) 265 266it ( 'with invalid argument' , function () 267local status =pcall_err ( 268exec_lua , 269[[ 270local work = vim.uv.new_thread(function() end, function() end) 271work:queue({}) 272]] 273) 274 275eq ( [[Error: thread arg not support type 'function' at 1]] , status ) 276end ) 277 278it ( 'with invalid return value' , function () 279local screen =Screen . new ( 50 , 10 ) 280 281exec_lua [[ 282local work = vim.uv.new_work(function() return {} end, function() end) 283work:queue() 284]] 285 286screen : expect ( [[ 287| 288{1:~ }|*5 289{3: }| 290{9:Luv thread:} | 291{9:Error: thread arg not support type 'table' at 1} | 292{6:Press ENTER or type command to continue}^ | 293]] ) 294end ) 295 296describe ( 'vim.*' , function () 297before_each ( function () 298clear () 299exec_lua [[ 300Threadpool_Test = {} 301 302Threadpool_Test.work_fn = function(work_fn_str, args) 303local decoded_args = vim.mpack.decode(args) 304return assert(loadstring(work_fn_str))(decoded_args) 305end 306 307function Threadpool_Test:do_test() 308local work = 309vim.uv.new_work(self.work_fn, self.after_work) 310work:queue(self.work_fn_str, self.args) 311end 312 313Threadpool_Test.new = function(work_fn, after_work, ...) 314self = {} 315setmetatable(self, {__index = Threadpool_Test}) 316self.args = vim.mpack.encode({...}) 317self.work_fn_str = string.dump(work_fn) 318self.after_work = after_work 319return self 320end 321]] 322end ) 323 324it ( 'uv' , function () 325exec_lua [[ 326local work_fn = function() 327return vim.uv.version() 328end 329local after_work_fn = function(ret) 330vim.rpcnotify(1, ret) 331end 332local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn) 333threadpool_test:do_test() 334]] 335 336local msg =next_msg () 337eq ( 'notification' , msg [ 1 ]) 338assert ( tonumber ( msg [ 2 ]) >= 72961 ) 339end ) 340 341it ( 'mpack' , function () 342exec_lua [[ 343local work_fn = function() 344local var = vim.mpack.encode({33, vim.NIL, 'text'}) 345return var 346end 347local after_work_fn = function(ret) 348vim.rpcnotify(1, 'result', vim.mpack.decode(ret)) 349end 350local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn) 351threadpool_test:do_test() 352]] 353 354eq ( { 'notification' , 'result' , { { 33 , NIL , 'text' } } } , next_msg ()) 355end ) 356 357it ( 'json' , function () 358exec_lua [[ 359local work_fn = function() 360local var = vim.json.encode({33, vim.NIL, 'text'}) 361return var 362end 363local after_work_fn = function(ret) 364vim.rpcnotify(1, 'result', vim.json.decode(ret)) 365end 366local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn) 367threadpool_test:do_test() 368]] 369 370eq ( { 'notification' , 'result' , { { 33 , NIL , 'text' } } } , next_msg ()) 371end ) 372 373it ( 'work' , function () 374exec_lua [[ 375local work_fn = function() 376return vim.diff('Hello\n', 'Helli\n') 377end 378local after_work_fn = function(ret) 379vim.rpcnotify(1, 'result', ret) 380end 381local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn) 382threadpool_test:do_test() 383]] 384 385eq ( { 386'notification' , 387'result' , 388{ 389table . concat ( { 390'@@ -1 +1 @@' , 391'-Hello' , 392'+Helli' , 393'' , 394} , '\n' ), 395} , 396} , next_msg ()) 397end ) 398end ) 399end )