1
0

command_request_test.py 9.6 KB

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