youcompleteme_test.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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.5+. "
  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.5+. "
  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. 'Logfile {0} does not exist.'.format( client_logfile ) )
  195. ycm.OnVimLeave()
  196. assert_that( not os.path.isfile( client_logfile ),
  197. 'Logfile {0} was not removed.'.format( client_logfile ) )
  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. 'Logfile {0} does not exist.'.format( client_logfile ) )
  203. ycm.OnVimLeave()
  204. assert_that( os.path.isfile( client_logfile ),
  205. 'Logfile {0} was removed.'.format( client_logfile ) )
  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. def YouCompleteMe_UpdateDiagnosticInterface_OldVim_test(
  580. post_vim_message, filetype_completer_exists, ycm ):
  581. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  582. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  583. 'g:ycm_enable_diagnostic_signs': 1,
  584. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  585. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  586. return_value = True )
  587. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  588. @patch( 'ycm.tests.test_utils.VIM_VERSION', Version( 8, 1, 614 ) )
  589. def YouCompleteMe_UpdateDiagnosticInterface_NewVim_test(
  590. post_vim_message, filetype_completer_exists, ycm ):
  591. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  592. @YouCompleteMeInstance( { 'g:ycm_enable_diagnostic_highlighting': 1 } )
  593. def YouCompleteMe_UpdateMatches_ClearDiagnosticMatchesInNewBuffer_test( ycm ):
  594. current_buffer = VimBuffer( 'buffer',
  595. filetype = 'c',
  596. number = 5 )
  597. test_utils.VIM_MATCHES_FOR_WINDOW[ 1 ] = [
  598. VimMatch( 'YcmWarningSection', '\\%3l\\%5c\\_.\\{-}\\%3l\\%7c' ),
  599. VimMatch( 'YcmWarningSection', '\\%3l\\%3c\\_.\\{-}\\%3l\\%9c' ),
  600. VimMatch( 'YcmErrorSection', '\\%3l\\%8c' )
  601. ]
  602. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  603. ycm.UpdateMatches()
  604. assert_that( test_utils.VIM_MATCHES_FOR_WINDOW,
  605. has_entries( { 1: empty() } ) )
  606. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  607. 'g:ycm_always_populate_location_list': 1,
  608. 'g:ycm_show_diagnostics_ui': 0,
  609. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  610. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  611. return_value = True )
  612. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  613. def YouCompleteMe_AsyncDiagnosticUpdate_UserDisabled_test(
  614. post_vim_message,
  615. filetype_completer_exists,
  616. ycm ):
  617. diagnostics = [
  618. {
  619. 'kind': 'ERROR',
  620. 'text': 'error text in current buffer',
  621. 'location': {
  622. 'filepath': '/current',
  623. 'line_num': 1,
  624. 'column_num': 1
  625. },
  626. 'location_extent': {
  627. 'start': {
  628. 'filepath': '/current',
  629. 'line_num': 1,
  630. 'column_num': 1,
  631. },
  632. 'end': {
  633. 'filepath': '/current',
  634. 'line_num': 1,
  635. 'column_num': 1,
  636. }
  637. },
  638. 'ranges': []
  639. },
  640. ]
  641. current_buffer = VimBuffer( '/current',
  642. filetype = 'ycmtest',
  643. contents = [ 'current' ] * 10,
  644. number = 1 )
  645. buffers = [ current_buffer ]
  646. windows = [ current_buffer ]
  647. # Register each buffer internally with YCM
  648. for current in buffers:
  649. with MockVimBuffers( buffers, [ current ] ):
  650. ycm.OnFileReadyToParse()
  651. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  652. new_callable = ExtendedMock ) as set_location_list_for_window:
  653. with MockVimBuffers( buffers, windows ):
  654. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  655. post_vim_message.assert_has_exact_calls( [] )
  656. set_location_list_for_window.assert_has_exact_calls( [] )
  657. assert_that(
  658. test_utils.VIM_MATCHES_FOR_WINDOW,
  659. empty()
  660. )
  661. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  662. 'g:ycm_always_populate_location_list': 1,
  663. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  664. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  665. return_value = True )
  666. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  667. def YouCompleteMe_AsyncDiagnosticUpdate_SingleFile_test(
  668. post_vim_message,
  669. filetype_completer_exists,
  670. ycm ):
  671. # This test simulates asynchronous diagnostic updates associated with a single
  672. # file (e.g. Translation Unit), but where the actual errors refer to other
  673. # open files and other non-open files. This is not strictly invalid, nor is it
  674. # completely normal, but it is supported and does work.
  675. # Contrast with the next test which sends the diagnostics filewise, which is
  676. # what the language server protocol will do.
  677. diagnostics = [
  678. {
  679. 'kind': 'ERROR',
  680. 'text': 'error text in current buffer',
  681. 'location': {
  682. 'filepath': '/current',
  683. 'line_num': 1,
  684. 'column_num': 1
  685. },
  686. 'location_extent': {
  687. 'start': {
  688. 'filepath': '/current',
  689. 'line_num': 1,
  690. 'column_num': 1,
  691. },
  692. 'end': {
  693. 'filepath': '/current',
  694. 'line_num': 1,
  695. 'column_num': 1,
  696. }
  697. },
  698. 'ranges': []
  699. },
  700. {
  701. 'kind': 'ERROR',
  702. 'text': 'error text in hidden buffer',
  703. 'location': {
  704. 'filepath': '/has_diags',
  705. 'line_num': 4,
  706. 'column_num': 2
  707. },
  708. 'location_extent': {
  709. 'start': {
  710. 'filepath': '/has_diags',
  711. 'line_num': 4,
  712. 'column_num': 2,
  713. },
  714. 'end': {
  715. 'filepath': '/has_diags',
  716. 'line_num': 4,
  717. 'column_num': 2,
  718. }
  719. },
  720. 'ranges': []
  721. },
  722. {
  723. 'kind': 'ERROR',
  724. 'text': 'error text in buffer not open in Vim',
  725. 'location': {
  726. 'filepath': '/not_open',
  727. 'line_num': 8,
  728. 'column_num': 4
  729. },
  730. 'location_extent': {
  731. 'start': {
  732. 'filepath': '/not_open',
  733. 'line_num': 8,
  734. 'column_num': 4,
  735. },
  736. 'end': {
  737. 'filepath': '/not_open',
  738. 'line_num': 8,
  739. 'column_num': 4,
  740. }
  741. },
  742. 'ranges': []
  743. }
  744. ]
  745. current_buffer = VimBuffer( '/current',
  746. filetype = 'ycmtest',
  747. contents = [ 'current' ] * 10,
  748. number = 1 )
  749. no_diags_buffer = VimBuffer( '/no_diags',
  750. filetype = 'ycmtest',
  751. contents = [ 'nodiags' ] * 10,
  752. number = 2 )
  753. hidden_buffer = VimBuffer( '/has_diags',
  754. filetype = 'ycmtest',
  755. contents = [ 'hasdiags' ] * 10,
  756. number = 3 )
  757. buffers = [ current_buffer, no_diags_buffer, hidden_buffer ]
  758. windows = [ current_buffer, no_diags_buffer ]
  759. # Register each buffer internally with YCM
  760. for current in buffers:
  761. with MockVimBuffers( buffers, [ current ] ):
  762. ycm.OnFileReadyToParse()
  763. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  764. new_callable = ExtendedMock ) as set_location_list_for_window:
  765. with MockVimBuffers( buffers, windows ):
  766. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  767. # We update the diagnostic on the current cursor position
  768. post_vim_message.assert_has_exact_calls( [
  769. call( "error text in current buffer", truncate = True, warning = False ),
  770. ] )
  771. # Ensure we included all the diags though
  772. set_location_list_for_window.assert_has_exact_calls( [
  773. call( 1, [
  774. {
  775. 'lnum': 1,
  776. 'col': 1,
  777. 'bufnr': 1,
  778. 'valid': 1,
  779. 'type': 'E',
  780. 'text': 'error text in current buffer',
  781. },
  782. {
  783. 'lnum': 4,
  784. 'col': 2,
  785. 'bufnr': 3,
  786. 'valid': 1,
  787. 'type': 'E',
  788. 'text': 'error text in hidden buffer',
  789. },
  790. {
  791. 'lnum': 8,
  792. 'col': 4,
  793. 'bufnr': -1, # sic: Our mocked bufnr function actually returns -1,
  794. # even though YCM is passing "create if needed".
  795. # FIXME? we shouldn't do that, and we should pass
  796. # filename instead
  797. 'valid': 1,
  798. 'type': 'E',
  799. 'text': 'error text in buffer not open in Vim'
  800. }
  801. ] )
  802. ] )
  803. assert_that(
  804. test_utils.VIM_MATCHES_FOR_WINDOW,
  805. has_entries( {
  806. 1: contains_exactly(
  807. VimMatch( 'YcmErrorSection', '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
  808. )
  809. } )
  810. )
  811. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  812. 'g:ycm_always_populate_location_list': 1,
  813. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  814. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  815. return_value = True )
  816. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  817. def YouCompleteMe_AsyncDiagnosticUpdate_PerFile_test(
  818. post_vim_message,
  819. filetype_completer_exists,
  820. ycm ):
  821. # This test simulates asynchronous diagnostic updates which are delivered per
  822. # file, including files which are open and files which are not.
  823. # Ordered to ensure that the calls to update are deterministic
  824. diagnostics_per_file = [
  825. ( '/current', [ {
  826. 'kind': 'ERROR',
  827. 'text': 'error text in current buffer',
  828. 'location': {
  829. 'filepath': '/current',
  830. 'line_num': 1,
  831. 'column_num': 1
  832. },
  833. 'location_extent': {
  834. 'start': {
  835. 'filepath': '/current',
  836. 'line_num': 1,
  837. 'column_num': 1,
  838. },
  839. 'end': {
  840. 'filepath': '/current',
  841. 'line_num': 1,
  842. 'column_num': 1,
  843. }
  844. },
  845. 'ranges': [],
  846. } ] ),
  847. ( '/separate_window', [ {
  848. 'kind': 'ERROR',
  849. 'text': 'error text in a buffer open in a separate window',
  850. 'location': {
  851. 'filepath': '/separate_window',
  852. 'line_num': 3,
  853. 'column_num': 3
  854. },
  855. 'location_extent': {
  856. 'start': {
  857. 'filepath': '/separate_window',
  858. 'line_num': 3,
  859. 'column_num': 3,
  860. },
  861. 'end': {
  862. 'filepath': '/separate_window',
  863. 'line_num': 3,
  864. 'column_num': 3,
  865. }
  866. },
  867. 'ranges': []
  868. } ] ),
  869. ( '/hidden', [ {
  870. 'kind': 'ERROR',
  871. 'text': 'error text in hidden buffer',
  872. 'location': {
  873. 'filepath': '/hidden',
  874. 'line_num': 4,
  875. 'column_num': 2
  876. },
  877. 'location_extent': {
  878. 'start': {
  879. 'filepath': '/hidden',
  880. 'line_num': 4,
  881. 'column_num': 2,
  882. },
  883. 'end': {
  884. 'filepath': '/hidden',
  885. 'line_num': 4,
  886. 'column_num': 2,
  887. }
  888. },
  889. 'ranges': []
  890. } ] ),
  891. ( '/not_open', [ {
  892. 'kind': 'ERROR',
  893. 'text': 'error text in buffer not open in Vim',
  894. 'location': {
  895. 'filepath': '/not_open',
  896. 'line_num': 8,
  897. 'column_num': 4
  898. },
  899. 'location_extent': {
  900. 'start': {
  901. 'filepath': '/not_open',
  902. 'line_num': 8,
  903. 'column_num': 4,
  904. },
  905. 'end': {
  906. 'filepath': '/not_open',
  907. 'line_num': 8,
  908. 'column_num': 4,
  909. }
  910. },
  911. 'ranges': []
  912. } ] )
  913. ]
  914. current_buffer = VimBuffer( '/current',
  915. filetype = 'ycmtest',
  916. contents = [ 'current' ] * 10,
  917. number = 1 )
  918. no_diags_buffer = VimBuffer( '/no_diags',
  919. filetype = 'ycmtest',
  920. contents = [ 'no_diags' ] * 10,
  921. number = 2 )
  922. separate_window = VimBuffer( '/separate_window',
  923. filetype = 'ycmtest',
  924. contents = [ 'separate_window' ] * 10,
  925. number = 3 )
  926. hidden_buffer = VimBuffer( '/hidden',
  927. filetype = 'ycmtest',
  928. contents = [ 'hidden' ] * 10,
  929. number = 4 )
  930. buffers = [
  931. current_buffer,
  932. no_diags_buffer,
  933. separate_window,
  934. hidden_buffer
  935. ]
  936. windows = [
  937. current_buffer,
  938. no_diags_buffer,
  939. separate_window
  940. ]
  941. # Register each buffer internally with YCM
  942. for current in buffers:
  943. with MockVimBuffers( buffers, [ current ] ):
  944. ycm.OnFileReadyToParse()
  945. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  946. new_callable = ExtendedMock ) as set_location_list_for_window:
  947. with MockVimBuffers( buffers, windows ):
  948. for filename, diagnostics in diagnostics_per_file:
  949. ycm.UpdateWithNewDiagnosticsForFile( filename, diagnostics )
  950. # We update the diagnostic on the current cursor position
  951. post_vim_message.assert_has_exact_calls( [
  952. call( "error text in current buffer", truncate = True, warning = False ),
  953. ] )
  954. # Ensure we included all the diags though
  955. set_location_list_for_window.assert_has_exact_calls( [
  956. call( 1, [
  957. {
  958. 'lnum': 1,
  959. 'col': 1,
  960. 'bufnr': 1,
  961. 'valid': 1,
  962. 'type': 'E',
  963. 'text': 'error text in current buffer',
  964. },
  965. ] ),
  966. call( 3, [
  967. {
  968. 'lnum': 3,
  969. 'col': 3,
  970. 'bufnr': 3,
  971. 'valid': 1,
  972. 'type': 'E',
  973. 'text': 'error text in a buffer open in a separate window',
  974. },
  975. ] )
  976. ] )
  977. # FIXME: diagnostic matches in windows other than the current one are not
  978. # updated.
  979. assert_that(
  980. test_utils.VIM_MATCHES_FOR_WINDOW,
  981. has_entries( {
  982. 1: contains_exactly(
  983. VimMatch( 'YcmErrorSection', '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
  984. )
  985. } )
  986. )
  987. @YouCompleteMeInstance()
  988. def YouCompleteMe_OnPeriodicTick_ServerNotRunning_test( ycm ):
  989. with patch.object( ycm, 'IsServerAlive', return_value = False ):
  990. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  991. @YouCompleteMeInstance()
  992. def YouCompleteMe_OnPeriodicTick_ServerNotReady_test( ycm ):
  993. with patch.object( ycm, 'IsServerAlive', return_value = True ):
  994. with patch.object( ycm, 'IsServerReady', return_value = False ):
  995. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  996. @YouCompleteMeInstance()
  997. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  998. return_value = True )
  999. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1000. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1001. def YouCompleteMe_OnPeriodicTick_DontRetry_test(
  1002. post_data_to_handler_async,
  1003. validate_response_object,
  1004. filetype_completer_exists,
  1005. ycm ):
  1006. current_buffer = VimBuffer( '/current',
  1007. filetype = 'ycmtest',
  1008. number = 1 )
  1009. # Create the request and make the first poll; we expect no response
  1010. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1011. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1012. post_data_to_handler_async.assert_called()
  1013. assert ycm._message_poll_requests[ 'ycmtest' ] is not None
  1014. post_data_to_handler_async.reset_mock()
  1015. # OK that sent the request, now poll to check if it is complete (say it is
  1016. # not)
  1017. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1018. mock_response = MockAsyncServerResponseInProgress()
  1019. with patch.dict( ycm._message_poll_requests, {} ):
  1020. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1021. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1022. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1023. poll_again = ycm.OnPeriodicTick()
  1024. mock_future.done.assert_called()
  1025. mock_future.result.assert_not_called()
  1026. assert_that( poll_again, equal_to( True ) )
  1027. # Poll again, but return a response (telling us to stop polling)
  1028. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1029. mock_response = MockAsyncServerResponseDone( False )
  1030. with patch.dict( ycm._message_poll_requests, {} ):
  1031. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1032. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1033. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1034. poll_again = ycm.OnPeriodicTick()
  1035. mock_future.done.assert_called()
  1036. mock_future.result.assert_called()
  1037. post_data_to_handler_async.assert_not_called()
  1038. # We reset and don't poll anymore
  1039. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1040. assert_that( poll_again, equal_to( False ) )
  1041. @YouCompleteMeInstance()
  1042. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1043. return_value = True )
  1044. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1045. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1046. def YouCompleteMe_OnPeriodicTick_Exception_test( post_data_to_handler_async,
  1047. validate_response_object,
  1048. filetype_completer_exists,
  1049. ycm ):
  1050. current_buffer = VimBuffer( '/current',
  1051. filetype = 'ycmtest',
  1052. number = 1 )
  1053. # Create the request and make the first poll; we expect no response
  1054. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1055. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1056. post_data_to_handler_async.assert_called()
  1057. post_data_to_handler_async.reset_mock()
  1058. # Poll again, but return an exception response
  1059. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1060. mock_response = MockAsyncServerResponseException( RuntimeError( 'test' ) )
  1061. with patch.dict( ycm._message_poll_requests, {} ):
  1062. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1063. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1064. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1065. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1066. mock_future.done.assert_called()
  1067. mock_future.result.assert_called()
  1068. post_data_to_handler_async.assert_not_called()
  1069. # We reset and don't poll anymore
  1070. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1071. @YouCompleteMeInstance()
  1072. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1073. return_value = True )
  1074. @patch( 'ycm.client.base_request._ValidateResponseObject', return_value = True )
  1075. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1076. @patch( 'ycm.client.messages_request._HandlePollResponse' )
  1077. def YouCompleteMe_OnPeriodicTick_ValidResponse_test( handle_poll_response,
  1078. post_data_to_handler_async,
  1079. validate_response_object,
  1080. filetype_completer_exists,
  1081. ycm ):
  1082. current_buffer = VimBuffer( '/current',
  1083. filetype = 'ycmtest',
  1084. number = 1 )
  1085. # Create the request and make the first poll; we expect no response
  1086. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1087. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1088. post_data_to_handler_async.assert_called()
  1089. post_data_to_handler_async.reset_mock()
  1090. # Poll again, and return a _proper_ response (finally!).
  1091. # Note, _HandlePollResponse is tested independently (for simplicity)
  1092. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  1093. mock_response = MockAsyncServerResponseDone( [] )
  1094. with patch.dict( ycm._message_poll_requests, {} ):
  1095. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll( v.current.buffer )
  1096. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1097. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1098. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1099. handle_poll_response.assert_called()
  1100. mock_future.done.assert_called()
  1101. mock_future.result.assert_called()
  1102. post_data_to_handler_async.assert_called() # Poll again!
  1103. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is not None )
  1104. @YouCompleteMeInstance()
  1105. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1106. def YouCompleteMe_OnCompleteDone_CompletionRequest_test( on_complete_done,
  1107. ycm ):
  1108. current_buffer = VimBuffer( 'current_buffer' )
  1109. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1110. ycm.SendCompletionRequest()
  1111. ycm.OnCompleteDone()
  1112. on_complete_done.assert_called()
  1113. @YouCompleteMeInstance()
  1114. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1115. def YouCompleteMe_OnCompleteDone_NoCompletionRequest_test( on_complete_done,
  1116. ycm ):
  1117. ycm.OnCompleteDone()
  1118. on_complete_done.assert_not_called()
  1119. @YouCompleteMeInstance()
  1120. def YouCompleteMe_ShouldResendFileParseRequest_NoParseRequest_test( ycm ):
  1121. current_buffer = VimBuffer( 'current_buffer' )
  1122. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1123. assert_that( ycm.ShouldResendFileParseRequest(), equal_to( False ) )