Friday, February 9, 2018

Re: Writing tests for completion

On 09/02/2018 14:32, Bram Moolenaar wrote:
>
> Lifepillar wrote:
>
>> On 05/02/2018 22:49, Nikolay Aleksandrovich Pavlov wrote:
>>> 2018-02-05 23:31 GMT+03:00 Lifepillar <lifepillar@lifepillar.me>:
>>>> I would like to automate the testing of my completion plugin,
>>>> so I have started to write functions like this (I am using
>>>> a test framework like the one in Vim):
>>>>
>>>> fun! Test_buffer_keyword_completion()
>>>> new
>>>> set completeopt=menuone,noselect
>>>> call feedkeys("ajump ju\<C-X>\<C-N>\<C-N>\<ESC>", "x")
>>>> call assert_equal("jump jump", getline(1))
>>>> bwipe!
>>>> endf
>>>>
>>>> This test passes (using `vim --clean`), but if I feed this
>>>> key sequence instead:
>>>>
>>>> call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "x")
>>>>
>>>> the test fails:
>>>>
>>>> Expected 'jump jump' but got 'jump ju'
>>>>
>>>> Why is that? If I try the key sequences manually, both give the
>>>> same result.
>>>
>>> To emulate user input you must add `t` to the flags, there are some
>>> differences between handling input from user and input from mappings
>>> and without `t` you are emulating the latter. I could not list all the
>>> differences though.
>>
>> Adding "t" does not seem to make a difference in this case. This is a
>> simplified way to reproduce the difference:
>>
>> vim --clean
>> :set completeopt=menuone,noselect
>> :call feedkeys("ajump ju\<C-X>\<C-N>\<C-N>\<ESC>", "tx")
>> :call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
>>
>> As I said, manual typing does not show any difference (the result is
>> "jump jump" in both cases). Am I missing something?
>> I am using Vim 8.0.1450.
>
> This happens because Vim checks for typed keys while finding matches.
> It then sees the CTRL-P before the first completion is found, causing it
> to stick to "ju".
>
> When sourcing a script with Normal mode commands, there is an extra
> check that prevents the completion to be aborted. I think we should do
> the same when using feedkeys(), as it's a very similar thing. And it's
> very difficult to do this in another way.
>
> However, this may break completion plugins. Or make them work better,
> hard to predict. I can make the change for this and await reports...

I have tried patch 1482 and tests like the above now work as expected.
I have tested my plugin (which uses feedkeys()) and I have found no
regressions. Great job!

Unfortunately, I still have some problems when I test my plugin's
mappings. I have traced back the issue to the following:

fun! s:dup_complete()
" Get the text in front of the cursor
return getline('.')[0:col('.') - 2]
endf

fun! Test_dup_completion()
new
imap <buffer> <expr> <silent> <plug>(Dup) <sid>dup_complete()
call feedkeys("ajump\<plug>(Dup)", "tx")
call assert_equal("jumpjump", getline(1))
bwipe!
endf

It seems that in s:dup_complete() getline('.') is always empty and
col('.') is always 1. Is there anything I can do about it?

(The idea is that my plugin gets the text in front of the cursor to
decide how to proceed with completion.)

Life.





--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: