youcompleteme_test.py 42 KB

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