postcomplete_tests.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 ycm.test_utils import MockVimModule
  18. MockVimModule()
  19. from mock import ( MagicMock, DEFAULT, patch )
  20. from nose.tools import eq_
  21. from hamcrest import assert_that, empty
  22. from ycm import vimsupport
  23. from ycm.youcompleteme import YouCompleteMe
  24. import contextlib
  25. def GetVariableValue_CompleteItemIs( word, abbr = None, menu = None,
  26. info = None, kind = None ):
  27. def Result( variable ):
  28. if variable == 'v:completed_item':
  29. return {
  30. 'word': word,
  31. 'abbr': abbr,
  32. 'menu': menu,
  33. 'info': info,
  34. 'kind': kind,
  35. }
  36. else:
  37. return DEFAULT
  38. return MagicMock( side_effect = Result )
  39. @contextlib.contextmanager
  40. def _SetupForCsharpCompletionDone( completions ):
  41. with patch( 'ycm.vimsupport.InsertNamespace' ):
  42. with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ):
  43. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  44. request = MagicMock();
  45. request.Done = MagicMock( return_value = True )
  46. request.RawResponse = MagicMock( return_value = completions )
  47. ycm_state._latest_completion_request = request
  48. yield ycm_state
  49. def _BuildCompletion( namespace = None, insertion_text = 'Test',
  50. menu_text = None, extra_menu_info = None,
  51. detailed_info = None, kind = None ):
  52. return {
  53. 'extra_data': { 'required_namespace_import' : namespace },
  54. 'insertion_text': insertion_text,
  55. 'menu_text': menu_text,
  56. 'extra_menu_info': extra_menu_info,
  57. 'kind': kind,
  58. 'detailed_info': detailed_info,
  59. }
  60. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "cs" ] )
  61. def GetCompleteDoneHooks_ResultOnCsharp_test( *args ):
  62. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  63. result = ycm_state.GetCompleteDoneHooks()
  64. eq_( 1, len( list( result ) ) )
  65. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  66. def GetCompleteDoneHooks_EmptyOnOtherFiletype_test( *args ):
  67. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  68. result = ycm_state.GetCompleteDoneHooks()
  69. eq_( 0, len( list( result ) ) )
  70. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  71. def OnCompleteDone_WithActionCallsIt_test( *args ):
  72. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  73. action = MagicMock()
  74. ycm_state._complete_done_hooks[ "txt" ] = action
  75. ycm_state.OnCompleteDone()
  76. assert action.called
  77. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ "txt" ] )
  78. def OnCompleteDone_NoActionNoError_test( *args ):
  79. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  80. ycm_state.OnCompleteDone()
  81. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  82. @patch( 'ycm.vimsupport.GetVariableValue',
  83. GetVariableValue_CompleteItemIs( 'Test' ) )
  84. def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( *args ):
  85. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  86. completions = [ _BuildCompletion( "Test" ) ]
  87. result = ycm_state._FilterToMatchingCompletions( completions, False )
  88. eq_( list( result ), completions )
  89. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  90. @patch( 'ycm.vimsupport.GetVariableValue',
  91. GetVariableValue_CompleteItemIs( 'A' ) )
  92. def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( *args ):
  93. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  94. completions = [ _BuildCompletion( "AAA" ) ]
  95. ycm_state._FilterToMatchingCompletions( completions, False )
  96. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  97. @patch( 'ycm.vimsupport.GetVariableValue',
  98. GetVariableValue_CompleteItemIs( 'Test' ) )
  99. def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( *args ):
  100. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  101. completions = [ _BuildCompletion( "Test" ) ]
  102. result = ycm_state._FilterToMatchingCompletions( completions, False )
  103. eq_( list( result ), completions )
  104. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  105. @patch( 'ycm.vimsupport.GetVariableValue',
  106. GetVariableValue_CompleteItemIs( ' Quote' ) )
  107. def FilterToCompletedCompletions_NewVim_NonMatchIsntReturned_test( *args ):
  108. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  109. completions = [ _BuildCompletion( "A" ) ]
  110. result = ycm_state._FilterToMatchingCompletions( completions, False )
  111. assert_that( list( result ), empty() )
  112. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  113. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Test" )
  114. def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( *args ):
  115. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  116. completions = [ _BuildCompletion( "Test" ) ]
  117. result = ycm_state._FilterToMatchingCompletions( completions, False )
  118. eq_( list( result ), completions )
  119. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  120. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "X" )
  121. def FilterToCompletedCompletions_OldVim_ShortTextDoesntRaise_test( *args ):
  122. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  123. completions = [ _BuildCompletion( "AAA" ) ]
  124. ycm_state._FilterToMatchingCompletions( completions, False )
  125. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  126. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "Test" )
  127. def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( *args ):
  128. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  129. completions = [ _BuildCompletion( "Test" ) ]
  130. result = ycm_state._FilterToMatchingCompletions( completions, False )
  131. eq_( list( result ), completions )
  132. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  133. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  134. def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( *args ):
  135. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  136. completions = [ _BuildCompletion( "A" ) ]
  137. result = ycm_state._FilterToMatchingCompletions( completions, False )
  138. assert_that( list( result ), empty() )
  139. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  140. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Te" )
  141. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test(
  142. *args ):
  143. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  144. completions = [ _BuildCompletion( "Test" ) ]
  145. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  146. eq_( result, True )
  147. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  148. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "X" )
  149. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ShortTextDoesntRaise_test( *args ) :
  150. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  151. completions = [ _BuildCompletion( "AAA" ) ]
  152. ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  153. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  154. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = "Test" )
  155. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned_test( *args ):
  156. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  157. completions = [ _BuildCompletion( "Test" ) ]
  158. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  159. eq_( result, False )
  160. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
  161. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  162. def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_NonMatchIsntReturned_test( *args ):
  163. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  164. completions = [ _BuildCompletion( "A" ) ]
  165. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  166. eq_( result, False )
  167. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  168. @patch( 'ycm.vimsupport.GetVariableValue',
  169. GetVariableValue_CompleteItemIs( "Te") )
  170. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  171. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test(
  172. *args ):
  173. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  174. completions = [ _BuildCompletion( "Test" ) ]
  175. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  176. eq_( result, True )
  177. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  178. @patch( 'ycm.vimsupport.GetVariableValue',
  179. GetVariableValue_CompleteItemIs( "X") )
  180. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = " Quote" )
  181. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_test( *args ):
  182. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  183. completions = [ _BuildCompletion( "AAA" ) ]
  184. ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  185. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  186. @patch( 'ycm.vimsupport.GetVariableValue',
  187. GetVariableValue_CompleteItemIs( "Test" ) )
  188. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
  189. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned_test(
  190. *args ):
  191. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  192. completions = [ _BuildCompletion( "Test" ) ]
  193. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  194. eq_( result, False )
  195. @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
  196. @patch( 'ycm.vimsupport.GetVariableValue',
  197. GetVariableValue_CompleteItemIs( " Quote" ) )
  198. @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
  199. def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_NonMatchIsntReturned_test( *args ):
  200. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  201. completions = [ _BuildCompletion( "A" ) ]
  202. result = ycm_state._HasCompletionsThatCouldBeCompletedWithMoreText( completions )
  203. eq_( result, False )
  204. def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test():
  205. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  206. eq_( None, ycm_state._GetRequiredNamespaceImport( {} ) )
  207. def GetRequiredNamespaceImport_ReturnNamespaceFromExtraData_test():
  208. namespace = "A_NAMESPACE"
  209. ycm_state = YouCompleteMe( MagicMock( spec_set = dict ) )
  210. eq_( namespace, ycm_state._GetRequiredNamespaceImport(
  211. _BuildCompletion( namespace )
  212. ))
  213. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfNotDone_test():
  214. with _SetupForCsharpCompletionDone( [] ) as ycm_state:
  215. ycm_state._latest_completion_request.Done = MagicMock( return_value = False )
  216. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  217. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_NewVim_test():
  218. completions = [ _BuildCompletion( None ) ]
  219. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  220. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  221. with patch( 'ycm.vimsupport.GetVariableValue',
  222. GetVariableValue_CompleteItemIs( 'Te' ) ):
  223. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  224. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_OldVim_test(
  225. *args ):
  226. completions = [ _BuildCompletion( None ) ]
  227. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  228. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  229. with patch( 'ycm.vimsupport.GetVariableValue',
  230. GetVariableValue_CompleteItemIs( 'Te' ) ):
  231. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  232. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatches_NewVim_test(
  233. *args ):
  234. info = [ "NS","Test", "Abbr", "Menu", "Info", "Kind" ]
  235. completions = [ _BuildCompletion( *info ) ]
  236. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  237. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  238. with patch( 'ycm.vimsupport.GetVariableValue',
  239. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  240. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  241. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatchesEvenIfPartial_NewVim_test( *args ):
  242. info = [ "NS", "Test", "Abbr", "Menu", "Info", "Kind" ]
  243. completions = [ _BuildCompletion( *info ),
  244. _BuildCompletion( insertion_text = "TestTest" ) ]
  245. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  246. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  247. with patch( 'ycm.vimsupport.GetVariableValue',
  248. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  249. eq_( [ completions[ 0 ] ],
  250. ycm_state.GetCompletionsUserMayHaveCompleted() )
  251. def GetCompletionsUserMayHaveCompleted_DontReturnMatchIfNontExactMatchesAndPartial_NewVim_test():
  252. info = [ "NS", "Test", "Abbr", "Menu", "Info", "Kind" ]
  253. completions = [ _BuildCompletion( insertion_text = info[ 0 ] ),
  254. _BuildCompletion( insertion_text = "TestTest" ) ]
  255. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  256. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ):
  257. with patch( 'ycm.vimsupport.GetVariableValue',
  258. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  259. eq_( [], ycm_state.GetCompletionsUserMayHaveCompleted() )
  260. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_NewVim_test( *args ):
  261. completions = [ _BuildCompletion( None ) ]
  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( "Test" ) ):
  266. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  267. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_OldVim_test( *args ):
  268. completions = [ _BuildCompletion( None ) ]
  269. ycm_state = _SetupForCsharpCompletionDone( completions )
  270. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  271. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  272. eq_( completions, ycm_state.GetCompletionsUserMayHaveCompleted() )
  273. def PostCompleteCsharp_EmptyDoesntInsertNamespace_test( *args ):
  274. with _SetupForCsharpCompletionDone( [] ) as ycm_state:
  275. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  276. ycm_state._OnCompleteDone_Csharp()
  277. assert not vimsupport.InsertNamespace.called
  278. def PostCompleteCsharp_ExistingWithoutNamespaceDoesntInsertNamespace_test( *args
  279. ):
  280. completions = [ _BuildCompletion( None ) ]
  281. with _SetupForCsharpCompletionDone( completions ) 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_ValueDoesInsertNamespace_test( *args ):
  286. namespace = "A_NAMESPACE"
  287. completions = [ _BuildCompletion( namespace ) ]
  288. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  289. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  290. ycm_state._OnCompleteDone_Csharp()
  291. vimsupport.InsertNamespace.assert_called_once_with( namespace )
  292. def PostCompleteCsharp_InsertSecondNamespaceIfSelected_test( *args ):
  293. namespace = "A_NAMESPACE"
  294. namespace2 = "ANOTHER_NAMESPACE"
  295. completions = [
  296. _BuildCompletion( namespace ),
  297. _BuildCompletion( namespace2 ),
  298. ]
  299. with _SetupForCsharpCompletionDone( completions ) as ycm_state:
  300. with patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ):
  301. with patch( 'ycm.vimsupport.PresentDialog', return_value = 1 ):
  302. ycm_state._OnCompleteDone_Csharp()
  303. vimsupport.InsertNamespace.assert_called_once_with( namespace2 )