command_request_test.py 8.4 KB

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