command_request_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # Copyright (C) 2016 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.tests.test_utils import ExtendedMock, MockVimModule
  18. MockVimModule()
  19. import json
  20. from hamcrest import assert_that
  21. from unittest import TestCase
  22. from unittest.mock import patch, call
  23. from ycm.client.command_request import CommandRequest
  24. def GoToTest( command, response ):
  25. with patch( 'ycm.vimsupport.JumpToLocation' ) as jump_to_location:
  26. request = CommandRequest( [ command ] )
  27. request._response = response
  28. request.RunPostCommandActionsIfNeeded( 'rightbelow' )
  29. jump_to_location.assert_called_with(
  30. response[ 'filepath' ],
  31. response[ 'line_num' ],
  32. response[ 'column_num' ],
  33. 'rightbelow',
  34. 'same-buffer' )
  35. def GoToListTest( command, response ):
  36. # Note: the detail of these called are tested by
  37. # GoToResponse_QuickFix_test, so here we just check that the right call is
  38. # made
  39. with patch( 'ycm.vimsupport.SetQuickFixList' ) as set_qf_list:
  40. with patch( 'ycm.vimsupport.OpenQuickFixList' ) as open_qf_list:
  41. request = CommandRequest( [ command ] )
  42. request._response = response
  43. request.RunPostCommandActionsIfNeeded( 'tab' )
  44. assert_that( set_qf_list.called )
  45. assert_that( open_qf_list.called )
  46. BASIC_GOTO = {
  47. 'filepath': 'test',
  48. 'line_num': 10,
  49. 'column_num': 100,
  50. }
  51. BASIC_FIXIT = {
  52. 'fixits': [ {
  53. 'resolve': False,
  54. 'chunks': [ {
  55. 'dummy chunk contents': True
  56. } ]
  57. } ]
  58. }
  59. BASIC_FIXIT_CHUNKS = BASIC_FIXIT[ 'fixits' ][ 0 ][ 'chunks' ]
  60. MULTI_FIXIT = {
  61. 'fixits': [ {
  62. 'text': 'first',
  63. 'resolve': False,
  64. 'chunks': [ {
  65. 'dummy chunk contents': True
  66. } ]
  67. }, {
  68. 'text': 'second',
  69. 'resolve': False,
  70. 'chunks': [ {
  71. 'dummy chunk contents': False
  72. } ]
  73. } ]
  74. }
  75. MULTI_FIXIT_FIRST_CHUNKS = MULTI_FIXIT[ 'fixits' ][ 0 ][ 'chunks' ]
  76. MULTI_FIXIT_SECOND_CHUNKS = MULTI_FIXIT[ 'fixits' ][ 1 ][ 'chunks' ]
  77. class GoToResponse_QuickFixTest( TestCase ):
  78. """This class tests the generation of QuickFix lists for GoTo responses which
  79. return multiple locations, such as the Python completer and JavaScript
  80. completer. It mostly proves that we use 1-based indexing for the column
  81. number."""
  82. def setUp( self ):
  83. self._request = CommandRequest( [ 'GoToTest' ] )
  84. def tearDown( self ):
  85. self._request = None
  86. def test_GoTo_EmptyList( self ):
  87. self._CheckGoToList( [], [] )
  88. def test_GoTo_SingleItem_List( self ):
  89. self._CheckGoToList( [ {
  90. 'filepath': 'dummy_file',
  91. 'line_num': 10,
  92. 'column_num': 1,
  93. 'description': 'this is some text',
  94. } ], [ {
  95. 'filename': 'dummy_file',
  96. 'text': 'this is some text',
  97. 'lnum': 10,
  98. 'col': 1
  99. } ] )
  100. def test_GoTo_MultiItem_List( self ):
  101. self._CheckGoToList( [ {
  102. 'filepath': 'dummy_file',
  103. 'line_num': 10,
  104. 'column_num': 1,
  105. 'description': 'this is some other text',
  106. }, {
  107. 'filepath': 'dummy_file2',
  108. 'line_num': 1,
  109. 'column_num': 21,
  110. 'description': 'this is some text',
  111. } ], [ {
  112. 'filename': 'dummy_file',
  113. 'text': 'this is some other text',
  114. 'lnum': 10,
  115. 'col': 1
  116. }, {
  117. 'filename': 'dummy_file2',
  118. 'text': 'this is some text',
  119. 'lnum': 1,
  120. 'col': 21
  121. } ] )
  122. @patch( 'ycm.vimsupport.VariableExists', return_value = True )
  123. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  124. @patch( 'vim.command', new_callable = ExtendedMock )
  125. @patch( 'vim.eval', new_callable = ExtendedMock )
  126. def _CheckGoToList( self,
  127. completer_response,
  128. expected_qf_list,
  129. vim_eval,
  130. vim_command,
  131. set_fitting_height,
  132. variable_exists ):
  133. self._request._response = completer_response
  134. self._request.RunPostCommandActionsIfNeeded( 'aboveleft' )
  135. vim_eval.assert_has_exact_calls( [
  136. call( f'setqflist( { json.dumps( expected_qf_list ) } )' )
  137. ] )
  138. vim_command.assert_has_exact_calls( [
  139. call( 'botright copen' ),
  140. call( 'augroup ycmquickfix' ),
  141. call( 'autocmd! * <buffer>' ),
  142. call( 'autocmd WinLeave <buffer> '
  143. 'if bufnr( "%" ) == expand( "<abuf>" ) | q | endif '
  144. '| autocmd! ycmquickfix' ),
  145. call( 'augroup END' ),
  146. call( 'doautocmd User YcmQuickFixOpened' )
  147. ] )
  148. set_fitting_height.assert_called_once_with()
  149. class Response_Detection_Test( TestCase ):
  150. def test_BasicResponse( self ):
  151. def _BasicResponseTest( command, response ):
  152. with patch( 'vim.command' ) as vim_command:
  153. request = CommandRequest( [ command ] )
  154. request._response = response
  155. request.RunPostCommandActionsIfNeeded( 'belowright' )
  156. vim_command.assert_called_with( f"echo '{ response }'" )
  157. for command, response in [
  158. [ 'AnythingYouLike', True ],
  159. [ 'GoToEvenWorks', 10 ],
  160. [ 'FixItWorks', 'String!' ],
  161. [ 'and8434fd andy garbag!', 10.3 ],
  162. ]:
  163. with self.subTest( command = command, response = response ):
  164. _BasicResponseTest( command, response )
  165. def test_FixIt_Response_Empty( self ):
  166. # Ensures we recognise and handle fixit responses which indicate that there
  167. # are no fixits available
  168. def EmptyFixItTest( command ):
  169. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  170. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  171. request = CommandRequest( [ command ] )
  172. request._response = {
  173. 'fixits': []
  174. }
  175. request.RunPostCommandActionsIfNeeded( 'botright' )
  176. post_vim_message.assert_called_with(
  177. 'No fixits found for current line', warning = False )
  178. replace_chunks.assert_not_called()
  179. for command in [ 'FixIt', 'Refactor', 'GoToHell', 'any_old_garbade!!!21' ]:
  180. with self.subTest( command = command ):
  181. EmptyFixItTest( command )
  182. def test_FixIt_Response( self ):
  183. # Ensures we recognise and handle fixit responses with some dummy chunk data
  184. def FixItTest( command, response, chunks, selection, silent ):
  185. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  186. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  187. with patch( 'ycm.vimsupport.SelectFromList',
  188. return_value = selection ):
  189. request = CommandRequest( [ command ] )
  190. request._response = response
  191. request.RunPostCommandActionsIfNeeded( 'leftabove' )
  192. replace_chunks.assert_called_with( chunks, silent = silent )
  193. post_vim_message.assert_not_called()
  194. for command, response, chunks, selection, silent in [
  195. [ 'AnythingYouLike',
  196. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  197. [ 'GoToEvenWorks',
  198. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  199. [ 'FixItWorks',
  200. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  201. [ 'and8434fd andy garbag!',
  202. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  203. [ 'Format',
  204. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, True ],
  205. [ 'select from multiple 1',
  206. MULTI_FIXIT, MULTI_FIXIT_FIRST_CHUNKS, 0, False ],
  207. [ 'select from multiple 2',
  208. MULTI_FIXIT, MULTI_FIXIT_SECOND_CHUNKS, 1, False ],
  209. ]:
  210. with self.subTest( command = command,
  211. response = response,
  212. chunks = chunks,
  213. selection = selection,
  214. silent = silent ):
  215. FixItTest( command, response, chunks, selection, silent )
  216. def test_Message_Response( self ):
  217. # Ensures we correctly recognise and handle responses with a message to show
  218. # to the user
  219. def MessageTest( command, message ):
  220. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  221. request = CommandRequest( [ command ] )
  222. request._response = { 'message': message }
  223. request.RunPostCommandActionsIfNeeded( 'rightbelow' )
  224. post_vim_message.assert_called_with( message, warning = False )
  225. for command, message in [
  226. [ '___________', 'This is a message' ],
  227. [ '', 'this is also a message' ],
  228. [ 'GetType', 'std::string' ],
  229. ]:
  230. with self.subTest( command = command, message = message ):
  231. MessageTest( command, message )
  232. def test_Detailed_Info( self ):
  233. # Ensures we correctly detect and handle detailed_info responses which are
  234. # used to display information in the preview window
  235. def DetailedInfoTest( command, info ):
  236. with patch( 'ycm.vimsupport.WriteToPreviewWindow' ) as write_to_preview:
  237. request = CommandRequest( [ command ] )
  238. request._response = { 'detailed_info': info }
  239. request.RunPostCommandActionsIfNeeded( 'topleft' )
  240. write_to_preview.assert_called_with( info, 'topleft' )
  241. for command, info in [
  242. [ '___________', 'This is a message' ],
  243. [ '', 'this is also a message' ],
  244. [ 'GetDoc', 'std::string\netc\netc' ],
  245. ]:
  246. with self.subTest( command = command, info = info ):
  247. DetailedInfoTest( command, info )
  248. def test_GoTo_Single( self ):
  249. for test, command, response in [
  250. [ GoToTest, 'AnythingYouLike', BASIC_GOTO ],
  251. [ GoToTest, 'GoTo', BASIC_GOTO ],
  252. [ GoToTest, 'FindAThing', BASIC_GOTO ],
  253. [ GoToTest, 'FixItGoto', BASIC_GOTO ],
  254. [ GoToListTest, 'AnythingYouLike', [ BASIC_GOTO ] ],
  255. [ GoToListTest, 'GoTo', [] ],
  256. [ GoToListTest, 'FixItGoto', [ BASIC_GOTO, BASIC_GOTO ] ],
  257. ]:
  258. with self.subTest( test = test, command = command, response = response ):
  259. test( command, response )