command_request_test.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.test_utils import 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( 'vim.eval' )
  76. def _CheckGoToList( self, completer_response, expected_qf_list, vim_eval ):
  77. self._request._response = completer_response
  78. self._request.RunPostCommandActionsIfNeeded()
  79. vim_eval.assert_has_calls( [
  80. call( 'setqflist( {0} )'.format( json.dumps( expected_qf_list ) ) ),
  81. call( 'youcompleteme#OpenGoToList()' ),
  82. ] )
  83. class Response_Detection_test( object ):
  84. def BasicResponse_test( self ):
  85. def _BasicResponseTest( command, response ):
  86. with patch( 'vim.command' ) as vim_command:
  87. request = CommandRequest( [ command ] )
  88. request._response = response
  89. request.RunPostCommandActionsIfNeeded()
  90. vim_command.assert_called_with( "echom '{0}'".format( response ) )
  91. tests = [
  92. [ 'AnythingYouLike', True ],
  93. [ 'GoToEvenWorks', 10 ],
  94. [ 'FixItWorks', 'String!' ],
  95. [ 'and8434fd andy garbag!', 10.3 ],
  96. ]
  97. for test in tests:
  98. yield _BasicResponseTest, test[ 0 ], test[ 1 ]
  99. def FixIt_Response_Empty_test( self ):
  100. # Ensures we recognise and handle fixit responses which indicate that there
  101. # are no fixits available
  102. def EmptyFixItTest( command ):
  103. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  104. with patch( 'ycm.vimsupport.EchoText' ) as echo_text:
  105. request = CommandRequest( [ command ] )
  106. request._response = {
  107. 'fixits': []
  108. }
  109. request.RunPostCommandActionsIfNeeded()
  110. echo_text.assert_called_with( 'No fixits found for current line' )
  111. replace_chunks.assert_not_called()
  112. for test in [ 'FixIt', 'Refactor', 'GoToHell', 'any_old_garbade!!!21' ]:
  113. yield EmptyFixItTest, test
  114. def FixIt_Response_test( self ):
  115. # Ensures we recognise and handle fixit responses with some dummy chunk data
  116. def FixItTest( command, response, chunks ):
  117. with patch( 'ycm.vimsupport.ReplaceChunks' ) as replace_chunks:
  118. with patch( 'ycm.vimsupport.EchoText' ) as echo_text:
  119. request = CommandRequest( [ command ] )
  120. request._response = response
  121. request.RunPostCommandActionsIfNeeded()
  122. replace_chunks.assert_called_with( chunks )
  123. echo_text.assert_not_called()
  124. basic_fixit = {
  125. 'fixits': [ {
  126. 'chunks': [ {
  127. 'dummy chunk contents': True
  128. } ]
  129. } ]
  130. }
  131. basic_fixit_chunks = basic_fixit[ 'fixits' ][ 0 ][ 'chunks' ]
  132. multi_fixit = {
  133. 'fixits': [ {
  134. 'chunks': [ {
  135. 'dummy chunk contents': True
  136. } ]
  137. }, {
  138. 'additional fixits are ignored currently': True
  139. } ]
  140. }
  141. multi_fixit_first_chunks = multi_fixit[ 'fixits' ][ 0 ][ 'chunks' ]
  142. tests = [
  143. [ 'AnythingYouLike', basic_fixit, basic_fixit_chunks ],
  144. [ 'GoToEvenWorks', basic_fixit, basic_fixit_chunks ],
  145. [ 'FixItWorks', basic_fixit, basic_fixit_chunks ],
  146. [ 'and8434fd andy garbag!', basic_fixit, basic_fixit_chunks ],
  147. [ 'additional fixits ignored', multi_fixit, multi_fixit_first_chunks ],
  148. ]
  149. for test in tests:
  150. yield FixItTest, test[ 0 ], test[ 1 ], test[ 2 ]
  151. def Message_Response_test( self ):
  152. # Ensures we correctly recognise and handle responses with a message to show
  153. # to the user
  154. def MessageTest( command, message ):
  155. with patch( 'ycm.vimsupport.EchoText' ) as echo_text:
  156. request = CommandRequest( [ command ] )
  157. request._response = { 'message': message }
  158. request.RunPostCommandActionsIfNeeded()
  159. echo_text.assert_called_with( message )
  160. tests = [
  161. [ '___________', 'This is a message' ],
  162. [ '', 'this is also a message' ],
  163. [ 'GetType', 'std::string' ],
  164. ]
  165. for test in tests:
  166. yield MessageTest, test[ 0 ], test[ 1 ]
  167. def Detailed_Info_test( self ):
  168. # Ensures we correctly detect and handle detailed_info responses which are
  169. # used to display information in the preview window
  170. def DetailedInfoTest( command, info ):
  171. with patch( 'ycm.vimsupport.WriteToPreviewWindow' ) as write_to_preview:
  172. request = CommandRequest( [ command ] )
  173. request._response = { 'detailed_info': info }
  174. request.RunPostCommandActionsIfNeeded()
  175. write_to_preview.assert_called_with( info )
  176. tests = [
  177. [ '___________', 'This is a message' ],
  178. [ '', 'this is also a message' ],
  179. [ 'GetDoc', 'std::string\netc\netc' ],
  180. ]
  181. for test in tests:
  182. yield DetailedInfoTest, test[ 0 ], test[ 1 ]
  183. def GoTo_Single_test( self ):
  184. # Ensures we handle any unknown type of response as a GoTo response
  185. def GoToTest( command, response ):
  186. with patch( 'ycm.vimsupport.JumpToLocation' ) as jump_to_location:
  187. request = CommandRequest( [ command ] )
  188. request._response = response
  189. request.RunPostCommandActionsIfNeeded()
  190. jump_to_location.assert_called_with(
  191. response[ 'filepath' ],
  192. response[ 'line_num' ],
  193. response[ 'column_num' ] )
  194. def GoToListTest( command, response ):
  195. # Note: the detail of these called are tested by
  196. # GoToResponse_QuickFix_test, so here we just check that the right call is
  197. # made
  198. with patch( 'ycm.vimsupport.SetQuickFixList' ) as set_qf_list:
  199. with patch( 'vim.eval' ) as vim_eval:
  200. request = CommandRequest( [ command ] )
  201. request._response = response
  202. request.RunPostCommandActionsIfNeeded()
  203. ok_( set_qf_list.called )
  204. ok_( vim_eval.called )
  205. basic_goto = {
  206. 'filepath': 'test',
  207. 'line_num': 10,
  208. 'column_num': 100,
  209. }
  210. tests = [
  211. [ GoToTest, 'AnythingYouLike', basic_goto ],
  212. [ GoToTest, 'GoTo', basic_goto ],
  213. [ GoToTest, 'FindAThing', basic_goto ],
  214. [ GoToTest, 'FixItGoto', basic_goto ],
  215. [ GoToListTest, 'AnythingYouLike', [ basic_goto ] ],
  216. [ GoToListTest, 'GoTo', [] ],
  217. [ GoToListTest, 'FixItGoto', [ basic_goto, basic_goto ] ],
  218. ]
  219. for test in tests:
  220. yield test[ 0 ], test[ 1 ], test[ 2 ]