From ef8701610baa18ecf2568990eab4ecf02ca8f6c1 Mon Sep 17 00:00:00 2001 From: Michael Ennen Date: Mon, 19 Dec 2016 20:09:07 -0700 Subject: Allow lambdas to be used with jobs, timers and dictwatchers. --- test/functional/core/job_spec.lua | 21 +++++++++++++++++ .../functional/ex_cmds/dict_notifications_spec.lua | 26 +++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) (limited to 'test/functional') diff --git a/test/functional/core/job_spec.lua b/test/functional/core/job_spec.lua index 6e9633465f..b15af6b9ee 100644 --- a/test/functional/core/job_spec.lua +++ b/test/functional/core/job_spec.lua @@ -391,6 +391,27 @@ describe('jobs', function() eq({'notification', '1', {'foo', 'bar', {'some text', ''}, 'stdout'}}, next_msg()) end) + it('jobstart() works with closures', function() + source([[ + fun! MkFun() + let a1 = 'foo' + let a2 = 'bar' + return {id, data, event -> rpcnotify(g:channel, '1', a1, a2, data, event)} + endfun + let g:job_opts = {'on_stdout': MkFun()} + call jobstart(['echo'], g:job_opts) + ]]) + eq({'notification', '1', {'foo', 'bar', {'', ''}, 'stdout'}}, next_msg()) + end) + + it('jobstart() works when closure passed directly to `jobstart`', function() + source([[ + let g:job_opts = {'on_stdout': {id, data, event -> rpcnotify(g:channel, '1', 'foo', 'bar', data, event)}} + call jobstart(['echo'], g:job_opts) + ]]) + eq({'notification', '1', {'foo', 'bar', {'', ''}, 'stdout'}}, next_msg()) + end) + describe('jobwait', function() it('returns a list of status codes', function() source([[ diff --git a/test/functional/ex_cmds/dict_notifications_spec.lua b/test/functional/ex_cmds/dict_notifications_spec.lua index 5e89986c0f..30753c34ac 100644 --- a/test/functional/ex_cmds/dict_notifications_spec.lua +++ b/test/functional/ex_cmds/dict_notifications_spec.lua @@ -254,23 +254,33 @@ describe('dictionary change notifications', function() command('call g:ReplaceWatcher2()') command('let g:key = "value"') eq({'notification', '2b', {'key', {old = 'v2', new = 'value'}}}, next_msg()) - end) it('does not crash when freeing a watched dictionary', function() source([[ - function! Watcher(dict, key, value) - echo a:key string(a:value) - endfunction + function! Watcher(dict, key, value) + echo a:key string(a:value) + endfunction - function! MakeWatch() - let d = {'foo': 'bar'} - call dictwatcheradd(d, 'foo', function('Watcher')) - endfunction + function! MakeWatch() + let d = {'foo': 'bar'} + call dictwatcheradd(d, 'foo', function('Watcher')) + endfunction ]]) command('call MakeWatch()') eq(2, eval('1+1')) -- Still alive? end) end) + + describe('with lambdas', function() + it('works correctly', function() + source([[ + let d = {'foo': 'baz'} + call dictwatcheradd(d, 'foo', {dict, key, value -> rpcnotify(g:channel, '2', key, value)}) + let d.foo = 'bar' + ]]) + eq({'notification', '2', {'foo', {old = 'baz', new = 'bar'}}}, next_msg()) + end) + end) end) -- cgit