command_request_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. import pytest
  21. from hamcrest import assert_that
  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_QuickFix_test:
  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_method( self ):
  83. self._request = CommandRequest( [ 'GoToTest' ] )
  84. def teardown_method( self ):
  85. self._request = None
  86. def GoTo_EmptyList_test( self ):
  87. self._CheckGoToList( [], [] )
  88. def GoTo_SingleItem_List_test( 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 GoTo_MultiItem_List_test( 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:
  150. @pytest.mark.parametrize( 'command,response', [
  151. [ 'AnythingYouLike', True ],
  152. [ 'GoToEvenWorks', 10 ],
  153. [ 'FixItWorks', 'String!' ],
  154. [ 'and8434fd andy garbag!', 10.3 ],
  155. ] )
  156. def BasicResponse_test( self, command, response ):
  157. def _BasicResponseTest( command, response ):
  158. with patch( 'vim.command' ) as vim_command:
  159. request = CommandRequest( [ command ] )
  160. request._response = response
  161. request.RunPostCommandActionsIfNeeded( 'belowright' )
  162. vim_command.assert_called_with( f"echo '{ response }'" )
  163. _BasicResponseTest( command, response )
  164. @pytest.mark.parametrize( 'command', [ 'FixIt',
  165. 'Refactor',
  166. 'GoToHell',
  167. 'any_old_garbade!!!21' ] )
  168. def FixIt_Response_Empty_test( self, command ):
  169. # Ensures we recognise and handle fixit responses which indicate that there
  170. # are no fixits available
  171. def EmptyFixItTest( command ):
  172. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  173. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  174. request = CommandRequest( [ command ] )
  175. request._response = {
  176. 'fixits': []
  177. }
  178. request.RunPostCommandActionsIfNeeded( 'botright' )
  179. post_vim_message.assert_called_with(
  180. 'No fixits found for current line', warning = False )
  181. replace_chunks.assert_not_called()
  182. EmptyFixItTest( command )
  183. @pytest.mark.parametrize( 'command,response,chunks,selection,silent', [
  184. [ 'AnythingYouLike',
  185. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  186. [ 'GoToEvenWorks',
  187. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  188. [ 'FixItWorks',
  189. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  190. [ 'and8434fd andy garbag!',
  191. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, False ],
  192. [ 'Format',
  193. BASIC_FIXIT, BASIC_FIXIT_CHUNKS, 0, True ],
  194. [ 'select from multiple 1',
  195. MULTI_FIXIT, MULTI_FIXIT_FIRST_CHUNKS, 0, False ],
  196. [ 'select from multiple 2',
  197. MULTI_FIXIT, MULTI_FIXIT_SECOND_CHUNKS, 1, False ],
  198. ] )
  199. def FixIt_Response_test( self, command, response, chunks, selection, silent ):
  200. # Ensures we recognise and handle fixit responses with some dummy chunk data
  201. def FixItTest( command, response, chunks, selection, silent ):
  202. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  203. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  204. with patch( 'ycm.vimsupport.SelectFromList',
  205. return_value = selection ):
  206. request = CommandRequest( [ command ] )
  207. request._response = response
  208. request.RunPostCommandActionsIfNeeded( 'leftabove' )
  209. replace_chunks.assert_called_with( chunks, silent = silent )
  210. post_vim_message.assert_not_called()
  211. FixItTest( command, response, chunks, selection, silent )
  212. @pytest.mark.parametrize( 'command,message', [
  213. [ '___________', 'This is a message' ],
  214. [ '', 'this is also a message' ],
  215. [ 'GetType', 'std::string' ],
  216. ] )
  217. def Message_Response_test( self, command, message ):
  218. # Ensures we correctly recognise and handle responses with a message to show
  219. # to the user
  220. def MessageTest( command, message ):
  221. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  222. request = CommandRequest( [ command ] )
  223. request._response = { 'message': message }
  224. request.RunPostCommandActionsIfNeeded( 'rightbelow' )
  225. post_vim_message.assert_called_with( message, warning = False )
  226. MessageTest( command, message )
  227. @pytest.mark.parametrize( 'command,info', [
  228. [ '___________', 'This is a message' ],
  229. [ '', 'this is also a message' ],
  230. [ 'GetDoc', 'std::string\netc\netc' ],
  231. ] )
  232. def Detailed_Info_test( self, command, info ):
  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 )
  241. DetailedInfoTest( command, info )
  242. @pytest.mark.parametrize( 'test,command,response', [
  243. [ GoToTest, 'AnythingYouLike', BASIC_GOTO ],
  244. [ GoToTest, 'GoTo', BASIC_GOTO ],
  245. [ GoToTest, 'FindAThing', BASIC_GOTO ],
  246. [ GoToTest, 'FixItGoto', BASIC_GOTO ],
  247. [ GoToListTest, 'AnythingYouLike', [ BASIC_GOTO ] ],
  248. [ GoToListTest, 'GoTo', [] ],
  249. [ GoToListTest, 'FixItGoto', [ BASIC_GOTO, BASIC_GOTO ] ],
  250. ] )
  251. def GoTo_Single_test( self, test, command, response ):
  252. test( command, response )