completion_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # coding: utf-8
  2. #
  3. # Copyright (C) 2016 YouCompleteMe contributors
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from __future__ import unicode_literals
  20. from __future__ import print_function
  21. from __future__ import division
  22. from __future__ import absolute_import
  23. # Not installing aliases from python-future; it's unreliable and slow.
  24. from builtins import * # noqa
  25. from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
  26. MockVimModule, MockVimBuffers, VimBuffer )
  27. MockVimModule()
  28. import contextlib
  29. from hamcrest import assert_that, contains, empty, has_entries
  30. from mock import call, MagicMock, patch
  31. from nose.tools import ok_
  32. from ycm.tests import PathToTestFile, YouCompleteMeInstance
  33. from ycmd.responses import ServerError
  34. @contextlib.contextmanager
  35. def MockCompletionRequest( response_method ):
  36. """Mock out the CompletionRequest, replacing the response handler
  37. JsonFromFuture with the |response_method| parameter."""
  38. # We don't want the requests to actually be sent to the server, just have it
  39. # return success.
  40. with patch( 'ycm.client.completer_available_request.'
  41. 'CompleterAvailableRequest.PostDataToHandler',
  42. return_value = True ):
  43. with patch( 'ycm.client.completion_request.CompletionRequest.'
  44. 'PostDataToHandlerAsync',
  45. return_value = MagicMock( return_value=True ) ):
  46. # We set up a fake response.
  47. with patch( 'ycm.client.base_request._JsonFromFuture',
  48. side_effect = response_method ):
  49. yield
  50. @YouCompleteMeInstance()
  51. def SendCompletionRequest_UnicodeWorkingDirectory_test( ycm ):
  52. unicode_dir = PathToTestFile( 'uni¢𐍈d€' )
  53. current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
  54. def ServerResponse( *args ):
  55. return { 'completions': [], 'completion_start_column': 1 }
  56. with CurrentWorkingDirectory( unicode_dir ):
  57. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  58. with MockCompletionRequest( ServerResponse ):
  59. ycm.SendCompletionRequest()
  60. ok_( ycm.CompletionRequestReady() )
  61. assert_that(
  62. ycm.GetCompletionResponse(),
  63. has_entries( {
  64. 'completions': empty(),
  65. 'completion_start_column': 1
  66. } )
  67. )
  68. @YouCompleteMeInstance()
  69. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  70. def SendCompletionRequest_ResponseContainingError_test( ycm, post_vim_message ):
  71. current_buffer = VimBuffer( 'buffer' )
  72. def ServerResponse( *args ):
  73. return {
  74. 'completions': [ {
  75. 'insertion_text': 'insertion_text',
  76. 'menu_text': 'menu_text',
  77. 'extra_menu_info': 'extra_menu_info',
  78. 'detailed_info': 'detailed_info',
  79. 'kind': 'kind',
  80. 'extra_data': {
  81. 'doc_string': 'doc_string'
  82. }
  83. } ],
  84. 'completion_start_column': 3,
  85. 'errors': [ {
  86. 'exception': {
  87. 'TYPE': 'Exception'
  88. },
  89. 'message': 'message',
  90. 'traceback': 'traceback'
  91. } ]
  92. }
  93. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  94. with MockCompletionRequest( ServerResponse ):
  95. ycm.SendCompletionRequest()
  96. ok_( ycm.CompletionRequestReady() )
  97. response = ycm.GetCompletionResponse()
  98. post_vim_message.assert_has_exact_calls( [
  99. call( 'Exception: message', truncate = True )
  100. ] )
  101. assert_that(
  102. response,
  103. has_entries( {
  104. 'completions': contains( has_entries( {
  105. 'word': 'insertion_text',
  106. 'abbr': 'menu_text',
  107. 'menu': 'extra_menu_info',
  108. 'info': 'detailed_info\ndoc_string',
  109. 'kind': 'k',
  110. 'dup': 1,
  111. 'empty': 1
  112. } ) ),
  113. 'completion_start_column': 3
  114. } )
  115. )
  116. @YouCompleteMeInstance()
  117. @patch( 'ycm.client.base_request._logger', autospec = True )
  118. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  119. def SendCompletionRequest_ErrorFromServer_test( ycm,
  120. post_vim_message,
  121. logger ):
  122. current_buffer = VimBuffer( 'buffer' )
  123. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  124. with MockCompletionRequest( ServerError( 'Server error' ) ):
  125. ycm.SendCompletionRequest()
  126. ok_( ycm.CompletionRequestReady() )
  127. response = ycm.GetCompletionResponse()
  128. logger.exception.assert_called_with( 'Error while handling server '
  129. 'response' )
  130. post_vim_message.assert_has_exact_calls( [
  131. call( 'Server error', truncate = True )
  132. ] )
  133. assert_that(
  134. response,
  135. has_entries( {
  136. 'completions': empty(),
  137. 'completion_start_column': -1
  138. } )
  139. )