postcomplete_test.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # encoding: utf-8
  2. #
  3. # Copyright (C) 2015-2016 YouCompleteMe contributors
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from __future__ import unicode_literals
  20. from __future__ import print_function
  21. from __future__ import division
  22. from __future__ import absolute_import
  23. # Not installing aliases from python-future; it's unreliable and slow.
  24. from builtins import * # noqa
  25. from ycm.tests.test_utils import MockVimModule
  26. MockVimModule()
  27. import contextlib
  28. from mock import MagicMock, DEFAULT, patch
  29. from nose.tools import eq_, ok_
  30. from ycm import vimsupport
  31. from ycmd.utils import ToBytes
  32. from ycm.client.completion_request import ( CompletionRequest,
  33. _FilterToMatchingCompletions,
  34. _GetRequiredNamespaceImport )
  35. from ycm.client.omni_completion_request import OmniCompletionRequest
  36. def CompleteItemIs( word, abbr = None, menu = None,
  37. info = None, kind = None, **kwargs ):
  38. item = {
  39. 'word': ToBytes( word ),
  40. 'abbr': ToBytes( abbr ),
  41. 'menu': ToBytes( menu ),
  42. 'info': ToBytes( info ),
  43. 'kind': ToBytes( kind ),
  44. }
  45. item.update( **kwargs )
  46. return item
  47. def GetVariableValue_CompleteItemIs( word, abbr = None, menu = None,
  48. info = None, kind = None, **kwargs ):
  49. def Result( variable ):
  50. if variable == 'v:completed_item':
  51. return CompleteItemIs( word, abbr, menu, info, kind, **kwargs )
  52. return DEFAULT
  53. return MagicMock( side_effect = Result )
  54. def BuildCompletion( insertion_text = 'Test',
  55. menu_text = None,
  56. extra_menu_info = None,
  57. detailed_info = None,
  58. kind = None,
  59. extra_data = None ):
  60. completion = {
  61. 'insertion_text': insertion_text
  62. }
  63. if extra_menu_info:
  64. completion[ 'extra_menu_info' ] = extra_menu_info
  65. if menu_text:
  66. completion[ 'menu_text' ] = menu_text
  67. if detailed_info:
  68. completion[ 'detailed_info' ] = detailed_info
  69. if kind:
  70. completion[ 'kind' ] = kind
  71. if extra_data:
  72. completion[ 'extra_data' ] = extra_data
  73. return completion
  74. def BuildCompletionNamespace( namespace = None,
  75. insertion_text = 'Test',
  76. menu_text = None,
  77. extra_menu_info = None,
  78. detailed_info = None,
  79. kind = None ):
  80. return BuildCompletion( insertion_text = insertion_text,
  81. menu_text = menu_text,
  82. extra_menu_info = extra_menu_info,
  83. detailed_info = detailed_info,
  84. kind = kind,
  85. extra_data = {
  86. 'required_namespace_import': namespace
  87. } )
  88. def BuildCompletionFixIt( fixits,
  89. insertion_text = 'Test',
  90. menu_text = None,
  91. extra_menu_info = None,
  92. detailed_info = None,
  93. kind = None ):
  94. return BuildCompletion( insertion_text = insertion_text,
  95. menu_text = menu_text,
  96. extra_menu_info = extra_menu_info,
  97. detailed_info = detailed_info,
  98. kind = kind,
  99. extra_data = {
  100. 'fixits': fixits,
  101. } )
  102. @contextlib.contextmanager
  103. def _SetupForCsharpCompletionDone( completions ):
  104. with patch( 'ycm.vimsupport.InsertNamespace' ):
  105. with _SetUpCompleteDone( completions ) as request:
  106. yield request
  107. @contextlib.contextmanager
  108. def _SetUpCompleteDone( completions ):
  109. with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ):
  110. request = CompletionRequest( None )
  111. request.Done = MagicMock( return_value = True )
  112. request._RawResponse = MagicMock( return_value = {
  113. 'completions': completions
  114. } )
  115. yield request
  116. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'ycmtest' ] )
  117. def OnCompleteDone_DefaultFixIt_test( *args ):
  118. request = CompletionRequest( None )
  119. request.Done = MagicMock( return_value = True )
  120. request._OnCompleteDone_Csharp = MagicMock()
  121. request._OnCompleteDone_FixIt = MagicMock()
  122. request.OnCompleteDone()
  123. request._OnCompleteDone_Csharp.assert_not_called()
  124. request._OnCompleteDone_FixIt.assert_called_once_with()
  125. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'cs' ] )
  126. def OnCompleteDone_CsharpFixIt_test( *args ):
  127. request = CompletionRequest( None )
  128. request.Done = MagicMock( return_value = True )
  129. request._OnCompleteDone_Csharp = MagicMock()
  130. request._OnCompleteDone_FixIt = MagicMock()
  131. request.OnCompleteDone()
  132. request._OnCompleteDone_Csharp.assert_called_once_with()
  133. request._OnCompleteDone_FixIt.assert_not_called()
  134. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'ycmtest' ] )
  135. def OnCompleteDone_NoFixItIfNotDone_test( *args ):
  136. request = CompletionRequest( None )
  137. request.Done = MagicMock( return_value = False )
  138. request._OnCompleteDone_Csharp = MagicMock()
  139. request._OnCompleteDone_FixIt = MagicMock()
  140. request.OnCompleteDone()
  141. request._OnCompleteDone_Csharp.assert_not_called()
  142. request._OnCompleteDone_FixIt.assert_not_called()
  143. @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'ycmtest' ] )
  144. def OnCompleteDone_NoFixItForOmnifunc_test( *args ):
  145. request = OmniCompletionRequest( 'omnifunc', None )
  146. request.Done = MagicMock( return_value = True )
  147. request._OnCompleteDone_Csharp = MagicMock()
  148. request._OnCompleteDone_FixIt = MagicMock()
  149. request.OnCompleteDone()
  150. request._OnCompleteDone_Csharp.assert_not_called()
  151. request._OnCompleteDone_FixIt.assert_not_called()
  152. def FilterToCompletedCompletions_MatchIsReturned_test():
  153. completions = [ BuildCompletion( insertion_text = 'Test' ) ]
  154. result = _FilterToMatchingCompletions( CompleteItemIs( 'Test' ), completions )
  155. eq_( list( result ), completions )
  156. def FilterToCompletedCompletions_ShortTextDoesntRaise_test():
  157. completions = [ BuildCompletion( insertion_text = 'AAA' ) ]
  158. result = _FilterToMatchingCompletions( CompleteItemIs( 'A' ), completions )
  159. eq_( list( result ), [] )
  160. def FilterToCompletedCompletions_ExactMatchIsReturned_test():
  161. completions = [ BuildCompletion( insertion_text = 'Test' ) ]
  162. result = _FilterToMatchingCompletions( CompleteItemIs( 'Test' ), completions )
  163. eq_( list( result ), completions )
  164. def FilterToCompletedCompletions_NonMatchIsntReturned_test():
  165. completions = [ BuildCompletion( insertion_text = 'A' ) ]
  166. result = _FilterToMatchingCompletions( CompleteItemIs( ' Quote' ),
  167. completions )
  168. eq_( list( result ), [] )
  169. def FilterToCompletedCompletions_Unicode_test():
  170. completions = [ BuildCompletion( insertion_text = '†es†' ) ]
  171. result = _FilterToMatchingCompletions( CompleteItemIs( '†es†' ),
  172. completions )
  173. eq_( list( result ), completions )
  174. def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test():
  175. eq_( _GetRequiredNamespaceImport( {} ), None )
  176. def GetRequiredNamespaceImport_ReturnNamespaceFromExtraData_test():
  177. namespace = 'A_NAMESPACE'
  178. eq_( _GetRequiredNamespaceImport( BuildCompletionNamespace( namespace ) ),
  179. namespace )
  180. @patch( 'ycm.vimsupport.GetVariableValue',
  181. GetVariableValue_CompleteItemIs( 'Te' ) )
  182. def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_test(
  183. *args ):
  184. completions = [ BuildCompletionNamespace( None ) ]
  185. with _SetupForCsharpCompletionDone( completions ) as request:
  186. eq_( request._GetCompletionsUserMayHaveCompleted(), [] )
  187. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatches_test( *args ):
  188. info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ]
  189. completions = [ BuildCompletionNamespace( *info ) ]
  190. with _SetupForCsharpCompletionDone( completions ) as request:
  191. with patch( 'ycm.vimsupport.GetVariableValue',
  192. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  193. eq_( request._GetCompletionsUserMayHaveCompleted(), completions )
  194. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatchesEvenIfPartial_test(): # noqa
  195. info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ]
  196. completions = [ BuildCompletionNamespace( *info ),
  197. BuildCompletion( insertion_text = 'TestTest' ) ]
  198. with _SetupForCsharpCompletionDone( completions ) as request:
  199. with patch( 'ycm.vimsupport.GetVariableValue',
  200. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  201. eq_( request._GetCompletionsUserMayHaveCompleted(), [ completions[ 0 ] ] )
  202. def GetCompletionsUserMayHaveCompleted_DontReturnMatchIfNoExactMatchesAndPartial_test(): # noqa
  203. info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ]
  204. completions = [ BuildCompletion( insertion_text = info[ 0 ] ),
  205. BuildCompletion( insertion_text = 'TestTest' ) ]
  206. with _SetupForCsharpCompletionDone( completions ) as request:
  207. with patch( 'ycm.vimsupport.GetVariableValue',
  208. GetVariableValue_CompleteItemIs( *info[ 1: ] ) ):
  209. eq_( request._GetCompletionsUserMayHaveCompleted(), [] )
  210. @patch( 'ycm.vimsupport.GetVariableValue',
  211. GetVariableValue_CompleteItemIs( 'Test' ) )
  212. def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_test( *args ):
  213. completions = [ BuildCompletionNamespace( None ) ]
  214. with _SetupForCsharpCompletionDone( completions ) as request:
  215. eq_( request._GetCompletionsUserMayHaveCompleted(), completions )
  216. @patch( 'ycm.vimsupport.GetVariableValue',
  217. GetVariableValue_CompleteItemIs( 'Test', user_data='0' ) )
  218. def GetCompletionsUserMayHaveCompleted_UseUserData0_test( *args ):
  219. # Identical completions but we specify the first one via user_data.
  220. completions = [
  221. BuildCompletionNamespace( 'namespace1' ),
  222. BuildCompletionNamespace( 'namespace2' )
  223. ]
  224. with _SetupForCsharpCompletionDone( completions ) as request:
  225. eq_( request._GetCompletionsUserMayHaveCompleted(),
  226. [ BuildCompletionNamespace( 'namespace1' ) ] )
  227. @patch( 'ycm.vimsupport.GetVariableValue',
  228. GetVariableValue_CompleteItemIs( 'Test', user_data='1' ) )
  229. def GetCompletionsUserMayHaveCompleted_UseUserData1_test( *args ):
  230. # Identical completions but we specify the second one via user_data.
  231. completions = [
  232. BuildCompletionNamespace( 'namespace1' ),
  233. BuildCompletionNamespace( 'namespace2' )
  234. ]
  235. with _SetupForCsharpCompletionDone( completions ) as request:
  236. eq_( request._GetCompletionsUserMayHaveCompleted(),
  237. [ BuildCompletionNamespace( 'namespace2' ) ] )
  238. @patch( 'ycm.vimsupport.GetVariableValue',
  239. GetVariableValue_CompleteItemIs( 'Test', user_data='' ) )
  240. def GetCompletionsUserMayHaveCompleted_EmptyUserData_test( *args ):
  241. # Identical completions but none is selected.
  242. completions = [
  243. BuildCompletionNamespace( 'namespace1' ),
  244. BuildCompletionNamespace( 'namespace2' )
  245. ]
  246. with _SetupForCsharpCompletionDone( completions ) as request:
  247. eq_( request._GetCompletionsUserMayHaveCompleted(), [] )
  248. @patch( 'ycm.vimsupport.GetVariableValue',
  249. GetVariableValue_CompleteItemIs( 'Test' ) )
  250. def PostCompleteCsharp_EmptyDoesntInsertNamespace_test( *args ):
  251. with _SetupForCsharpCompletionDone( [] ) as request:
  252. request._OnCompleteDone_Csharp()
  253. ok_( not vimsupport.InsertNamespace.called )
  254. @patch( 'ycm.vimsupport.GetVariableValue',
  255. GetVariableValue_CompleteItemIs( 'Test' ) )
  256. def PostCompleteCsharp_ExistingWithoutNamespaceDoesntInsertNamespace_test(
  257. *args ):
  258. completions = [ BuildCompletionNamespace( None ) ]
  259. with _SetupForCsharpCompletionDone( completions ) as request:
  260. request._OnCompleteDone_Csharp()
  261. ok_( not vimsupport.InsertNamespace.called )
  262. @patch( 'ycm.vimsupport.GetVariableValue',
  263. GetVariableValue_CompleteItemIs( 'Test' ) )
  264. def PostCompleteCsharp_ValueDoesInsertNamespace_test( *args ):
  265. namespace = 'A_NAMESPACE'
  266. completions = [ BuildCompletionNamespace( namespace ) ]
  267. with _SetupForCsharpCompletionDone( completions ) as request:
  268. request._OnCompleteDone_Csharp()
  269. vimsupport.InsertNamespace.assert_called_once_with( namespace )
  270. @patch( 'ycm.vimsupport.GetVariableValue',
  271. GetVariableValue_CompleteItemIs( 'Test' ) )
  272. @patch( 'ycm.vimsupport.PresentDialog', return_value = 1 )
  273. def PostCompleteCsharp_InsertSecondNamespaceIfSelected_test( *args ):
  274. namespace = 'A_NAMESPACE'
  275. namespace2 = 'ANOTHER_NAMESPACE'
  276. completions = [
  277. BuildCompletionNamespace( namespace ),
  278. BuildCompletionNamespace( namespace2 ),
  279. ]
  280. with _SetupForCsharpCompletionDone( completions ) as request:
  281. request._OnCompleteDone_Csharp()
  282. vimsupport.InsertNamespace.assert_called_once_with( namespace2 )
  283. @patch( 'ycm.vimsupport.GetVariableValue',
  284. GetVariableValue_CompleteItemIs( 'Test' ) )
  285. @patch( 'ycm.vimsupport.ReplaceChunks' )
  286. def PostCompleteFixIt_ApplyFixIt_NoFixIts_test( replace_chunks, *args ):
  287. completions = [
  288. BuildCompletionFixIt( [] )
  289. ]
  290. with _SetUpCompleteDone( completions ) as request:
  291. request._OnCompleteDone_FixIt()
  292. replace_chunks.assert_not_called()
  293. @patch( 'ycm.vimsupport.GetVariableValue',
  294. GetVariableValue_CompleteItemIs( 'Test' ) )
  295. @patch( 'ycm.vimsupport.ReplaceChunks' )
  296. def PostCompleteFixIt_ApplyFixIt_EmptyFixIt_test( replace_chunks, *args ):
  297. completions = [
  298. BuildCompletionFixIt( [ { 'chunks': [] } ] )
  299. ]
  300. with _SetUpCompleteDone( completions ) as request:
  301. request._OnCompleteDone_FixIt()
  302. replace_chunks.assert_called_once_with( [], silent = True )
  303. @patch( 'ycm.vimsupport.GetVariableValue',
  304. GetVariableValue_CompleteItemIs( 'Test' ) )
  305. @patch( 'ycm.vimsupport.ReplaceChunks' )
  306. def PostCompleteFixIt_ApplyFixIt_NoFixIt_test( replace_chunks, *args ):
  307. completions = [
  308. BuildCompletion()
  309. ]
  310. with _SetUpCompleteDone( completions ) as request:
  311. request._OnCompleteDone_FixIt()
  312. replace_chunks.assert_not_called()
  313. @patch( 'ycm.vimsupport.GetVariableValue',
  314. GetVariableValue_CompleteItemIs( 'Test' ) )
  315. @patch( 'ycm.vimsupport.ReplaceChunks' )
  316. def PostCompleteFixIt_ApplyFixIt_PickFirst_test( replace_chunks, *args ):
  317. completions = [
  318. BuildCompletionFixIt( [ { 'chunks': 'one' } ] ),
  319. BuildCompletionFixIt( [ { 'chunks': 'two' } ] ),
  320. ]
  321. with _SetUpCompleteDone( completions ) as request:
  322. request._OnCompleteDone_FixIt()
  323. replace_chunks.assert_called_once_with( 'one', silent = True )
  324. @patch( 'ycm.vimsupport.GetVariableValue',
  325. GetVariableValue_CompleteItemIs( 'Test', user_data='0' ) )
  326. @patch( 'ycm.vimsupport.ReplaceChunks' )
  327. def PostCompleteFixIt_ApplyFixIt_PickFirstUserData_test( replace_chunks,
  328. *args ):
  329. completions = [
  330. BuildCompletionFixIt( [ { 'chunks': 'one' } ] ),
  331. BuildCompletionFixIt( [ { 'chunks': 'two' } ] ),
  332. ]
  333. with _SetUpCompleteDone( completions ) as request:
  334. request._OnCompleteDone_FixIt()
  335. replace_chunks.assert_called_once_with( 'one', silent = True )
  336. @patch( 'ycm.vimsupport.GetVariableValue',
  337. GetVariableValue_CompleteItemIs( 'Test', user_data='1' ) )
  338. @patch( 'ycm.vimsupport.ReplaceChunks' )
  339. def PostCompleteFixIt_ApplyFixIt_PickSecond_test( replace_chunks, *args ):
  340. completions = [
  341. BuildCompletionFixIt( [ { 'chunks': 'one' } ] ),
  342. BuildCompletionFixIt( [ { 'chunks': 'two' } ] ),
  343. ]
  344. with _SetUpCompleteDone( completions ) as request:
  345. request._OnCompleteDone_FixIt()
  346. replace_chunks.assert_called_once_with( 'two', silent = True )