completion_info.test.vim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. function! s:AssertInfoPopupNotVisible()
  2. call WaitForAssert( {-> assert_true(
  3. \ popup_findinfo() == 0 ||
  4. \ !popup_getpos( popup_findinfo() ).visible ) } )
  5. endfunction
  6. function! s:AssertInfoPopupVisible()
  7. call WaitForAssert( {-> assert_true(
  8. \ popup_findinfo() != 0 &&
  9. \ !empty( popup_getpos( popup_findinfo() ) ) &&
  10. \ popup_getpos( popup_findinfo() ).visible ) } )
  11. endfunction
  12. function! SetUp()
  13. let g:ycm_use_clangd = 1
  14. let g:ycm_confirm_extra_conf = 0
  15. let g:ycm_auto_trigger = 1
  16. let g:ycm_keep_logfiles = 1
  17. let g:ycm_log_level = 'DEBUG'
  18. let g:ycm_add_preview_to_completeopt = 'popup'
  19. let g:ycm_enable_semantic_highlighting = 1
  20. call youcompleteme#test#setup#SetUp()
  21. endfunction
  22. function! TearDown()
  23. call youcompleteme#test#setup#CleanUp()
  24. endfunction
  25. exe 'source' expand( "<sfile>:p:h" ) .. '/completion.common.vim'
  26. function! Test_Using_Ondemand_Resolve()
  27. let debug_info = split( execute( 'YcmDebugInfo' ), "\n" )
  28. enew
  29. setf cpp
  30. call assert_equal( '', &completefunc )
  31. for line in debug_info
  32. if line =~# "^-- Resolve completions: "
  33. let ver = substitute( line, "^-- Resolve completions: ", "", "" )
  34. call assert_equal( 'On demand', ver, 'API version' )
  35. return
  36. endif
  37. endfor
  38. call assert_report( "Didn't find the resolve type in the YcmDebugInfo" )
  39. endfunction
  40. function! Test_ResolveCompletion_OnChange()
  41. call SkipIf( !exists( '*popup_findinfo' ), 'no popup_findinfo' )
  42. " Only the java completer actually uses the completion resolve
  43. call youcompleteme#test#setup#OpenFile(
  44. \ '/third_party/ycmd/ycmd/tests/java/testdata/simple_eclipse_project' .
  45. \ '/src/com/test/TestWithDocumentation.java', { 'delay': 15 } )
  46. call setpos( '.', [ 0, 6, 21 ] )
  47. " Required to trigger TextChangedI
  48. " https://github.com/vim/vim/issues/4665#event-2480928194
  49. call test_override( 'char_avail', 1 )
  50. function! Check1( id )
  51. call WaitForCompletion()
  52. call s:AssertInfoPopupNotVisible()
  53. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2', [ 0 ] ) )
  54. endfunction
  55. let found_getAString = 0
  56. function! Check2( counter, id ) closure
  57. call WaitForCompletion()
  58. call s:AssertInfoPopupVisible()
  59. let info_popup_id = popup_findinfo()
  60. let compl = complete_info()
  61. let selected = compl.items[ compl.selected ]
  62. " All items should be resolved
  63. " NOTE: Even after resolving the item still has this as there's no way to
  64. " update the user data of the item at this point (need a vim change to do
  65. " that)
  66. call assert_true( has_key( json_decode( selected.user_data ),
  67. \ 'resolve' ) )
  68. if selected.word ==# 'getAString'
  69. " It's line 5 because we truncated the signature to fit it in
  70. call WaitForAssert( { ->
  71. \ assert_equal( [ 'MethodsWithDocumentation.getAString() : String',
  72. \ '',
  73. \ 'getAString() : String',
  74. \ '',
  75. \ 'Single line description.',
  76. \ ],
  77. \ getbufline( winbufnr( info_popup_id ), '1', '5' ) )
  78. \ } )
  79. let found_getAString += 1
  80. endif
  81. if a:counter < 10
  82. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2', [ a:counter + 1 ] ) )
  83. else
  84. call feedkeys( "\<Esc>" )
  85. endif
  86. endfunction
  87. call FeedAndCheckMain( 'cw', funcref( 'Check1' ) )
  88. call assert_false( pumvisible(), 'pumvisible()' )
  89. call assert_equal( 1, found_getAString )
  90. call test_override( 'ALL', 0 )
  91. endfunction
  92. function! Test_Resolve_FixIt()
  93. call SkipIf( !exists( '*popup_findinfo' ), 'no popup_findinfo' )
  94. " Only the java completer actually uses the completion resolve
  95. call youcompleteme#test#setup#OpenFile(
  96. \ '/third_party/ycmd/ycmd/tests/java/testdata/simple_eclipse_project' .
  97. \ '/src/com/test/TestWithDocumentation.java', { 'delay': 15 } )
  98. " Required to trigger TextChangedI
  99. " https://github.com/vim/vim/issues/4665#event-2480928194
  100. call test_override( 'char_avail', 1 )
  101. function! Check1( id )
  102. call WaitForCompletion()
  103. call CheckCurrentLine( ' Tes' )
  104. call CheckCompletionItemsHasItems( [ 'Test - com.youcompleteme' ] )
  105. let tabs = IndexOfCompletionItemInList( 'Test - com.youcompleteme' ) + 1
  106. let tabs = repeat( "\<Tab>", tabs )
  107. call FeedAndCheckAgain( tabs, funcref( 'Check2' ) )
  108. endfunction
  109. function! Check2( id )
  110. call WaitForCompletion()
  111. call CheckCompletionItemsHasItems( [ 'Test - com.youcompleteme' ] )
  112. call CheckCurrentLine( ' Test' )
  113. call FeedAndCheckAgain( "\<C-y>", funcref( 'Check3' ) )
  114. endfunction
  115. function! Check3( id )
  116. call WaitForAssert( {-> assert_false( pumvisible(), 'pumvisible()' ) } )
  117. call CheckCurrentLine( ' Test' )
  118. call assert_equal( 'import com.youcompleteme.Test;', getline( 3 ) )
  119. call feedkeys( "\<Esc>" )
  120. endfunction
  121. call setpos( '.', [ 0, 7, 1 ] )
  122. call FeedAndCheckMain( "oTes\<C-space>", funcref( 'Check1' ) )
  123. call test_override( 'ALL', 0 )
  124. endfunction
  125. function! Test_DontResolveCompletion_AlreadyResolved()
  126. call SkipIf( !exists( '*popup_findinfo' ), 'no popup_findinfo' )
  127. " Only the java completer actually uses the completion resolve
  128. call youcompleteme#test#setup#OpenFile(
  129. \ '/third_party/ycmd/ycmd/tests/java/testdata/simple_eclipse_project' .
  130. \ '/src/com/test/TestWithDocumentation.java', { 'delay': 15 } )
  131. call setpos( '.', [ 0, 7, 12 ] )
  132. " Required to trigger TextChangedI
  133. " https://github.com/vim/vim/issues/4665#event-2480928194
  134. call test_override( 'char_avail', 1 )
  135. function! Check1( id )
  136. call WaitForCompletion()
  137. call CheckCompletionItemsContainsExactly( [ 'hashCode' ], 'word' )
  138. call s:AssertInfoPopupNotVisible()
  139. call assert_equal( -1, complete_info().selected )
  140. let compl = complete_info()
  141. let hashCode = compl.items[ 0 ]
  142. call assert_equal( 1, len( compl.items ) )
  143. call assert_equal( 'hashCode', hashCode.word )
  144. call assert_false( has_key( json_decode( hashCode.user_data ),
  145. \ 'resolve' ) )
  146. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2' ) )
  147. endfunction
  148. function! Check2( id )
  149. call WaitForCompletion()
  150. call s:AssertInfoPopupVisible()
  151. let compl = complete_info()
  152. let selected = compl.items[ 0 ]
  153. call assert_equal( 1, len( compl.items ) )
  154. call assert_equal( 'hashCode', selected.word )
  155. call assert_false( has_key( json_decode( selected.user_data ),
  156. \ 'resolve' ) )
  157. call feedkeys( "\<Esc>" )
  158. endfunction
  159. call FeedAndCheckMain( 'C', funcref( 'Check1' ) )
  160. call assert_false( pumvisible(), 'pumvisible()' )
  161. call test_override( 'ALL', 0 )
  162. endfunction
  163. function! Test_SwitchingToSemanticCompletionAfterSelectingIdentifierCandidate()
  164. call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/identifier_semantic_switch.cpp', {} )
  165. call setpos( '.', [ 0, 3, 0 ] )
  166. call test_override( 'char_avail', 1 )
  167. function! Check1( id )
  168. call WaitForCompletion()
  169. call CheckCompletionItemsContainsExactly( [ 'ZbCdE' ], 'word' )
  170. let compl = complete_info()
  171. call assert_equal( -1, compl.selected )
  172. call assert_equal( 1, len( compl.items ) )
  173. let ZbCdE = compl.items[ 0 ]
  174. call assert_equal( 'ZbCdE', ZbCdE.word )
  175. call assert_equal( '[ID]', ZbCdE.menu )
  176. call assert_equal( '', ZbCdE.kind )
  177. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2' ) )
  178. endfunction
  179. function! Check2( id )
  180. call WaitForCompletion()
  181. call assert_match( 'ZbCdE', getline( '.' ) )
  182. call FeedAndCheckAgain( "\<C-Space>", funcref( 'Check3' ) )
  183. endfunction
  184. function! Check3( id )
  185. call WaitForCompletion()
  186. call CheckCompletionItemsContainsExactly( [ 'ZbCdE' ], 'word' )
  187. let compl = complete_info()
  188. call assert_equal( -1, compl.selected )
  189. call assert_match( 'ZbCdE', getline( '.' ) )
  190. call assert_equal( 1, len( compl.items ) )
  191. let ZbCdE = compl.items[ 0 ]
  192. call assert_equal( 'ZbCdE', ZbCdE.word )
  193. call assert_equal( 'void', ZbCdE.menu )
  194. call assert_equal( 'f', ZbCdE.kind )
  195. call feedkeys( "\<Esc>" )
  196. endfunction
  197. call FeedAndCheckMain( 'ZCE', funcref( 'Check1' ) )
  198. call assert_false( pumvisible(), 'pumvisible()' )
  199. call test_override( 'ALL', 0 )
  200. endfunction