command_request_test.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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()
  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( 'au WinLeave <buffer> q' ),
  93. call( 'doautocmd User YcmQuickFixOpened' )
  94. ] )
  95. set_fitting_height.assert_called_once_with()
  96. class Response_Detection_test( object ):
  97. def BasicResponse_test( self ):
  98. def _BasicResponseTest( command, response ):
  99. with patch( 'vim.command' ) as vim_command:
  100. request = CommandRequest( [ command ] )
  101. request._response = response
  102. request.RunPostCommandActionsIfNeeded()
  103. vim_command.assert_called_with( "echo '{0}'".format( response ) )
  104. tests = [
  105. [ 'AnythingYouLike', True ],
  106. [ 'GoToEvenWorks', 10 ],
  107. [ 'FixItWorks', 'String!' ],
  108. [ 'and8434fd andy garbag!', 10.3 ],
  109. ]
  110. for test in tests:
  111. yield _BasicResponseTest, test[ 0 ], test[ 1 ]
  112. def FixIt_Response_Empty_test( self ):
  113. # Ensures we recognise and handle fixit responses which indicate that there
  114. # are no fixits available
  115. def EmptyFixItTest( command ):
  116. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  117. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  118. request = CommandRequest( [ command ] )
  119. request._response = {
  120. 'fixits': []
  121. }
  122. request.RunPostCommandActionsIfNeeded()
  123. post_vim_message.assert_called_with(
  124. 'No fixits found for current line', warning = False )
  125. replace_chunks.assert_not_called()
  126. for test in [ 'FixIt', 'Refactor', 'GoToHell', 'any_old_garbade!!!21' ]:
  127. yield EmptyFixItTest, test
  128. def FixIt_Response_test( self ):
  129. # Ensures we recognise and handle fixit responses with some dummy chunk data
  130. def FixItTest( command, response, chunks, selection ):
  131. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  132. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  133. with patch( 'ycm.vimsupport.SelectFromList',
  134. return_value = selection ):
  135. request = CommandRequest( [ command ] )
  136. request._response = response
  137. request.RunPostCommandActionsIfNeeded()
  138. replace_chunks.assert_called_with( chunks )
  139. post_vim_message.assert_not_called()
  140. basic_fixit = {
  141. 'fixits': [ {
  142. 'chunks': [ {
  143. 'dummy chunk contents': True
  144. } ]
  145. } ]
  146. }
  147. basic_fixit_chunks = basic_fixit[ 'fixits' ][ 0 ][ 'chunks' ]
  148. multi_fixit = {
  149. 'fixits': [ {
  150. 'text': 'first',
  151. 'chunks': [ {
  152. 'dummy chunk contents': True
  153. } ]
  154. }, {
  155. 'text': 'second',
  156. 'chunks': [ {
  157. 'dummy chunk contents': False
  158. }]
  159. } ]
  160. }
  161. multi_fixit_first_chunks = multi_fixit[ 'fixits' ][ 0 ][ 'chunks' ]
  162. multi_fixit_second_chunks = multi_fixit[ 'fixits' ][ 1 ][ 'chunks' ]
  163. tests = [
  164. [ 'AnythingYouLike', basic_fixit, basic_fixit_chunks, 0 ],
  165. [ 'GoToEvenWorks', basic_fixit, basic_fixit_chunks, 0 ],
  166. [ 'FixItWorks', basic_fixit, basic_fixit_chunks, 0 ],
  167. [ 'and8434fd andy garbag!', basic_fixit, basic_fixit_chunks, 0 ],
  168. [ 'select from multiple 1', multi_fixit, multi_fixit_first_chunks, 0 ],
  169. [ 'select from multiple 2', multi_fixit, multi_fixit_second_chunks, 1 ],
  170. ]
  171. for test in tests:
  172. yield FixItTest, test[ 0 ], test[ 1 ], test[ 2 ], test[ 3 ]
  173. def Message_Response_test( self ):
  174. # Ensures we correctly recognise and handle responses with a message to show
  175. # to the user
  176. def MessageTest( command, message ):
  177. with patch( 'ycm.vimsupport.PostVimMessage' ) as post_vim_message:
  178. request = CommandRequest( [ command ] )
  179. request._response = { 'message': message }
  180. request.RunPostCommandActionsIfNeeded()
  181. post_vim_message.assert_called_with( message, warning = False )
  182. tests = [
  183. [ '___________', 'This is a message' ],
  184. [ '', 'this is also a message' ],
  185. [ 'GetType', 'std::string' ],
  186. ]
  187. for test in tests:
  188. yield MessageTest, test[ 0 ], test[ 1 ]
  189. def Detailed_Info_test( self ):
  190. # Ensures we correctly detect and handle detailed_info responses which are
  191. # used to display information in the preview window
  192. def DetailedInfoTest( command, info ):
  193. with patch( 'ycm.vimsupport.WriteToPreviewWindow' ) as write_to_preview:
  194. request = CommandRequest( [ command ] )
  195. request._response = { 'detailed_info': info }
  196. request.RunPostCommandActionsIfNeeded()
  197. write_to_preview.assert_called_with( info )
  198. tests = [
  199. [ '___________', 'This is a message' ],
  200. [ '', 'this is also a message' ],
  201. [ 'GetDoc', 'std::string\netc\netc' ],
  202. ]
  203. for test in tests:
  204. yield DetailedInfoTest, test[ 0 ], test[ 1 ]
  205. def GoTo_Single_test( self ):
  206. # Ensures we handle any unknown type of response as a GoTo response
  207. def GoToTest( command, response ):
  208. with patch( 'ycm.vimsupport.JumpToLocation' ) as jump_to_location:
  209. request = CommandRequest( [ command ] )
  210. request._response = response
  211. request.RunPostCommandActionsIfNeeded()
  212. jump_to_location.assert_called_with(
  213. response[ 'filepath' ],
  214. response[ 'line_num' ],
  215. response[ 'column_num' ] )
  216. def GoToListTest( command, response ):
  217. # Note: the detail of these called are tested by
  218. # GoToResponse_QuickFix_test, so here we just check that the right call is
  219. # made
  220. with patch( 'ycm.vimsupport.SetQuickFixList' ) as set_qf_list:
  221. with patch( 'ycm.vimsupport.OpenQuickFixList' ) as open_qf_list:
  222. request = CommandRequest( [ command ] )
  223. request._response = response
  224. request.RunPostCommandActionsIfNeeded()
  225. ok_( set_qf_list.called )
  226. ok_( open_qf_list.called )
  227. basic_goto = {
  228. 'filepath': 'test',
  229. 'line_num': 10,
  230. 'column_num': 100,
  231. }
  232. tests = [
  233. [ GoToTest, 'AnythingYouLike', basic_goto ],
  234. [ GoToTest, 'GoTo', basic_goto ],
  235. [ GoToTest, 'FindAThing', basic_goto ],
  236. [ GoToTest, 'FixItGoto', basic_goto ],
  237. [ GoToListTest, 'AnythingYouLike', [ basic_goto ] ],
  238. [ GoToListTest, 'GoTo', [] ],
  239. [ GoToListTest, 'FixItGoto', [ basic_goto, basic_goto ] ],
  240. ]
  241. for test in tests:
  242. yield test[ 0 ], test[ 1 ], test[ 2 ]