comparison src/testdir/test_listener.vim @ 16648:a7f06505ad39 v8.1.1326

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