Skip to content

Commit 9b472f4

Browse files
committed
Merge remote-tracking branch 'vim/master'
2 parents 1227fce + 39b2127 commit 9b472f4

9 files changed

Lines changed: 172 additions & 51 deletions

File tree

src/channel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ channel_get_json(int ch_idx, int id, typval_T **rettv)
811811

812812
if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
813813
|| (id <= 0 && (tv->v_type != VAR_NUMBER
814+
|| tv->vval.v_number == 0
814815
|| tv->vval.v_number != channel->ch_block_id)))
815816
{
816817
*rettv = item->value;

src/eval.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ static void f_delete(typval_T *argvars, typval_T *rettv);
532532
static void f_did_filetype(typval_T *argvars, typval_T *rettv);
533533
static void f_diff_filler(typval_T *argvars, typval_T *rettv);
534534
static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
535+
static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
535536
static void f_empty(typval_T *argvars, typval_T *rettv);
536537
static void f_escape(typval_T *argvars, typval_T *rettv);
537538
static void f_eval(typval_T *argvars, typval_T *rettv);
@@ -8111,6 +8112,7 @@ static struct fst
81118112
{"did_filetype", 0, 0, f_did_filetype},
81128113
{"diff_filler", 1, 1, f_diff_filler},
81138114
{"diff_hlID", 2, 2, f_diff_hlID},
8115+
{"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
81148116
{"empty", 1, 1, f_empty},
81158117
{"escape", 2, 2, f_escape},
81168118
{"eval", 1, 1, f_eval},
@@ -10605,6 +10607,15 @@ f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1060510607
#endif
1060610608
}
1060710609

10610+
/*
10611+
* "disable_char_avail_for_testing({expr})" function
10612+
*/
10613+
static void
10614+
f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10615+
{
10616+
disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10617+
}
10618+
1060810619
/*
1060910620
* "empty({expr})" function
1061010621
*/
@@ -12456,8 +12467,11 @@ getpos_both(
1245612467
#endif
1245712468
(varnumber_T)0);
1245812469
if (getcurpos)
12470+
{
12471+
update_curswant();
1245912472
list_append_number(l, curwin->w_curswant == MAXCOL ?
1246012473
(varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
12474+
}
1246112475
}
1246212476
else
1246312477
rettv->vval.v_number = FALSE;

src/getchar.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,12 @@ char_avail(void)
18881888
{
18891889
int retval;
18901890

1891+
#ifdef FEAT_EVAL
1892+
/* When disable_char_avail_for_testing(1) was called pretend there is no
1893+
* typeahead. */
1894+
if (disable_char_avail_for_testing)
1895+
return FALSE;
1896+
#endif
18911897
++no_mapping;
18921898
retval = vpeekc();
18931899
--no_mapping;

src/globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,8 @@ EXTERN alloc_id_T alloc_fail_id INIT(= aid_none);
16251625
EXTERN int alloc_fail_countdown INIT(= -1);
16261626
/* set by alloc_fail(), number of times alloc() returns NULL */
16271627
EXTERN int alloc_fail_repeat INIT(= 0);
1628+
1629+
EXTERN int disable_char_avail_for_testing INIT(= 0);
16281630
#endif
16291631

16301632
/*

src/testdir/README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ What you can use (see test_assert.vim for an example):
2525
to check memory allocation failures are handled gracefully. You need to
2626
change the source code to add an ID to the allocation. Update LAST_ID_USED
2727
above alloc_id() to the highest ID used.
28+
- Use disable_char_avail_for_testing(1) if char_avail() must return FALSE for
29+
a while. E.g. to trigger the CursorMovedI autocommand event.
30+
See test_cursor_func.vim for an example
2831

2932

3033
TO ADD AN OLD STYLE TEST:

src/testdir/test_channel.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def handle(self):
130130
elif decoded[1] == 'eval-result':
131131
# Send back the last received eval result.
132132
response = last_eval
133+
elif decoded[1] == 'call me':
134+
cmd = '[0,"we called you"]'
135+
print("sending: {}".format(cmd))
136+
self.request.sendall(cmd.encode('utf-8'))
137+
response = "ok"
138+
elif decoded[1] == 'call me again':
139+
cmd = '[0,"we did call you"]'
140+
print("sending: {}".format(cmd))
141+
self.request.sendall(cmd.encode('utf-8'))
142+
response = ""
133143
elif decoded[1] == '!quit!':
134144
# we're done
135145
self.server.shutdown()
@@ -140,9 +150,12 @@ def handle(self):
140150
else:
141151
response = "what?"
142152

143-
encoded = json.dumps([decoded[0], response])
144-
print("sending: {}".format(encoded))
145-
self.request.sendall(encoded.encode('utf-8'))
153+
if response == "":
154+
print("no response")
155+
else:
156+
encoded = json.dumps([decoded[0], response])
157+
print("sending: {}".format(encoded))
158+
self.request.sendall(encoded.encode('utf-8'))
146159

147160
# Negative numbers are used for "eval" responses.
148161
elif decoded[0] < 0:

src/testdir/test_channel.vim

Lines changed: 99 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,63 @@ elseif has('win32')
1919
finish
2020
endif
2121
else
22+
" Can't run this test.
2223
finish
2324
endif
2425

25-
let s:port = -1
2626
let s:chopt = has('macunix') ? {'waittime' : 1} : {}
2727

28-
func s:start_server()
28+
" Run "testfunc" after sarting the server and stop the server afterwards.
29+
func s:run_server(testfunc)
2930
" The Python program writes the port number in Xportnr.
3031
call delete("Xportnr")
3132

32-
if has('job')
33-
let s:job = job_start("python test_channel.py")
34-
elseif has('win32')
35-
silent !start cmd /c start "test_channel" py test_channel.py
36-
else
37-
silent !python test_channel.py&
38-
endif
33+
try
34+
if has('job')
35+
let s:job = job_start("python test_channel.py")
36+
elseif has('win32')
37+
silent !start cmd /c start "test_channel" py test_channel.py
38+
else
39+
silent !python test_channel.py&
40+
endif
3941

40-
" Wait for up to 2 seconds for the port number to be there.
41-
let cnt = 20
42-
let l = []
43-
while cnt > 0
44-
try
45-
let l = readfile("Xportnr")
46-
catch
47-
endtry
48-
if len(l) >= 1
49-
break
42+
" Wait for up to 2 seconds for the port number to be there.
43+
let cnt = 20
44+
let l = []
45+
while cnt > 0
46+
try
47+
let l = readfile("Xportnr")
48+
catch
49+
endtry
50+
if len(l) >= 1
51+
break
52+
endif
53+
sleep 100m
54+
let cnt -= 1
55+
endwhile
56+
call delete("Xportnr")
57+
58+
if len(l) == 0
59+
" Can't make the connection, give up.
60+
call assert_false(1, "Can't start test_channel.py")
61+
return -1
5062
endif
51-
sleep 100m
52-
let cnt -= 1
53-
endwhile
54-
call delete("Xportnr")
63+
let port = l[0]
5564

56-
if len(l) == 0
57-
" Can't make the connection, give up.
65+
call call(function(a:testfunc), [port])
66+
catch
67+
call assert_false(1, "Caught exception: " . v:exception)
68+
finally
5869
call s:kill_server()
59-
call assert_false(1, "Can't start test_channel.py")
60-
return -1
61-
endif
62-
let s:port = l[0]
63-
64-
let handle = ch_open('localhost:' . s:port, s:chopt)
65-
return handle
70+
endtry
6671
endfunc
6772

6873
func s:kill_server()
6974
if has('job')
70-
call job_stop(s:job)
75+
if exists('s:job')
76+
call job_stop(s:job)
77+
unlet s:job
78+
endif
7179
elseif has('win32')
7280
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
7381
else
@@ -82,9 +90,10 @@ func s:RequestHandler(handle, msg)
8290
let s:responseMsg = a:msg
8391
endfunc
8492

85-
func Test_communicate()
86-
let handle = s:start_server()
93+
func s:communicate(port)
94+
let handle = ch_open('localhost:' . a:port, s:chopt)
8795
if handle < 0
96+
call assert_false(1, "Can't open channel")
8897
return
8998
endif
9099

@@ -144,39 +153,84 @@ func Test_communicate()
144153

145154
" make the server quit, can't check if this works, should not hang.
146155
call ch_sendexpr(handle, '!quit!', 0)
156+
endfunc
147157

148-
call s:kill_server()
158+
func Test_communicate()
159+
call s:run_server('s:communicate')
149160
endfunc
150161

151162
" Test that we can open two channels.
152-
func Test_two_channels()
153-
let handle = s:start_server()
163+
func s:two_channels(port)
164+
let handle = ch_open('localhost:' . a:port, s:chopt)
154165
if handle < 0
166+
call assert_false(1, "Can't open channel")
155167
return
156168
endif
169+
157170
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
158171

159-
let newhandle = ch_open('localhost:' . s:port, s:chopt)
172+
let newhandle = ch_open('localhost:' . a:port, s:chopt)
173+
if newhandle < 0
174+
call assert_false(1, "Can't open second channel")
175+
return
176+
endif
160177
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
161178
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
162179

163180
call ch_close(handle)
164181
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
165182

166-
call s:kill_server()
183+
call ch_close(newhandle)
184+
endfunc
185+
186+
func Test_two_channels()
187+
call s:run_server('s:two_channels')
167188
endfunc
168189

169190
" Test that a server crash is handled gracefully.
170-
func Test_server_crash()
171-
let handle = s:start_server()
191+
func s:server_crash(port)
192+
let handle = ch_open('localhost:' . a:port, s:chopt)
172193
if handle < 0
194+
call assert_false(1, "Can't open channel")
173195
return
174196
endif
197+
175198
call ch_sendexpr(handle, '!crash!')
176199

177-
" kill the server in case if failed to crash
178200
sleep 10m
179-
call s:kill_server()
201+
endfunc
202+
203+
func Test_server_crash()
204+
call s:run_server('s:server_crash')
205+
endfunc
206+
207+
let s:reply = ""
208+
func s:Handler(chan, msg)
209+
let s:reply = a:msg
210+
endfunc
211+
212+
func s:channel_handler(port)
213+
let chopt = copy(s:chopt)
214+
let chopt['callback'] = 's:Handler'
215+
let handle = ch_open('localhost:' . a:port, chopt)
216+
if handle < 0
217+
call assert_false(1, "Can't open channel")
218+
return
219+
endif
220+
221+
" Test that it works while waiting on a numbered message.
222+
call assert_equal('ok', ch_sendexpr(handle, 'call me'))
223+
sleep 10m
224+
call assert_equal('we called you', s:reply)
225+
226+
" Test that it works while not waiting on a numbered message.
227+
call ch_sendexpr(handle, 'call me again', 0)
228+
sleep 10m
229+
call assert_equal('we did call you', s:reply)
230+
endfunc
231+
232+
func Test_channel_handler()
233+
call s:run_server('s:channel_handler')
180234
endfunc
181235

182236
" Test that trying to connect to a non-existing port fails quickly.
@@ -198,6 +252,7 @@ func Test_connect_waittime()
198252
call ch_close(handle)
199253
else
200254
" Failed connection doesn't wait the full time on Unix.
255+
" TODO: why is MS-Windows different?
201256
let elapsed = reltime(start)
202257
call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0))
203258
endif

src/testdir/test_cursor_func.vim

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,35 @@ func Test_move_cursor()
2020
call assert_equal([4, 3, 0, 3], getcurpos()[1:])
2121

2222
call cursor(2, 2)
23-
call assert_equal([2, 2, 0, 3], getcurpos()[1:])
23+
call assert_equal([2, 2, 0, 2], getcurpos()[1:])
2424
" line number zero keeps the line number
2525
call cursor(0, 1)
26-
call assert_equal([2, 1, 0, 3], getcurpos()[1:])
26+
call assert_equal([2, 1, 0, 1], getcurpos()[1:])
2727
" col number zero keeps the column
2828
call cursor(3, 0)
29-
call assert_equal([3, 1, 0, 3], getcurpos()[1:])
29+
call assert_equal([3, 1, 0, 1], getcurpos()[1:])
3030
" below last line goes to last line
3131
call cursor(9, 1)
32-
call assert_equal([4, 1, 0, 3], getcurpos()[1:])
32+
call assert_equal([4, 1, 0, 1], getcurpos()[1:])
3333

3434
quit!
3535
endfunc
36+
37+
" Very short version of what matchparen does.
38+
function s:Highlight_Matching_Pair()
39+
let save_cursor = getcurpos()
40+
call setpos('.', save_cursor)
41+
endfunc
42+
43+
func Test_curswant_with_autocommand()
44+
new
45+
call setline(1, ['func()', '{', '}', '----'])
46+
autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
47+
call disable_char_avail_for_testing(1)
48+
exe "normal! 3Ga\<Down>X\<Esc>"
49+
call disable_char_avail_for_testing(0)
50+
call assert_equal('-X---', getline(4))
51+
autocmd! CursorMovedI *
52+
quit!
53+
endfunc
54+

src/version.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,14 @@ static char *(features[]) =
762762

763763
static int included_patches[] =
764764
{ /* Add new patch number below this line */
765+
/**/
766+
1301,
767+
/**/
768+
1300,
769+
/**/
770+
1299,
771+
/**/
772+
1298,
765773
/**/
766774
1297,
767775
/**/

0 commit comments

Comments
 (0)