youcompleteme_test.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. # Copyright (C) 2016-2017 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 )
  25. MockVimModule()
  26. import os
  27. import sys
  28. from hamcrest import ( assert_that, contains, empty, equal_to, is_in, is_not,
  29. matches_regexp )
  30. from mock import call, MagicMock, patch
  31. from ycm.paths import _PathToPythonUsedDuringBuild
  32. from ycm.youcompleteme import YouCompleteMe
  33. from ycm.tests import ( MakeUserOptions, StopServer, test_utils,
  34. WaitUntilReady, YouCompleteMeInstance )
  35. from ycm.client.base_request import _LoadExtraConfFile
  36. from ycmd.responses import ServerError
  37. @YouCompleteMeInstance()
  38. def YouCompleteMe_YcmCoreNotImported_test( ycm ):
  39. assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) )
  40. @patch( 'ycm.vimsupport.PostVimMessage' )
  41. def YouCompleteMe_InvalidPythonInterpreterPath_test( post_vim_message ):
  42. try:
  43. with patch( 'ycm.tests.test_utils.server_python_interpreter',
  44. '/invalid/path/to/python' ):
  45. ycm = YouCompleteMe( MakeUserOptions() )
  46. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  47. post_vim_message.assert_called_once_with(
  48. "Unable to start the ycmd server. "
  49. "Path in 'g:ycm_server_python_interpreter' option does not point "
  50. "to a valid Python 2.6+ or 3.3+. "
  51. "Correct the error then restart the server with ':YcmRestartServer'." )
  52. post_vim_message.reset_mock()
  53. with patch( 'ycm.tests.test_utils.server_python_interpreter',
  54. _PathToPythonUsedDuringBuild() ):
  55. ycm.RestartServer()
  56. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  57. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  58. finally:
  59. WaitUntilReady()
  60. StopServer( ycm )
  61. @patch( 'ycmd.utils.PathToFirstExistingExecutable', return_value = None )
  62. @patch( 'ycm.paths._EndsWithPython', return_value = False )
  63. @patch( 'ycm.vimsupport.PostVimMessage' )
  64. def YouCompleteMe_NoPythonInterpreterFound_test( post_vim_message, *args ):
  65. try:
  66. with patch( 'ycmd.utils.ReadFile', side_effect = IOError ):
  67. ycm = YouCompleteMe( MakeUserOptions() )
  68. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  69. post_vim_message.assert_called_once_with(
  70. "Unable to start the ycmd server. Cannot find Python 2.6+ or 3.3+. "
  71. "Set the 'g:ycm_server_python_interpreter' option to a Python "
  72. "interpreter path. "
  73. "Correct the error then restart the server with ':YcmRestartServer'." )
  74. post_vim_message.reset_mock()
  75. with patch( 'ycm.tests.test_utils.server_python_interpreter',
  76. _PathToPythonUsedDuringBuild() ):
  77. ycm.RestartServer()
  78. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  79. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  80. finally:
  81. WaitUntilReady()
  82. StopServer( ycm )
  83. @YouCompleteMeInstance()
  84. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  85. def RunNotifyUserIfServerCrashed( ycm, test, post_vim_message ):
  86. StopServer( ycm )
  87. ycm._logger = MagicMock( autospec = True )
  88. ycm._server_popen = MagicMock( autospec = True )
  89. ycm._server_popen.poll.return_value = test[ 'return_code' ]
  90. ycm.OnFileReadyToParse()
  91. assert_that( ycm._logger.error.call_args[ 0 ][ 0 ],
  92. test[ 'expected_message' ] )
  93. assert_that( post_vim_message.call_args[ 0 ][ 0 ],
  94. test[ 'expected_message' ] )
  95. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedCore_test():
  96. message = ( "The ycmd server SHUT DOWN \(restart with ':YcmRestartServer'\). "
  97. "Unexpected error while loading the YCM core library. Type "
  98. "':YcmToggleLogs ycmd_\d+_stderr_.+.log' to check the logs." )
  99. RunNotifyUserIfServerCrashed( {
  100. 'return_code': 3,
  101. 'expected_message': matches_regexp( message )
  102. } )
  103. def YouCompleteMe_NotifyUserIfServerCrashed_MissingCore_test():
  104. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  105. "YCM core library not detected; you need to compile YCM before "
  106. "using it. Follow the instructions in the documentation." )
  107. RunNotifyUserIfServerCrashed( {
  108. 'return_code': 4,
  109. 'expected_message': equal_to( message )
  110. } )
  111. def YouCompleteMe_NotifyUserIfServerCrashed_Python2Core_test():
  112. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  113. "YCM core library compiled for Python 2 but loaded in Python 3. "
  114. "Set the 'g:ycm_server_python_interpreter' option to a Python 2 "
  115. "interpreter path." )
  116. RunNotifyUserIfServerCrashed( {
  117. 'return_code': 5,
  118. 'expected_message': equal_to( message )
  119. } )
  120. def YouCompleteMe_NotifyUserIfServerCrashed_Python3Core_test():
  121. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  122. "YCM core library compiled for Python 3 but loaded in Python 2. "
  123. "Set the 'g:ycm_server_python_interpreter' option to a Python 3 "
  124. "interpreter path." )
  125. RunNotifyUserIfServerCrashed( {
  126. 'return_code': 6,
  127. 'expected_message': equal_to( message )
  128. } )
  129. def YouCompleteMe_NotifyUserIfServerCrashed_OutdatedCore_test():
  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( {
  134. 'return_code': 7,
  135. 'expected_message': equal_to( message )
  136. } )
  137. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedExitCode_test():
  138. message = ( "The ycmd server SHUT DOWN \(restart with ':YcmRestartServer'\). "
  139. "Unexpected exit code 1. Type "
  140. "':YcmToggleLogs ycmd_\d+_stderr_.+.log' to check the logs." )
  141. RunNotifyUserIfServerCrashed( {
  142. 'return_code': 1,
  143. 'expected_message': matches_regexp( message )
  144. } )
  145. @YouCompleteMeInstance( { 'extra_conf_vim_data': [ 'tempname()' ] } )
  146. def YouCompleteMe_DebugInfo_ServerRunning_test( ycm ):
  147. dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
  148. buf_name = os.path.join( dir_of_script, 'testdata', 'test.cpp' )
  149. extra_conf = os.path.join( dir_of_script, 'testdata', '.ycm_extra_conf.py' )
  150. _LoadExtraConfFile( extra_conf )
  151. current_buffer = VimBuffer( buf_name, filetype='cpp' )
  152. with MockVimBuffers( [ current_buffer ], current_buffer ):
  153. assert_that(
  154. ycm.DebugInfo(),
  155. matches_regexp(
  156. 'Client logfile: .+\n'
  157. 'Server Python interpreter: .+\n'
  158. 'Server Python version: .+\n'
  159. 'Server has Clang support compiled in: '
  160. '(?P<CLANG>True)?(?(CLANG)|False)\n'
  161. 'Clang version: .+\n'
  162. 'Extra configuration file found and loaded\n'
  163. 'Extra configuration path: .*testdata[/\\\\]\\.ycm_extra_conf\\.py\n'
  164. '(?(CLANG)C-family completer debug information:\n'
  165. ' Compilation database path: None\n'
  166. ' Flags: \\[\'_TEMP_FILE_\'.*\\]\n)'
  167. 'Server running at: .+\n'
  168. 'Server process ID: \d+\n'
  169. 'Server logfiles:\n'
  170. ' .+\n'
  171. ' .+' )
  172. )
  173. @YouCompleteMeInstance()
  174. def YouCompleteMe_DebugInfo_ServerNotRunning_test( ycm ):
  175. StopServer( ycm )
  176. current_buffer = VimBuffer( 'current_buffer' )
  177. with MockVimBuffers( [ current_buffer ], current_buffer ):
  178. assert_that(
  179. ycm.DebugInfo(),
  180. matches_regexp(
  181. 'Client logfile: .+\n'
  182. 'Server errored, no debug info from server\n'
  183. 'Server running at: .+\n'
  184. 'Server process ID: \d+\n'
  185. 'Server logfiles:\n'
  186. ' .+\n'
  187. ' .+' )
  188. )
  189. @YouCompleteMeInstance()
  190. def YouCompleteMe_OnVimLeave_RemoveClientLogfileByDefault_test( ycm ):
  191. client_logfile = ycm._client_logfile
  192. assert_that( os.path.isfile( client_logfile ),
  193. 'Logfile {0} does not exist.'.format( client_logfile ) )
  194. ycm.OnVimLeave()
  195. assert_that( not os.path.isfile( client_logfile ),
  196. 'Logfile {0} was not removed.'.format( client_logfile ) )
  197. @YouCompleteMeInstance( { 'keep_logfiles': 1 } )
  198. def YouCompleteMe_OnVimLeave_KeepClientLogfile_test( ycm ):
  199. client_logfile = ycm._client_logfile
  200. assert_that( os.path.isfile( client_logfile ),
  201. 'Logfile {0} does not exist.'.format( client_logfile ) )
  202. ycm.OnVimLeave()
  203. assert_that( os.path.isfile( client_logfile ),
  204. 'Logfile {0} was removed.'.format( client_logfile ) )
  205. @YouCompleteMeInstance()
  206. @patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
  207. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  208. def YouCompleteMe_ToggleLogs_WithParameters_test( ycm,
  209. open_filename,
  210. close_buffers_for_filename ):
  211. logfile_buffer = VimBuffer( ycm._client_logfile, window = 1 )
  212. with MockVimBuffers( [ logfile_buffer ], logfile_buffer ):
  213. ycm.ToggleLogs( os.path.basename( ycm._client_logfile ),
  214. 'nonexisting_logfile',
  215. os.path.basename( ycm._server_stdout ) )
  216. open_filename.assert_has_exact_calls( [
  217. call( ycm._server_stdout, { 'size': 12,
  218. 'watch': True,
  219. 'fix': True,
  220. 'focus': False,
  221. 'position': 'end' } )
  222. ] )
  223. close_buffers_for_filename.assert_has_exact_calls( [
  224. call( ycm._client_logfile )
  225. ] )
  226. @YouCompleteMeInstance()
  227. # Select the second item of the list which is the ycmd stderr logfile.
  228. @patch( 'ycm.vimsupport.SelectFromList', return_value = 1 )
  229. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  230. def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileNotAlreadyOpen_test(
  231. ycm, open_filename, *args ):
  232. current_buffer = VimBuffer( 'current_buffer' )
  233. with MockVimBuffers( [ current_buffer ], current_buffer ):
  234. ycm.ToggleLogs()
  235. open_filename.assert_has_exact_calls( [
  236. call( ycm._server_stderr, { 'size': 12,
  237. 'watch': True,
  238. 'fix': True,
  239. 'focus': False,
  240. 'position': 'end' } )
  241. ] )
  242. @YouCompleteMeInstance()
  243. # Select the third item of the list which is the ycmd stdout logfile.
  244. @patch( 'ycm.vimsupport.SelectFromList', return_value = 2 )
  245. @patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
  246. def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileAlreadyOpen_test(
  247. ycm, close_buffers_for_filename, *args ):
  248. logfile_buffer = VimBuffer( ycm._server_stdout, window = 1 )
  249. with MockVimBuffers( [ logfile_buffer ], logfile_buffer ):
  250. ycm.ToggleLogs()
  251. close_buffers_for_filename.assert_has_exact_calls( [
  252. call( ycm._server_stdout )
  253. ] )
  254. @YouCompleteMeInstance()
  255. @patch( 'ycm.vimsupport.SelectFromList',
  256. side_effect = RuntimeError( 'Error message' ) )
  257. @patch( 'ycm.vimsupport.PostVimMessage' )
  258. def YouCompleteMe_ToggleLogs_WithoutParameters_NoSelection_test(
  259. ycm, post_vim_message, *args ):
  260. current_buffer = VimBuffer( 'current_buffer' )
  261. with MockVimBuffers( [ current_buffer ], current_buffer ):
  262. ycm.ToggleLogs()
  263. assert_that(
  264. # Argument passed to PostVimMessage.
  265. post_vim_message.call_args[ 0 ][ 0 ],
  266. equal_to( 'Error message' )
  267. )
  268. @YouCompleteMeInstance()
  269. def YouCompleteMe_GetDefinedSubcommands_ListFromServer_test( ycm ):
  270. current_buffer = VimBuffer( 'buffer' )
  271. with MockVimBuffers( [ current_buffer ], current_buffer ):
  272. with patch( 'ycm.client.base_request.JsonFromFuture',
  273. return_value = [ 'SomeCommand', 'AnotherCommand' ] ):
  274. assert_that(
  275. ycm.GetDefinedSubcommands(),
  276. contains(
  277. 'SomeCommand',
  278. 'AnotherCommand'
  279. )
  280. )
  281. @YouCompleteMeInstance()
  282. @patch( 'ycm.client.base_request._logger', autospec = True )
  283. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  284. def YouCompleteMe_GetDefinedSubcommands_ErrorFromServer_test( ycm,
  285. post_vim_message,
  286. logger ):
  287. current_buffer = VimBuffer( 'buffer' )
  288. with MockVimBuffers( [ current_buffer ], current_buffer ):
  289. with patch( 'ycm.client.base_request.JsonFromFuture',
  290. side_effect = ServerError( 'Server error' ) ):
  291. result = ycm.GetDefinedSubcommands()
  292. logger.exception.assert_called_with( 'Error while handling server response' )
  293. post_vim_message.assert_has_exact_calls( [
  294. call( 'Server error', truncate = False )
  295. ] )
  296. assert_that( result, empty() )
  297. @YouCompleteMeInstance()
  298. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  299. def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test(
  300. ycm, post_vim_message ):
  301. current_buffer = VimBuffer( 'buffer' )
  302. with MockVimBuffers( [ current_buffer ], current_buffer ):
  303. with patch( 'ycm.client.base_request.JsonFromFuture',
  304. return_value = { 'message': 'some_detailed_diagnostic' } ):
  305. ycm.ShowDetailedDiagnostic(),
  306. post_vim_message.assert_has_exact_calls( [
  307. call( 'some_detailed_diagnostic', warning = False )
  308. ] )
  309. @YouCompleteMeInstance()
  310. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  311. def YouCompleteMe_ShowDiagnostics_FiletypeNotSupported_test( ycm,
  312. post_vim_message ):
  313. current_buffer = VimBuffer( 'buffer', filetype = 'not_supported' )
  314. with MockVimBuffers( [ current_buffer ], current_buffer ):
  315. ycm.ShowDiagnostics()
  316. post_vim_message.assert_called_once_with(
  317. 'Native filetype completion not supported for current file, '
  318. 'cannot force recompilation.', warning = False )
  319. @YouCompleteMeInstance()
  320. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  321. return_value = True )
  322. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  323. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  324. def YouCompleteMe_ShowDiagnostics_NoDiagnosticsDetected_test(
  325. ycm, set_location_list, post_vim_message, *args ):
  326. current_buffer = VimBuffer( 'buffer', filetype = 'cpp' )
  327. with MockVimBuffers( [ current_buffer ], current_buffer ):
  328. with patch( 'ycm.client.event_notification.EventNotification.Response',
  329. return_value = {} ):
  330. ycm.ShowDiagnostics()
  331. post_vim_message.assert_has_exact_calls( [
  332. call( 'Forcing compilation, this will block Vim until done.',
  333. warning = False ),
  334. call( 'Diagnostics refreshed', warning = False ),
  335. call( 'No warnings or errors detected.', warning = False )
  336. ] )
  337. set_location_list.assert_called_once_with( [] )
  338. @YouCompleteMeInstance( { 'log_level': 'debug',
  339. 'keep_logfiles': 1,
  340. 'open_loclist_on_ycm_diags': 0 } )
  341. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  342. return_value = True )
  343. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  344. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  345. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList_test(
  346. ycm, set_location_list, post_vim_message, *args ):
  347. diagnostic = {
  348. 'kind': 'ERROR',
  349. 'text': 'error text',
  350. 'location': {
  351. 'filepath': 'buffer',
  352. 'line_num': 19,
  353. 'column_num': 2
  354. }
  355. }
  356. current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
  357. with MockVimBuffers( [ current_buffer ], current_buffer ):
  358. with patch( 'ycm.client.event_notification.EventNotification.Response',
  359. return_value = [ diagnostic ] ):
  360. ycm.ShowDiagnostics()
  361. post_vim_message.assert_has_exact_calls( [
  362. call( 'Forcing compilation, this will block Vim until done.',
  363. warning = False ),
  364. call( 'Diagnostics refreshed', warning = False )
  365. ] )
  366. set_location_list.assert_called_once_with( [ {
  367. 'bufnr': 3,
  368. 'lnum': 19,
  369. 'col': 2,
  370. 'text': 'error text',
  371. 'type': 'E',
  372. 'valid': 1
  373. } ] )
  374. @YouCompleteMeInstance( { 'open_loclist_on_ycm_diags': 1 } )
  375. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  376. return_value = True )
  377. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  378. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  379. @patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
  380. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList_test(
  381. ycm, open_location_list, set_location_list, post_vim_message, *args ):
  382. diagnostic = {
  383. 'kind': 'ERROR',
  384. 'text': 'error text',
  385. 'location': {
  386. 'filepath': 'buffer',
  387. 'line_num': 19,
  388. 'column_num': 2
  389. }
  390. }
  391. current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
  392. with MockVimBuffers( [ current_buffer ], current_buffer ):
  393. with patch( 'ycm.client.event_notification.EventNotification.Response',
  394. return_value = [ diagnostic ] ):
  395. ycm.ShowDiagnostics()
  396. post_vim_message.assert_has_exact_calls( [
  397. call( 'Forcing compilation, this will block Vim until done.',
  398. warning = False ),
  399. call( 'Diagnostics refreshed', warning = False )
  400. ] )
  401. set_location_list.assert_called_once_with( [ {
  402. 'bufnr': 3,
  403. 'lnum': 19,
  404. 'col': 2,
  405. 'text': 'error text',
  406. 'type': 'E',
  407. 'valid': 1
  408. } ] )
  409. open_location_list.assert_called_once_with( focus = True )
  410. @YouCompleteMeInstance( { 'echo_current_diagnostic': 1,
  411. 'enable_diagnostic_signs': 1,
  412. 'enable_diagnostic_highlighting': 1 } )
  413. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  414. return_value = True )
  415. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  416. @patch( 'vim.command', new_callable = ExtendedMock )
  417. def YouCompleteMe_UpdateDiagnosticInterface_PrioritizeErrorsOverWarnings_test(
  418. ycm, vim_command, post_vim_message, *args ):
  419. contents = """int main() {
  420. int x, y;
  421. x == y
  422. }"""
  423. # List of diagnostics returned by ycmd for the above code.
  424. diagnostics = [ {
  425. 'kind': 'ERROR',
  426. 'text': "expected ';' after expression",
  427. 'location': {
  428. 'filepath': 'buffer',
  429. 'line_num': 3,
  430. 'column_num': 9
  431. },
  432. # Looks strange but this is really what ycmd is returning.
  433. 'location_extent': {
  434. 'start': {
  435. 'filepath': '',
  436. 'line_num': 0,
  437. 'column_num': 0,
  438. },
  439. 'end': {
  440. 'filepath': '',
  441. 'line_num': 0,
  442. 'column_num': 0,
  443. }
  444. },
  445. 'ranges': [],
  446. 'fixit_available': True
  447. }, {
  448. 'kind': 'WARNING',
  449. 'text': 'equality comparison result unused',
  450. 'location': {
  451. 'filepath': 'buffer',
  452. 'line_num': 3,
  453. 'column_num': 7,
  454. },
  455. 'location_extent': {
  456. 'start': {
  457. 'filepath': 'buffer',
  458. 'line_num': 3,
  459. 'column_num': 5,
  460. },
  461. 'end': {
  462. 'filepath': 'buffer',
  463. 'line_num': 3,
  464. 'column_num': 7,
  465. }
  466. },
  467. 'ranges': [ {
  468. 'start': {
  469. 'filepath': 'buffer',
  470. 'line_num': 3,
  471. 'column_num': 3,
  472. },
  473. 'end': {
  474. 'filepath': 'buffer',
  475. 'line_num': 3,
  476. 'column_num': 9,
  477. }
  478. } ],
  479. 'fixit_available': True
  480. } ]
  481. current_buffer = VimBuffer( 'buffer',
  482. filetype = 'c',
  483. contents = contents.splitlines(),
  484. number = 5,
  485. window = 2 )
  486. test_utils.VIM_MATCHES = []
  487. with MockVimBuffers( [ current_buffer ], current_buffer, ( 3, 1 ) ):
  488. with patch( 'ycm.client.event_notification.EventNotification.Response',
  489. return_value = diagnostics ):
  490. ycm.OnFileReadyToParse()
  491. ycm.HandleFileParseRequest( block = True )
  492. # The error on the current line is echoed, not the warning.
  493. post_vim_message.assert_called_once_with(
  494. "expected ';' after expression (FixIt)",
  495. truncate = True, warning = False )
  496. # Error match is added after warning matches.
  497. assert_that(
  498. test_utils.VIM_MATCHES,
  499. contains(
  500. VimMatch( 'YcmWarningSection', '\%3l\%5c\_.\{-}\%3l\%7c' ),
  501. VimMatch( 'YcmWarningSection', '\%3l\%3c\_.\{-}\%3l\%9c' ),
  502. VimMatch( 'YcmErrorSection', '\%3l\%8c' )
  503. )
  504. )
  505. # Only the error sign is placed.
  506. vim_command.assert_has_exact_calls( [
  507. call( 'sign place 1 name=YcmError line=3 buffer=5' ),
  508. ] )
  509. # The error is not echoed again when moving the cursor along the line.
  510. with MockVimBuffers( [ current_buffer ], current_buffer, ( 3, 2 ) ):
  511. post_vim_message.reset_mock()
  512. ycm.OnCursorMoved()
  513. post_vim_message.assert_not_called()
  514. # The error is cleared when moving the cursor to another line.
  515. with MockVimBuffers( [ current_buffer ], current_buffer, ( 2, 2 ) ):
  516. post_vim_message.reset_mock()
  517. ycm.OnCursorMoved()
  518. post_vim_message.assert_called_once_with( "", warning = False )
  519. # The error is echoed when moving the cursor back.
  520. with MockVimBuffers( [ current_buffer ], current_buffer, ( 3, 2 ) ):
  521. post_vim_message.reset_mock()
  522. ycm.OnCursorMoved()
  523. post_vim_message.assert_called_once_with(
  524. "expected ';' after expression (FixIt)",
  525. truncate = True, warning = False )
  526. vim_command.reset_mock()
  527. with patch( 'ycm.client.event_notification.EventNotification.Response',
  528. return_value = diagnostics[ 1 : ] ):
  529. ycm.OnFileReadyToParse()
  530. ycm.HandleFileParseRequest( block = True )
  531. assert_that(
  532. test_utils.VIM_MATCHES,
  533. contains(
  534. VimMatch( 'YcmWarningSection', '\%3l\%5c\_.\{-}\%3l\%7c' ),
  535. VimMatch( 'YcmWarningSection', '\%3l\%3c\_.\{-}\%3l\%9c' )
  536. )
  537. )
  538. vim_command.assert_has_exact_calls( [
  539. call( 'sign place 2 name=YcmWarning line=3 buffer=5' ),
  540. call( 'try | exec "sign unplace 1 buffer=5" | catch /E158/ | endtry' )
  541. ] )