Skip to content

Commit 2ab375e

Browse files
committed
patch 7.4.1300
Problem: Cannot test CursorMovedI because there is typeahead. Solution: Add disable_char_avail_for_testing().
1 parent f615728 commit 2ab375e

6 files changed

Lines changed: 50 additions & 4 deletions

File tree

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
*/
@@ -12449,8 +12460,11 @@ getpos_both(
1244912460
#endif
1245012461
(varnumber_T)0);
1245112462
if (getcurpos)
12463+
{
12464+
update_curswant();
1245212465
list_append_number(l, curwin->w_curswant == MAXCOL ?
1245312466
(varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
12467+
}
1245412468
}
1245512469
else
1245612470
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
@@ -1621,6 +1621,8 @@ EXTERN alloc_id_T alloc_fail_id INIT(= aid_none);
16211621
EXTERN int alloc_fail_countdown INIT(= -1);
16221622
/* set by alloc_fail(), number of times alloc() returns NULL */
16231623
EXTERN int alloc_fail_repeat INIT(= 0);
1624+
1625+
EXTERN int disable_char_avail_for_testing INIT(= 0);
16241626
#endif
16251627

16261628
/*

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_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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static char *(features[]) =
747747

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1300,
750752
/**/
751753
1299,
752754
/**/

0 commit comments

Comments
 (0)