comparison src/testdir/test_listener.vim @ 16638:4790302965fc v8.1.1321

patch 8.1.1321: no docs or tests for listener functions commit https://github.com/vim/vim/commit/a334772967de25764ed7b11d768e8b977818d0c6 Author: Bram Moolenaar <Bram@vim.org> Date: Sat May 11 21:14:24 2019 +0200 patch 8.1.1321: no docs or tests for listener functions Problem: No docs or tests for listener functions. Solution: Add help and tests for listener_add() and listener_remove(). Invoke the callbacks before redrawing.
author Bram Moolenaar <Bram@vim.org>
date Sat, 11 May 2019 21:15:06 +0200
parents
children a7f06505ad39
comparison
equal deleted inserted replaced
16637:511e89374819 16638:4790302965fc
1 " tests for listener_add() and listener_remove()
2
3 func StoreList(l)
4 let g:list = a:l
5 endfunc
6
7 func AnotherStoreList(l)
8 let g:list2 = a:l
9 endfunc
10
11 func EvilStoreList(l)
12 let g:list3 = a:l
13 call assert_fails("call add(a:l, 'myitem')", "E742:")
14 endfunc
15
16 func Test_listening()
17 new
18 call setline(1, ['one', 'two'])
19 let id = listener_add({l -> StoreList(l)})
20 call setline(1, 'one one')
21 redraw
22 call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list)
23
24 " Two listeners, both get called.
25 let id2 = listener_add({l -> AnotherStoreList(l)})
26 let g:list = []
27 let g:list2 = []
28 exe "normal $asome\<Esc>"
29 redraw
30 call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], g:list)
31 call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], g:list2)
32
33 call listener_remove(id2)
34 let g:list = []
35 let g:list2 = []
36 call setline(3, 'three')
37 redraw
38 call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}], g:list)
39 call assert_equal([], g:list2)
40
41 " the "o" command first adds an empty line and then changes it
42 let g:list = []
43 exe "normal Gofour\<Esc>"
44 redraw
45 call assert_equal([{'lnum': 4, 'end': 4, 'col': 1, 'added': 1},
46 \ {'lnum': 4, 'end': 5, 'col': 1, 'added': 0}], g:list)
47
48 let g:list = []
49 call listener_remove(id)
50 call setline(1, 'asdfasdf')
51 redraw
52 call assert_equal([], g:list)
53
54 " Trying to change the list fails
55 let id = listener_add({l -> EvilStoreList(l)})
56 let g:list3 = []
57 call setline(1, 'asdfasdf')
58 redraw
59 call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list3)
60
61 bwipe!
62 endfunc
63
64 func Test_listening_other_buf()
65 new
66 call setline(1, ['one', 'two'])
67 let bufnr = bufnr('')
68 normal ww
69 let id = listener_add({l -> StoreList(l)}, bufnr)
70 let g:list = []
71 call setbufline(bufnr, 1, 'hello')
72 redraw
73 call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list)
74
75 exe "buf " .. bufnr
76 bwipe!
77 endfunc