postcomplete_tests.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. # Copyright (C) 2015 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. from future import standard_library
  22. standard_library.install_aliases()
  23. from builtins import * # noqa
  24. from ycm.test_utils import MockVimModule
  25. MockVimModule()
  26. from mock import ( MagicMock, DEFAULT, patch )
  27. from nose.tools import eq_
  28. from hamcrest import assert_that, empty
  29. from ycm import vimsupport
  30. from ycm.youcompleteme import YouCompleteMe
  31. import contextlib
  32. def GetVariableValue_CompleteItemIs( word, abbr = None, menu = None,
  33. info = None, kind = None ):
  34. def Result( variable ):
  35. if variable == 'v:completed_item':
  36. return {
  37. 'word': word,
  38. 'abbr': abbr,
  39. 'menu': menu,
  40. 'info': info,
  41. 'kind': kind,
  42. }
  43. else:
  44. return DEFAULT
  45. return MagicMock( side_effect = Result )
  46. @contextlib.contextmanager
  47. def _SetupForCsharpCompletionDone( completions ):
  48. with patch( 'ycm.vimsupport.InsertNamespace' ):
  49. with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ):
  50. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  51. request = MagicMock();
  52. request.Done = MagicMock( return_value = True )
  53. request.RawResponse = MagicMock( return_value = completions )
  54. ycm_state._latest_completion_request = request
  55. yield ycm_state
  56. def _BuildCompletion( namespace = None, insertion_text = 'Test',
  57. menu_text = None, extra_menu_info = None,
  58. detailed_info = None, kind = None ):
  59. return {
  60. 'extra_data': { 'required_namespace_import' : namespace },
  61. 'insertion_text': insertion_text,
  62. 'menu_text': menu_text,
  63. 'extra_menu_info': extra_menu_info,
  64. 'kind': kind,
  65. 'detailed_info': detailed_info,
  66. }
  67. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "cs" ] )
  68. def GetCompleteDoneHooks_ResultOnCsharp_test( *args ):
  69. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  70. result = ycm_state.GetCompleteDoneHooks()
  71. eq_( 1, len( list( result ) ) )
  72. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  73. def GetCompleteDoneHooks_EmptyOnOtherFiletype_test( *args ):
  74. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  75. result = ycm_state.GetCompleteDoneHooks()
  76. eq_( 0, len( list( result ) ) )
  77. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  78. def OnCompleteDone_WithActionCallsIt_test( *args ):
  79. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  80. action = MagicMock()
  81. ycm_state._complete_done_hooks[ "txt" ] = action
  82. ycm_state.OnCompleteDone()
  83. assert action.called
  84. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  85. def OnCompleteDone_NoActionNoError_test( *args ):
  86. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  87. ycm_state.OnCompleteDone()
  88. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  89. @patch( 'ycm.vimsupport.GetVariableValue',
  90. GetVariableValue_CompleteItemIs( 'Test' ) )
  91. def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( *args ):
  92. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  93. completions = [ _BuildCompletion( "Test" ) ]
  94. result = ycm_state._FilterToMatchingCompletions( completions, False )
  95. eq_( list( result ), completions )
  96. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  97. @patch( 'ycm.vimsupport.GetVariableValue',
  98. GetVariableValue_CompleteItemIs( 'A' ) )
  99. def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( *args ):
  100. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  101. completions = [ _BuildCompletion( "AAA" ) ]
  102. ycm_state._FilterToMatchingCompletions( completions, False )
  103. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  104. @patch( 'ycm.vimsupport.GetVariableValue',
  105. GetVariableValue_CompleteItemIs( 'Test' ) )
  106. def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( *args ):
  107. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  108. completions = [ _BuildCompletion( "Test" ) ]
  109. result = ycm_state._FilterToMatchingCompletions( completions, False )
  110. eq_( list( result ), completions )
  111. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  112. @patch( 'ycm.vimsupport.GetVariableValue',
  113. GetVariableValue_CompleteItemIs( ' Quote' ) )
  114. def FilterToCompletedCompletions_NewVim_NonMatchIsntReturned_test( *args ):
  115. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  116. completions = [ _BuildCompletion( "A" ) ]
  117. result = ycm_state._FilterToMatchingCompletions( completions, False )
  118. assert_that( list( result ), empty() )
  119. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  120. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Test" )
  121. def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( *args ):
  122. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  123. completions = [ _BuildCompletion( "Test" ) ]
  124. result = ycm_state._FilterToMatchingCompletions( completions, False )
  125. eq_( list( result ), completions )
  126. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  127. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "X" )
  128. def FilterToCompletedCompletions_OldVim_ShortTextDoesntRaise_test( *args ):
  129. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  130. completions = [ _BuildCompletion( "AAA" ) ]
  131. ycm_state._FilterToMatchingCompletions( completions, False )
  132. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  133. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "Test" )
  134. def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( *args ):
  135. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  136. completions = [ _BuildCompletion( "Test" ) ]
  137. result = ycm_state._FilterToMatchingCompletions( completions, False )
  138. eq_( list( result ), completions )
  139. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  140. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  141. def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( *args ):
  142. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  143. completions = [ _BuildCompletion( "A" ) ]
  144. result = ycm_state._FilterToMatchingCompletions( completions, False )
  145. assert_that( list( result ), empty() )
  146. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  147. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Te" )
  148. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test(
  149. *args ):
  150. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  151. completions = [ _BuildCompletion( "Test" ) ]
  152. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  153. eq_( result, True )
  154. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  155. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "X" )
  156. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ShortTextDoesntRaise_test( *args ) :
  157. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  158. completions = [ _BuildCompletion( "AAA" ) ]
  159. ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  160. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  161. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "Test" )
  162. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned_test( *args ):
  163. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  164. completions = [ _BuildCompletion( "Test" ) ]
  165. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  166. eq_( result, False )
  167. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  168. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  169. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_NonMatchIsntReturned_test( *args ):
  170. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  171. completions = [ _BuildCompletion( "A" ) ]
  172. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  173. eq_( result, False )
  174. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  175. @patch( 'ycm.vimsupport.GetVariableValue',
  176. GetVariableValue_CompleteItemIs( "Te") )
  177. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  178. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test(
  179. *args ):
  180. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  181. completions = [ _BuildCompletion( "Test" ) ]
  182. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  183. eq_( result, True )
  184. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  185. @patch( 'ycm.vimsupport.GetVariableValue',
  186. GetVariableValue_CompleteItemIs( "X") )
  187. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  188. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_test( *args ):
  189. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  190. completions = [ _BuildCompletion( "AAA" ) ]
  191. ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  192. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  193. @patch( 'ycm.vimsupport.GetVariableValue',
  194. GetVariableValue_CompleteItemIs( "Test" ) )
  195. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
  196. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned_test(
  197. *args ):
  198. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  199. completions = [ _BuildCompletion( "Test" ) ]
  200. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  201. eq_( result, False )
  202. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  203. @patch( 'ycm.vimsupport.GetVariableValue',
  204. GetVariableValue_CompleteItemIs( " Quote" ) )
  205. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
  206. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_NonMatchIsntReturned_test( *args ):
  207. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  208. completions = [ _BuildCompletion( "A" ) ]
  209. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  210. eq_( result, False )
  211. def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test():
  212. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  213. eq_( None, ycm_state._GetRequiredNamespaceImport( {} ) )
  214. def GetRequiredNamespaceImport_ReturnNamespaceFromExtraData_test():
  215. namespace = "A_NAMESPACE"
  216. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  217. eq_( namespace, ycm_state._GetRequiredNamespaceImport(
  218. _BuildCompletion( namespace )
  219. ))
  220. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfNotDone_test():
  221. with _SetupForCsharpCompletionDone( [] ) as ycm_state:
  222. ycm_state._latest_completion_request.Done = MagicMock( return_value = False )
  223. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  224. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_NewVim_test():
  225. completions = [ _BuildCompletion( None ) ]
  226. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  227. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  228. with patch( 'ycm.vimsupport.GetVariableValue',
  229. GetVariableValue_CompleteItemIs( 'Te' ) ):
  230. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  231. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_OldVim_test(
  232. *args ):
  233. completions = [ _BuildCompletion( None ) ]
  234. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  235. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  236. with patch( 'ycm.vimsupport.GetVariableValue',
  237. GetVariableValue_CompleteItemIs( 'Te' ) ):
  238. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  239. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatches_NewVim_test(
  240. *args ):
  241. info = [ "NS","Test", "Abbr", "Menu", "Info", "Kind" ]
  242. completions = [ _BuildCompletion( *info ) ]
  243. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  244. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  245. with patch( 'ycm.vimsupport.GetVariableValue',
  246. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  247. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  248. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatchesEvenIfPartial_NewVim_test( *args ):
  249. info = [ "NS", "Test", "Abbr", "Menu", "Info", "Kind" ]
  250. completions = [ _BuildCompletion( *info ),
  251. _BuildCompletion( insertion_text = "TestTest" ) ]
  252. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  253. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  254. with patch( 'ycm.vimsupport.GetVariableValue',
  255. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  256. eq_( [ completions[ 0 ] ],
  257. ycm_state.GetCompletionsUserMayHaveCompleted() )
  258. def GetCompletionsUserMayHaveCompleted_DontReturnMatchIfNontExactMatchesAndPartial_NewVim_test():
  259. info = [ "NS", "Test", "Abbr", "Menu", "Info", "Kind" ]
  260. completions = [ _BuildCompletion( insertion_text = info[ 0 ] ),
  261. _BuildCompletion( insertion_text = "TestTest" ) ]
  262. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  263. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  264. with patch( 'ycm.vimsupport.GetVariableValue',
  265. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  266. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  267. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_NewVim_test( *args ):
  268. completions = [ _BuildCompletion( None ) ]
  269. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  270. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  271. with patch( 'ycm.vimsupport.GetVariableValue',
  272. GetVariableValue_CompleteItemIs( "Test" ) ):
  273. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  274. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_OldVim_test( *args ):
  275. completions = [ _BuildCompletion( None ) ]
  276. ycm_state = _SetupForCsharpCompletionDone( completions )
  277. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  278. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  279. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  280. def PostCompleteCsharp_EmptyDoesntInsertNamespace_test( *args ):
  281. with _SetupForCsharpCompletionDone( [] ) as ycm_state:
  282. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  283. ycm_state._OnCompleteDone_Csharp()
  284. assert not vimsupport.InsertNamespace.called
  285. def PostCompleteCsharp_ExistingWithoutNamespaceDoesntInsertNamespace_test( *args
  286. ):
  287. completions = [ _BuildCompletion( None ) ]
  288. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  289. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  290. ycm_state._OnCompleteDone_Csharp()
  291. assert not vimsupport.InsertNamespace.called
  292. def PostCompleteCsharp_ValueDoesInsertNamespace_test( *args ):
  293. namespace = "A_NAMESPACE"
  294. completions = [ _BuildCompletion( namespace ) ]
  295. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  296. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  297. ycm_state._OnCompleteDone_Csharp()
  298. vimsupport.InsertNamespace.assert_called_once_with( namespace )
  299. def PostCompleteCsharp_InsertSecondNamespaceIfSelected_test( *args ):
  300. namespace = "A_NAMESPACE"
  301. namespace2 = "ANOTHER_NAMESPACE"
  302. completions = [
  303. _BuildCompletion( namespace ),
  304. _BuildCompletion( namespace2 ),
  305. ]
  306. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  307. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  308. with patch( 'ycm.vimsupport.PresentDialog', return_value = 1 ):
  309. ycm_state._OnCompleteDone_Csharp()
  310. vimsupport.InsertNamespace.assert_called_once_with( namespace2 )