youcompleteme_test.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. # Copyright (C) 2016-2018 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.client.messages_request import MessagesPoll
  18. from ycm.tests.test_utils import ( ExtendedMock,
  19. MockVimBuffers,
  20. MockVimModule,
  21. Version,
  22. VimBuffer,
  23. VimMatch,
  24. VimSign )
  25. MockVimModule()
  26. import os
  27. import sys
  28. from hamcrest import ( assert_that, contains_exactly, empty, equal_to,
  29. has_entries, is_in, is_not, matches_regexp )
  30. from unittest.mock import call, MagicMock, patch
  31. from ycm import vimsupport
  32. from ycm.paths import _PathToPythonUsedDuringBuild
  33. from ycm.vimsupport import ( SetVariableValue,
  34. SIGN_BUFFER_ID_INITIAL_VALUE )
  35. from ycm.tests import ( StopServer,
  36. test_utils,
  37. UserOptions,
  38. WaitUntilReady,
  39. YouCompleteMeInstance )
  40. from ycm.client.base_request import _LoadExtraConfFile
  41. from ycm.youcompleteme import YouCompleteMe
  42. from ycmd.responses import ServerError
  43. from ycm.tests.mock_utils import ( MockAsyncServerResponseDone,
  44. MockAsyncServerResponseInProgress,
  45. MockAsyncServerResponseException )
  46. @YouCompleteMeInstance()
  47. def YouCompleteMe_YcmCoreNotImported_test( ycm ):
  48. assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) )
  49. @patch( 'ycm.vimsupport.PostVimMessage' )
  50. def YouCompleteMe_InvalidPythonInterpreterPath_test( post_vim_message ):
  51. with UserOptions( {
  52. 'g:ycm_server_python_interpreter': '/invalid/python/path' } ):
  53. try:
  54. ycm = YouCompleteMe()
  55. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  56. post_vim_message.assert_called_once_with(
  57. "Unable to start the ycmd server. "
  58. "Path in 'g:ycm_server_python_interpreter' option does not point "
  59. "to a valid Python 3.6+. "
  60. "Correct the error then restart the server with ':YcmRestartServer'." )
  61. post_vim_message.reset_mock()
  62. SetVariableValue( 'g:ycm_server_python_interpreter',
  63. _PathToPythonUsedDuringBuild() )
  64. ycm.RestartServer()
  65. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  66. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  67. finally:
  68. WaitUntilReady()
  69. StopServer( ycm )
  70. @patch( 'ycmd.utils.PathToFirstExistingExecutable', return_value = None )
  71. @patch( 'ycm.paths._EndsWithPython', return_value = False )
  72. @patch( 'ycm.vimsupport.PostVimMessage' )
  73. def YouCompleteMe_NoPythonInterpreterFound_test( post_vim_message, *args ):
  74. with UserOptions( {} ):
  75. try:
  76. with patch( 'ycmd.utils.ReadFile', side_effect = IOError ):
  77. ycm = YouCompleteMe()
  78. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  79. post_vim_message.assert_called_once_with(
  80. "Unable to start the ycmd server. Cannot find Python 3.6+. "
  81. "Set the 'g:ycm_server_python_interpreter' option to a Python "
  82. "interpreter path. "
  83. "Correct the error then restart the server with ':YcmRestartServer'." )
  84. post_vim_message.reset_mock()
  85. SetVariableValue( 'g:ycm_server_python_interpreter',
  86. _PathToPythonUsedDuringBuild() )
  87. ycm.RestartServer()
  88. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  89. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  90. finally:
  91. WaitUntilReady()
  92. StopServer( ycm )
  93. def RunNotifyUserIfServerCrashed( ycm, post_vim_message, test ):
  94. StopServer( ycm )
  95. ycm._logger = MagicMock( autospec = True )
  96. ycm._server_popen = MagicMock( autospec = True )
  97. ycm._server_popen.poll.return_value = test[ 'return_code' ]
  98. ycm.OnFileReadyToParse()
  99. assert_that( ycm._logger.error.call_args[ 0 ][ 0 ],
  100. test[ 'expected_message' ] )
  101. assert_that( post_vim_message.call_args[ 0 ][ 0 ],
  102. test[ 'expected_message' ] )
  103. @YouCompleteMeInstance()
  104. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  105. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedCore_test(
  106. post_vim_message, ycm ):
  107. message = (
  108. "The ycmd server SHUT DOWN \\(restart with ':YcmRestartServer'\\). "
  109. "Unexpected error while loading the YCM core library. Type "
  110. "':YcmToggleLogs ycmd_\\d+_stderr_.+.log' to check the logs." )
  111. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  112. 'return_code': 3,
  113. 'expected_message': matches_regexp( message )
  114. } )
  115. @YouCompleteMeInstance()
  116. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  117. def YouCompleteMe_NotifyUserIfServerCrashed_MissingCore_test(
  118. post_vim_message, ycm ):
  119. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  120. "YCM core library not detected; you need to compile YCM before "
  121. "using it. Follow the instructions in the documentation." )
  122. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  123. 'return_code': 4,
  124. 'expected_message': equal_to( message )
  125. } )
  126. @YouCompleteMeInstance()
  127. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  128. def YouCompleteMe_NotifyUserIfServerCrashed_OutdatedCore_test(
  129. post_vim_message, ycm ):
  130. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  131. "YCM core library too old; PLEASE RECOMPILE by running the "
  132. "install.py script. See the documentation for more details." )
  133. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  134. 'return_code': 7,
  135. 'expected_message': equal_to( message )
  136. } )
  137. @YouCompleteMeInstance()
  138. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  139. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedExitCode_test(
  140. post_vim_message, ycm ):
  141. message = (
  142. "The ycmd server SHUT DOWN \\(restart with ':YcmRestartServer'\\). "
  143. "Unexpected exit code 1. Type "
  144. "':YcmToggleLogs ycmd_\\d+_stderr_.+.log' to check the logs." )
  145. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  146. 'return_code': 1,
  147. 'expected_message': matches_regexp( message )
  148. } )
  149. @YouCompleteMeInstance( { 'g:ycm_extra_conf_vim_data': [ 'tempname()' ] } )
  150. def YouCompleteMe_DebugInfo_ServerRunning_test( ycm ):
  151. dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
  152. buf_name = os.path.join( dir_of_script, 'testdata', 'test.cpp' )
  153. extra_conf = os.path.join( dir_of_script, 'testdata', '.ycm_extra_conf.py' )
  154. _LoadExtraConfFile( extra_conf )
  155. current_buffer = VimBuffer( buf_name, filetype = 'cpp' )
  156. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  157. assert_that(
  158. ycm.DebugInfo(),
  159. matches_regexp(
  160. 'Client logfile: .+\n'
  161. 'Server Python interpreter: .+\n'
  162. 'Server Python version: .+\n'
  163. 'Server has Clang support compiled in: (False|True)\n'
  164. 'Clang version: .+\n'
  165. 'Extra configuration file found and loaded\n'
  166. 'Extra configuration path: .*testdata[/\\\\]\\.ycm_extra_conf\\.py\n'
  167. '[\\w\\W]*'
  168. 'Server running at: .+\n'
  169. 'Server process ID: \\d+\n'
  170. 'Server logfiles:\n'
  171. ' .+\n'
  172. ' .+' )
  173. )
  174. @YouCompleteMeInstance()
  175. def YouCompleteMe_DebugInfo_ServerNotRunning_test( ycm ):
  176. StopServer( ycm )
  177. current_buffer = VimBuffer( 'current_buffer' )
  178. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  179. assert_that(
  180. ycm.DebugInfo(),
  181. matches_regexp(
  182. 'Client logfile: .+\n'
  183. 'Server errored, no debug info from server\n'
  184. 'Server running at: .+\n'
  185. 'Server process ID: \\d+\n'
  186. 'Server logfiles:\n'
  187. ' .+\n'
  188. ' .+' )
  189. )
  190. @YouCompleteMeInstance()
  191. def YouCompleteMe_OnVimLeave_RemoveClientLogfileByDefault_test( ycm ):
  192. client_logfile = ycm._client_logfile
  193. assert_that( os.path.isfile( client_logfile ),
  194. f'Logfile { client_logfile } does not exist.' )
  195. ycm.OnVimLeave()
  196. assert_that( not os.path.isfile( client_logfile ),
  197. f'Logfile { client_logfile } was not removed.' )
  198. @YouCompleteMeInstance( { 'g:ycm_keep_logfiles': 1 } )
  199. def YouCompleteMe_OnVimLeave_KeepClientLogfile_test( ycm ):
  200. client_logfile = ycm._client_logfile
  201. assert_that( os.path.isfile( client_logfile ),
  202. f'Logfile { client_logfile } does not exist.' )
  203. ycm.OnVimLeave()
  204. assert_that( os.path.isfile( client_logfile ),
  205. f'Logfile { client_logfile } was removed.' )
  206. @YouCompleteMeInstance()
  207. @patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
  208. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  209. def YouCompleteMe_ToggleLogs_WithParameters_test( open_filename,
  210. close_buffers_for_filename,
  211. ycm ):
  212. logfile_buffer = VimBuffer( ycm._client_logfile )
  213. with MockVimBuffers( [ logfile_buffer ], [ logfile_buffer ] ):
  214. ycm.ToggleLogs( 90,
  215. 'botright vertical',
  216. os.path.basename( ycm._client_logfile ),
  217. 'nonexisting_logfile',
  218. os.path.basename( ycm._server_stdout ) )
  219. open_filename.assert_has_exact_calls( [
  220. call( ycm._server_stdout, { 'size': 90,
  221. 'watch': True,
  222. 'fix': True,
  223. 'focus': False,
  224. 'position': 'end',
  225. 'mods': 'botright vertical' } )
  226. ] )
  227. close_buffers_for_filename.assert_has_exact_calls( [
  228. call( ycm._client_logfile )
  229. ] )
  230. @YouCompleteMeInstance()
  231. # Select the second item of the list which is the ycmd stderr logfile.
  232. @patch( 'ycm.vimsupport.SelectFromList', return_value = 1 )
  233. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  234. def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileNotAlreadyOpen_test(
  235. open_filename, select_from_list, ycm ):
  236. current_buffer = VimBuffer( 'current_buffer' )
  237. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  238. ycm.ToggleLogs( 0, '' )
  239. open_filename.assert_has_exact_calls( [
  240. call( ycm._server_stderr, { 'size': 12,
  241. 'watch': True,
  242. 'fix': True,
  243. 'focus': False,
  244. 'position': 'end',
  245. 'mods': '' } )
  246. ] )
  247. @YouCompleteMeInstance()
  248. # Select the third item of the list which is the ycmd stdout logfile.
  249. @patch( 'ycm.vimsupport.SelectFromList', return_value = 2 )
  250. @patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
  251. def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileAlreadyOpen_test(
  252. close_buffers_for_filename, select_from_list, ycm ):
  253. logfile_buffer = VimBuffer( ycm._server_stdout )
  254. with MockVimBuffers( [ logfile_buffer ], [ logfile_buffer ] ):
  255. ycm.ToggleLogs( 0, '' )
  256. close_buffers_for_filename.assert_has_exact_calls( [
  257. call( ycm._server_stdout )
  258. ] )
  259. @YouCompleteMeInstance()
  260. @patch( 'ycm.vimsupport.SelectFromList',
  261. side_effect = RuntimeError( 'Error message' ) )
  262. @patch( 'ycm.vimsupport.PostVimMessage' )
  263. def YouCompleteMe_ToggleLogs_WithoutParameters_NoSelection_test(
  264. post_vim_message, select_from_list, ycm ):
  265. current_buffer = VimBuffer( 'current_buffer' )
  266. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  267. ycm.ToggleLogs( 0, '' )
  268. assert_that(
  269. # Argument passed to PostVimMessage.
  270. post_vim_message.call_args[ 0 ][ 0 ],
  271. equal_to( 'Error message' )
  272. )
  273. @YouCompleteMeInstance()
  274. def YouCompleteMe_GetDefinedSubcommands_ListFromServer_test( ycm ):
  275. current_buffer = VimBuffer( 'buffer' )
  276. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  277. with patch( 'ycm.client.base_request._JsonFromFuture',
  278. return_value = [ 'SomeCommand', 'AnotherCommand' ] ):
  279. assert_that(
  280. ycm.GetDefinedSubcommands(),
  281. contains_exactly(
  282. 'SomeCommand',
  283. 'AnotherCommand'
  284. )
  285. )
  286. @YouCompleteMeInstance()
  287. @patch( 'ycm.client.base_request._logger', autospec = True )
  288. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  289. def YouCompleteMe_GetDefinedSubcommands_ErrorFromServer_test( post_vim_message,
  290. logger,
  291. ycm ):
  292. current_buffer = VimBuffer( 'buffer' )
  293. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  294. with patch( 'ycm.client.base_request._JsonFromFuture',
  295. side_effect = ServerError( 'Server error' ) ):
  296. result = ycm.GetDefinedSubcommands()
  297. logger.exception.assert_called_with( 'Error while handling server response' )
  298. post_vim_message.assert_has_exact_calls( [
  299. call( 'Server error', truncate = False )
  300. ] )
  301. assert_that( result, empty() )
  302. @YouCompleteMeInstance()
  303. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  304. def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test(
  305. post_vim_message, ycm ):
  306. current_buffer = VimBuffer( 'buffer' )
  307. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  308. with patch( 'ycm.client.base_request._JsonFromFuture',
  309. return_value = { 'message': 'some_detailed_diagnostic' } ):
  310. ycm.ShowDetailedDiagnostic(),
  311. post_vim_message.assert_has_exact_calls( [
  312. call( 'some_detailed_diagnostic', warning = False )
  313. ] )
  314. @YouCompleteMeInstance()
  315. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  316. def YouCompleteMe_ShowDetailedDiagnostic_Exception_test(
  317. post_vim_message, ycm ):
  318. current_buffer = VimBuffer( 'buffer' )
  319. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  320. with patch( 'ycm.client.base_request._JsonFromFuture',
  321. side_effect = RuntimeError( 'Some exception' ) ):
  322. ycm.ShowDetailedDiagnostic(),
  323. post_vim_message.assert_has_exact_calls( [
  324. call( 'Some exception', truncate = False )
  325. ] )
  326. @YouCompleteMeInstance()
  327. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  328. def YouCompleteMe_ShowDiagnostics_FiletypeNotSupported_test( post_vim_message,
  329. ycm ):
  330. current_buffer = VimBuffer( 'buffer', filetype = 'not_supported' )
  331. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  332. ycm.ShowDiagnostics()
  333. post_vim_message.assert_called_once_with(
  334. 'Native filetype completion not supported for current file, '
  335. 'cannot force recompilation.', warning = False )
  336. @YouCompleteMeInstance()
  337. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  338. return_value = True )
  339. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  340. @patch( 'ycm.vimsupport.SetLocationListForWindow', new_callable = ExtendedMock )
  341. def YouCompleteMe_ShowDiagnostics_NoDiagnosticsDetected_test(
  342. set_location_list_for_window,
  343. post_vim_message,
  344. filetype_completer_exists,
  345. ycm ):
  346. current_buffer = VimBuffer( 'buffer', filetype = 'cpp' )
  347. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  348. with patch( 'ycm.client.event_notification.EventNotification.Response',
  349. return_value = {} ):
  350. ycm.ShowDiagnostics()
  351. post_vim_message.assert_has_exact_calls( [
  352. call( 'Forcing compilation, this will block Vim until done.',
  353. warning = False ),
  354. call( 'Diagnostics refreshed', warning = False ),
  355. call( 'No warnings or errors detected.', warning = False )
  356. ] )
  357. set_location_list_for_window.assert_called_once_with( 1, [] )
  358. @YouCompleteMeInstance( { 'g:ycm_log_level': 'debug',
  359. 'g:ycm_keep_logfiles': 1,
  360. 'g:ycm_open_loclist_on_ycm_diags': 0 } )
  361. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  362. return_value = True )
  363. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  364. @patch( 'ycm.vimsupport.SetLocationListForWindow', new_callable = ExtendedMock )
  365. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList_test(
  366. set_location_list_for_window,
  367. post_vim_message,
  368. filetype_completer_exists,
  369. ycm ):
  370. diagnostic = {
  371. 'kind': 'ERROR',
  372. 'text': 'error text',
  373. 'location': {
  374. 'filepath': 'buffer',
  375. 'line_num': 19,
  376. 'column_num': 2
  377. }
  378. }
  379. current_buffer = VimBuffer( 'buffer',
  380. filetype = 'cpp',
  381. number = 3 )
  382. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  383. with patch( 'ycm.client.event_notification.EventNotification.Response',
  384. return_value = [ diagnostic ] ):
  385. ycm.ShowDiagnostics()
  386. post_vim_message.assert_has_exact_calls( [
  387. call( 'Forcing compilation, this will block Vim until done.',
  388. warning = False ),
  389. call( 'Diagnostics refreshed', warning = False )
  390. ] )
  391. set_location_list_for_window.assert_called_once_with( 1, [ {
  392. 'bufnr': 3,
  393. 'lnum': 19,
  394. 'col': 2,
  395. 'text': 'error text',
  396. 'type': 'E',
  397. 'valid': 1
  398. } ] )
  399. @YouCompleteMeInstance( { 'g:ycm_open_loclist_on_ycm_diags': 1 } )
  400. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  401. return_value = True )
  402. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  403. @patch( 'ycm.vimsupport.SetLocationListForWindow', new_callable = ExtendedMock )
  404. @patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
  405. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList_test(
  406. open_location_list,
  407. set_location_list_for_window,
  408. post_vim_message,
  409. filetype_completer_exists,
  410. ycm ):
  411. diagnostic = {
  412. 'kind': 'ERROR',
  413. 'text': 'error text',
  414. 'location': {
  415. 'filepath': 'buffer',
  416. 'line_num': 19,
  417. 'column_num': 2
  418. }
  419. }
  420. current_buffer = VimBuffer( 'buffer',
  421. filetype = 'cpp',
  422. number = 3 )
  423. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  424. with patch( 'ycm.client.event_notification.EventNotification.Response',
  425. return_value = [ diagnostic ] ):
  426. ycm.ShowDiagnostics()
  427. post_vim_message.assert_has_exact_calls( [
  428. call( 'Forcing compilation, this will block Vim until done.',
  429. warning = False ),
  430. call( 'Diagnostics refreshed', warning = False )
  431. ] )
  432. set_location_list_for_window.assert_called_once_with( 1, [ {
  433. 'bufnr': 3,
  434. 'lnum': 19,
  435. 'col': 2,
  436. 'text': 'error text',
  437. 'type': 'E',
  438. 'valid': 1
  439. } ] )
  440. open_location_list.assert_called_once_with( focus = True )
  441. def YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message, *args ):
  442. contents = """int main() {
  443. int x, y;
  444. x == y
  445. }"""
  446. # List of diagnostics returned by ycmd for the above code.
  447. diagnostics = [ {
  448. 'kind': 'ERROR',
  449. 'text': "expected ';' after expression",
  450. 'location': {
  451. 'filepath': 'buffer',
  452. 'line_num': 3,
  453. 'column_num': 9
  454. },
  455. # Looks strange but this is really what ycmd is returning.
  456. 'location_extent': {
  457. 'start': {
  458. 'filepath': '',
  459. 'line_num': 0,
  460. 'column_num': 0,
  461. },
  462. 'end': {
  463. 'filepath': '',
  464. 'line_num': 0,
  465. 'column_num': 0,
  466. }
  467. },
  468. 'ranges': [],
  469. 'fixit_available': True
  470. }, {
  471. 'kind': 'WARNING',
  472. 'text': 'equality comparison result unused',
  473. 'location': {
  474. 'filepath': 'buffer',
  475. 'line_num': 3,
  476. 'column_num': 7,
  477. },
  478. 'location_extent': {
  479. 'start': {
  480. 'filepath': 'buffer',
  481. 'line_num': 3,
  482. 'column_num': 5,
  483. },
  484. 'end': {
  485. 'filepath': 'buffer',
  486. 'line_num': 3,
  487. 'column_num': 7,
  488. }
  489. },
  490. 'ranges': [ {
  491. 'start': {
  492. 'filepath': 'buffer',
  493. 'line_num': 3,
  494. 'column_num': 3,
  495. },
  496. 'end': {
  497. 'filepath': 'buffer',
  498. 'line_num': 3,
  499. 'column_num': 9,
  500. }
  501. } ],
  502. 'fixit_available': True
  503. } ]
  504. current_buffer = VimBuffer( 'buffer',
  505. filetype = 'c',
  506. contents = contents.splitlines(),
  507. number = 5 )
  508. test_utils.VIM_SIGNS = []
  509. vimsupport.SIGN_ID_FOR_BUFFER.clear()
  510. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 1 ) ):
  511. with patch( 'ycm.client.event_notification.EventNotification.Response',
  512. return_value = diagnostics ):
  513. ycm.OnFileReadyToParse()
  514. ycm.HandleFileParseRequest( block = True )
  515. # The error on the current line is echoed, not the warning.
  516. post_vim_message.assert_called_once_with(
  517. "expected ';' after expression (FixIt)",
  518. truncate = True, warning = False )
  519. # Error match is added after warning matches.
  520. assert_that(
  521. test_utils.VIM_MATCHES_FOR_WINDOW,
  522. has_entries( {
  523. 1: contains_exactly(
  524. VimMatch( 'YcmWarningSection', '\\%3l\\%5c\\_.\\{-}\\%3l\\%7c' ),
  525. VimMatch( 'YcmWarningSection', '\\%3l\\%3c\\_.\\{-}\\%3l\\%9c' ),
  526. VimMatch( 'YcmErrorSection', '\\%3l\\%8c' )
  527. )
  528. } )
  529. )
  530. # Only the error sign is placed.
  531. assert_that(
  532. test_utils.VIM_SIGNS,
  533. contains_exactly(
  534. VimSign( SIGN_BUFFER_ID_INITIAL_VALUE, 3, 'YcmError', 5 )
  535. )
  536. )
  537. # The error is not echoed again when moving the cursor along the line.
  538. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 2 ) ):
  539. post_vim_message.reset_mock()
  540. ycm.OnCursorMoved()
  541. post_vim_message.assert_not_called()
  542. # The error is cleared when moving the cursor to another line.
  543. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 2, 2 ) ):
  544. post_vim_message.reset_mock()
  545. ycm.OnCursorMoved()
  546. post_vim_message.assert_called_once_with( "", warning = False )
  547. # The error is echoed when moving the cursor back.
  548. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 2 ) ):
  549. post_vim_message.reset_mock()
  550. ycm.OnCursorMoved()
  551. post_vim_message.assert_called_once_with(
  552. "expected ';' after expression (FixIt)",
  553. truncate = True, warning = False )
  554. with patch( 'ycm.client.event_notification.EventNotification.Response',
  555. return_value = diagnostics[ 1 : ] ):
  556. ycm.OnFileReadyToParse()
  557. ycm.HandleFileParseRequest( block = True )
  558. assert_that(
  559. test_utils.VIM_MATCHES_FOR_WINDOW,
  560. has_entries( {
  561. 1: contains_exactly(
  562. VimMatch( 'YcmWarningSection', '\\%3l\\%5c\\_.\\{-}\\%3l\\%7c' ),
  563. VimMatch( 'YcmWarningSection', '\\%3l\\%3c\\_.\\{-}\\%3l\\%9c' )
  564. )
  565. } )
  566. )
  567. assert_that(
  568. test_utils.VIM_SIGNS,
  569. contains_exactly(
  570. VimSign( SIGN_BUFFER_ID_INITIAL_VALUE + 1, 3, 'YcmWarning', 5 )
  571. )
  572. )
  573. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  574. 'g:ycm_enable_diagnostic_signs': 1,
  575. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  576. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  577. return_value = True )
  578. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  579. @patch( 'ycm.client.event_notification.EventNotification.Done',
  580. return_value = True )
  581. def YouCompleteMe_UpdateDiagnosticInterface_OldVim_test(
  582. request_done, post_vim_message, filetype_completer_exists, ycm ):
  583. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  584. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  585. 'g:ycm_enable_diagnostic_signs': 1,
  586. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  587. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  588. return_value = True )
  589. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  590. @patch( 'ycm.tests.test_utils.VIM_VERSION', Version( 8, 1, 614 ) )
  591. @patch( 'ycm.client.event_notification.EventNotification.Done',
  592. return_value = True )
  593. def YouCompleteMe_UpdateDiagnosticInterface_NewVim_test(
  594. request_done, post_vim_message, filetype_completer_exists, ycm ):
  595. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  596. @YouCompleteMeInstance( { 'g:ycm_enable_diagnostic_highlighting': 1 } )
  597. def YouCompleteMe_UpdateMatches_ClearDiagnosticMatchesInNewBuffer_test( ycm ):
  598. current_buffer = VimBuffer( 'buffer',
  599. filetype = 'c',
  600. number = 5 )
  601. test_utils.VIM_MATCHES_FOR_WINDOW[ 1 ] = [
  602. VimMatch( 'YcmWarningSection', '\\%3l\\%5c\\_.\\{-}\\%3l\\%7c' ),
  603. VimMatch( 'YcmWarningSection', '\\%3l\\%3c\\_.\\{-}\\%3l\\%9c' ),
  604. VimMatch( 'YcmErrorSection', '\\%3l\\%8c' )
  605. ]
  606. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  607. ycm.UpdateMatches()
  608. assert_that( test_utils.VIM_MATCHES_FOR_WINDOW,
  609. has_entries( { 1: empty() } ) )
  610. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  611. 'g:ycm_always_populate_location_list': 1,
  612. 'g:ycm_show_diagnostics_ui': 0,
  613. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  614. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  615. return_value = True )
  616. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  617. def YouCompleteMe_AsyncDiagnosticUpdate_UserDisabled_test(
  618. post_vim_message,
  619. filetype_completer_exists,
  620. ycm ):
  621. diagnostics = [
  622. {
  623. 'kind': 'ERROR',
  624. 'text': 'error text in current buffer',
  625. 'location': {
  626. 'filepath': '/current',
  627. 'line_num': 1,
  628. 'column_num': 1
  629. },
  630. 'location_extent': {
  631. 'start': {
  632. 'filepath': '/current',
  633. 'line_num': 1,
  634. 'column_num': 1,
  635. },
  636. 'end': {
  637. 'filepath': '/current',
  638. 'line_num': 1,
  639. 'column_num': 1,
  640. }
  641. },
  642. 'ranges': []
  643. },
  644. ]
  645. current_buffer = VimBuffer( '/current',
  646. filetype = 'ycmtest',
  647. contents = [ 'current' ] * 10,
  648. number = 1 )
  649. buffers = [ current_buffer ]
  650. windows = [ current_buffer ]
  651. # Register each buffer internally with YCM
  652. for current in buffers:
  653. with MockVimBuffers( buffers, [ current ] ):
  654. ycm.OnFileReadyToParse()
  655. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  656. new_callable = ExtendedMock ) as set_location_list_for_window:
  657. with MockVimBuffers( buffers, windows ):
  658. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  659. post_vim_message.assert_has_exact_calls( [] )
  660. set_location_list_for_window.assert_has_exact_calls( [] )
  661. assert_that(
  662. test_utils.VIM_MATCHES_FOR_WINDOW,
  663. empty()
  664. )
  665. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  666. 'g:ycm_always_populate_location_list': 1,
  667. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  668. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  669. return_value = True )
  670. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  671. def YouCompleteMe_AsyncDiagnosticUpdate_SingleFile_test(
  672. post_vim_message,
  673. filetype_completer_exists,
  674. ycm ):
  675. # This test simulates asynchronous diagnostic updates associated with a single
  676. # file (e.g. Translation Unit), but where the actual errors refer to other
  677. # open files and other non-open files. This is not strictly invalid, nor is it
  678. # completely normal, but it is supported and does work.
  679. # Contrast with the next test which sends the diagnostics filewise, which is
  680. # what the language server protocol will do.
  681. diagnostics = [
  682. {
  683. 'kind': 'ERROR',
  684. 'text': 'error text in current buffer',
  685. 'location': {
  686. 'filepath': '/current',
  687. 'line_num': 1,
  688. 'column_num': 1
  689. },
  690. 'location_extent': {
  691. 'start': {
  692. 'filepath': '/current',
  693. 'line_num': 1,
  694. 'column_num': 1,
  695. },
  696. 'end': {
  697. 'filepath': '/current',
  698. 'line_num': 1,
  699. 'column_num': 1,
  700. }
  701. },
  702. 'ranges': []
  703. },
  704. {
  705. 'kind': 'ERROR',
  706. 'text': 'error text in hidden buffer',
  707. 'location': {
  708. 'filepath': '/has_diags',
  709. 'line_num': 4,
  710. 'column_num': 2
  711. },
  712. 'location_extent': {
  713. 'start': {
  714. 'filepath': '/has_diags',
  715. 'line_num': 4,
  716. 'column_num': 2,
  717. },
  718. 'end': {
  719. 'filepath': '/has_diags',
  720. 'line_num': 4,
  721. 'column_num': 2,
  722. }
  723. },
  724. 'ranges': []
  725. },
  726. {
  727. 'kind': 'ERROR',
  728. 'text': 'error text in buffer not open in Vim',
  729. 'location': {
  730. 'filepath': '/not_open',
  731. 'line_num': 8,
  732. 'column_num': 4
  733. },
  734. 'location_extent': {
  735. 'start': {
  736. 'filepath': '/not_open',
  737. 'line_num': 8,
  738. 'column_num': 4,
  739. },
  740. 'end': {
  741. 'filepath': '/not_open',
  742. 'line_num': 8,
  743. 'column_num': 4,
  744. }
  745. },
  746. 'ranges': []
  747. }
  748. ]
  749. current_buffer = VimBuffer( '/current',
  750. filetype = 'ycmtest',
  751. contents = [ 'current' ] * 10,
  752. number = 1 )
  753. no_diags_buffer = VimBuffer( '/no_diags',
  754. filetype = 'ycmtest',
  755. contents = [ 'nodiags' ] * 10,
  756. number = 2 )
  757. hidden_buffer = VimBuffer( '/has_diags',
  758. filetype = 'ycmtest',
  759. contents = [ 'hasdiags' ] * 10,
  760. number = 3 )
  761. buffers = [ current_buffer, no_diags_buffer, hidden_buffer ]
  762. windows = [ current_buffer, no_diags_buffer ]
  763. # Register each buffer internally with YCM
  764. for current in buffers:
  765. with MockVimBuffers( buffers, [ current ] ):
  766. ycm.OnFileReadyToParse()
  767. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  768. new_callable = ExtendedMock ) as set_location_list_for_window:
  769. with MockVimBuffers( buffers, windows ):
  770. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  771. # We update the diagnostic on the current cursor position
  772. post_vim_message.assert_has_exact_calls( [
  773. call( "error text in current buffer", truncate = True, warning = False ),
  774. ] )
  775. # Ensure we included all the diags though
  776. set_location_list_for_window.assert_has_exact_calls( [
  777. call( 1, [
  778. {
  779. 'lnum': 1,
  780. 'col': 1,
  781. 'bufnr': 1,
  782. 'valid': 1,
  783. 'type': 'E',
  784. 'text': 'error text in current buffer',
  785. },
  786. {
  787. 'lnum': 4,
  788. 'col': 2,
  789. 'bufnr': 3,
  790. 'valid': 1,
  791. 'type': 'E',
  792. 'text': 'error text in hidden buffer',
  793. },
  794. {
  795. 'lnum': 8,
  796. 'col': 4,
  797. 'bufnr': -1, # sic: Our mocked bufnr function actually returns -1,
  798. # even though YCM is passing "create if needed".
  799. # FIXME? we shouldn't do that, and we should pass
  800. # filename instead
  801. 'valid': 1,
  802. 'type': 'E',
  803. 'text': 'error text in buffer not open in Vim'
  804. }
  805. ] )
  806. ] )
  807. assert_that(
  808. test_utils.VIM_MATCHES_FOR_WINDOW,
  809. has_entries( {
  810. 1: contains_exactly(
  811. VimMatch( 'YcmErrorSection', '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
  812. )
  813. } )
  814. )
  815. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  816. 'g:ycm_always_populate_location_list': 1,
  817. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  818. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  819. return_value = True )
  820. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  821. def YouCompleteMe_AsyncDiagnosticUpdate_PerFile_test(
  822. post_vim_message,
  823. filetype_completer_exists,
  824. ycm ):
  825. # This test simulates asynchronous diagnostic updates which are delivered per
  826. # file, including files which are open and files which are not.
  827. # Ordered to ensure that the calls to update are deterministic
  828. diagnostics_per_file = [
  829. ( '/current', [ {
  830. 'kind': 'ERROR',
  831. 'text': 'error text in current buffer',
  832. 'location': {
  833. 'filepath': '/current',
  834. 'line_num': 1,
  835. 'column_num': 1
  836. },
  837. 'location_extent': {
  838. 'start': {
  839. 'filepath': '/current',
  840. 'line_num': 1,
  841. 'column_num': 1,
  842. },
  843. 'end': {
  844. 'filepath': '/current',
  845. 'line_num': 1,
  846. 'column_num': 1,
  847. }
  848. },
  849. 'ranges': [],
  850. } ] ),
  851. ( '/separate_window', [ {
  852. 'kind': 'ERROR',
  853. 'text': 'error text in a buffer open in a separate window',
  854. 'location': {
  855. 'filepath': '/separate_window',
  856. 'line_num': 3,
  857. 'column_num': 3
  858. },
  859. 'location_extent': {
  860. 'start': {
  861. 'filepath': '/separate_window',
  862. 'line_num': 3,
  863. 'column_num': 3,
  864. },
  865. 'end': {
  866. 'filepath': '/separate_window',
  867. 'line_num': 3,
  868. 'column_num': 3,
  869. }
  870. },
  871. 'ranges': []
  872. } ] ),
  873. ( '/hidden', [ {
  874. 'kind': 'ERROR',
  875. 'text': 'error text in hidden buffer',
  876. 'location': {
  877. 'filepath': '/hidden',
  878. 'line_num': 4,
  879. 'column_num': 2
  880. },
  881. 'location_extent': {
  882. 'start': {
  883. 'filepath': '/hidden',
  884. 'line_num': 4,
  885. 'column_num': 2,
  886. },
  887. 'end': {
  888. 'filepath': '/hidden',
  889. 'line_num': 4,
  890. 'column_num': 2,
  891. }
  892. },
  893. 'ranges': []
  894. } ] ),
  895. ( '/not_open', [ {
  896. 'kind': 'ERROR',
  897. 'text': 'error text in buffer not open in Vim',
  898. 'location': {
  899. 'filepath': '/not_open',
  900. 'line_num': 8,
  901. 'column_num': 4
  902. },
  903. 'location_extent': {
  904. 'start': {
  905. 'filepath': '/not_open',
  906. 'line_num': 8,
  907. 'column_num': 4,
  908. },
  909. 'end': {
  910. 'filepath': '/not_open',
  911. 'line_num': 8,
  912. 'column_num': 4,
  913. }
  914. },
  915. 'ranges': []
  916. } ] )
  917. ]
  918. current_buffer = VimBuffer( '/current',
  919. filetype = 'ycmtest',
  920. contents = [ 'current' ] * 10,
  921. number = 1 )
  922. no_diags_buffer = VimBuffer( '/no_diags',
  923. filetype = 'ycmtest',
  924. contents = [ 'no_diags' ] * 10,
  925. number = 2 )
  926. separate_window = VimBuffer( '/separate_window',
  927. filetype = 'ycmtest',
  928. contents = [ 'separate_window' ] * 10,
  929. number = 3 )
  930. hidden_buffer = VimBuffer( '/hidden',
  931. filetype = 'ycmtest',
  932. contents = [ 'hidden' ] * 10,
  933. number = 4 )
  934. buffers = [
  935. current_buffer,
  936. no_diags_buffer,
  937. separate_window,
  938. hidden_buffer
  939. ]
  940. windows = [
  941. current_buffer,
  942. no_diags_buffer,
  943. separate_window
  944. ]
  945. # Register each buffer internally with YCM
  946. for current in buffers:
  947. with MockVimBuffers( buffers, [ current ] ):
  948. ycm.OnFileReadyToParse()
  949. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  950. new_callable = ExtendedMock ) as set_location_list_for_window:
  951. with MockVimBuffers( buffers, windows ):
  952. for filename, diagnostics in diagnostics_per_file:
  953. ycm.UpdateWithNewDiagnosticsForFile( filename, diagnostics )
  954. # We update the diagnostic on the current cursor position
  955. post_vim_message.assert_has_exact_calls( [
  956. call( "error text in current buffer", truncate = True, warning = False ),
  957. ] )
  958. # Ensure we included all the diags though
  959. set_location_list_for_window.assert_has_exact_calls( [
  960. call( 1, [
  961. {
  962. 'lnum': 1,
  963. 'col': 1,
  964. 'bufnr': 1,
  965. 'valid': 1,
  966. 'type': 'E',
  967. 'text': 'error text in current buffer',
  968. },
  969. ] ),
  970. call( 3, [
  971. {
  972. 'lnum': 3,
  973. 'col': 3,
  974. 'bufnr': 3,
  975. 'valid': 1,
  976. 'type': 'E',
  977. 'text': 'error text in a buffer open in a separate window',
  978. },
  979. ] )
  980. ] )
  981. # FIXME: diagnostic matches in windows other than the current one are not
  982. # updated.
  983. assert_that(
  984. test_utils.VIM_MATCHES_FOR_WINDOW,
  985. has_entries( {
  986. 1: contains_exactly(
  987. VimMatch( 'YcmErrorSection', '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
  988. )
  989. } )
  990. )
  991. @YouCompleteMeInstance()
  992. def YouCompleteMe_OnPeriodicTick_ServerNotRunning_test( ycm ):
  993. with patch.object( ycm, 'IsServerAlive', return_value = False ):
  994. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  995. @YouCompleteMeInstance()
  996. def YouCompleteMe_OnPeriodicTick_ServerNotReady_test( ycm ):
  997. with patch.object( ycm, 'IsServerAlive', return_value = True ):
  998. with patch.object( ycm, 'IsServerReady', return_value = False ):
  999. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1000. @YouCompleteMeInstance()
  1001. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1002. return_value = True )
  1003. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1004. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1005. def YouCompleteMe_OnPeriodicTick_DontRetry_test(
  1006. post_data_to_handler_async,
  1007. validate_response_object,
  1008. filetype_completer_exists,
  1009. ycm ):
  1010. current_buffer = VimBuffer( '/current',
  1011. filetype = 'ycmtest',
  1012. number = 1 )
  1013. # Create the request and make the first poll; we expect no response
  1014. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1015. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1016. post_data_to_handler_async.assert_called()
  1017. assert ycm._message_poll_requests[ 'ycmtest' ] is not None
  1018. post_data_to_handler_async.reset_mock()
  1019. # OK that sent the request, now poll to check if it is complete (say it is
  1020. # not)
  1021. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1022. mock_response = MockAsyncServerResponseInProgress()
  1023. with patch.dict( ycm._message_poll_requests, {} ):
  1024. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1025. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1026. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1027. poll_again = ycm.OnPeriodicTick()
  1028. mock_future.done.assert_called()
  1029. mock_future.result.assert_not_called()
  1030. assert_that( poll_again, equal_to( True ) )
  1031. # Poll again, but return a response (telling us to stop polling)
  1032. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1033. mock_response = MockAsyncServerResponseDone( False )
  1034. with patch.dict( ycm._message_poll_requests, {} ):
  1035. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1036. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1037. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1038. poll_again = ycm.OnPeriodicTick()
  1039. mock_future.done.assert_called()
  1040. mock_future.result.assert_called()
  1041. post_data_to_handler_async.assert_not_called()
  1042. # We reset and don't poll anymore
  1043. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1044. assert_that( poll_again, equal_to( False ) )
  1045. @YouCompleteMeInstance()
  1046. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1047. return_value = True )
  1048. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1049. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1050. def YouCompleteMe_OnPeriodicTick_Exception_test( post_data_to_handler_async,
  1051. validate_response_object,
  1052. filetype_completer_exists,
  1053. ycm ):
  1054. current_buffer = VimBuffer( '/current',
  1055. filetype = 'ycmtest',
  1056. number = 1 )
  1057. # Create the request and make the first poll; we expect no response
  1058. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1059. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1060. post_data_to_handler_async.assert_called()
  1061. post_data_to_handler_async.reset_mock()
  1062. # Poll again, but return an exception response
  1063. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1064. mock_response = MockAsyncServerResponseException( RuntimeError( 'test' ) )
  1065. with patch.dict( ycm._message_poll_requests, {} ):
  1066. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1067. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1068. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1069. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1070. mock_future.done.assert_called()
  1071. mock_future.result.assert_called()
  1072. post_data_to_handler_async.assert_not_called()
  1073. # We reset and don't poll anymore
  1074. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1075. @YouCompleteMeInstance()
  1076. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1077. return_value = True )
  1078. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1079. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1080. @patch( 'ycm.client.messages_request._HandlePollResponse' )
  1081. def YouCompleteMe_OnPeriodicTick_ValidResponse_test( handle_poll_response,
  1082. post_data_to_handler_async,
  1083. validate_response_object,
  1084. filetype_completer_exists,
  1085. ycm ):
  1086. current_buffer = VimBuffer( '/current',
  1087. filetype = 'ycmtest',
  1088. number = 1 )
  1089. # Create the request and make the first poll; we expect no response
  1090. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1091. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1092. post_data_to_handler_async.assert_called()
  1093. post_data_to_handler_async.reset_mock()
  1094. # Poll again, and return a _proper_ response (finally!).
  1095. # Note, _HandlePollResponse is tested independently (for simplicity)
  1096. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1097. mock_response = MockAsyncServerResponseDone( [] )
  1098. with patch.dict( ycm._message_poll_requests, {} ):
  1099. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1100. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1101. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1102. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1103. handle_poll_response.assert_called()
  1104. mock_future.done.assert_called()
  1105. mock_future.result.assert_called()
  1106. post_data_to_handler_async.assert_called() # Poll again!
  1107. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is not None )
  1108. @YouCompleteMeInstance()
  1109. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1110. def YouCompleteMe_OnCompleteDone_CompletionRequest_test( on_complete_done,
  1111. ycm ):
  1112. current_buffer = VimBuffer( 'current_buffer' )
  1113. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1114. ycm.SendCompletionRequest()
  1115. ycm.OnCompleteDone()
  1116. on_complete_done.assert_called()
  1117. @YouCompleteMeInstance()
  1118. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1119. def YouCompleteMe_OnCompleteDone_NoCompletionRequest_test( on_complete_done,
  1120. ycm ):
  1121. ycm.OnCompleteDone()
  1122. on_complete_done.assert_not_called()
  1123. @YouCompleteMeInstance()
  1124. def YouCompleteMe_ShouldResendFileParseRequest_NoParseRequest_test( ycm ):
  1125. current_buffer = VimBuffer( 'current_buffer' )
  1126. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1127. assert_that( ycm.ShouldResendFileParseRequest(), equal_to( False ) )