command_request_test.py 10 KB

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