completion_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
  18. MockVimModule, MockVimBuffers, VimBuffer )
  19. MockVimModule()
  20. import contextlib
  21. from hamcrest import assert_that, contains_exactly, empty, has_entries
  22. from unittest.mock import call, MagicMock, patch
  23. from ycm.tests import PathToTestFile, YouCompleteMeInstance
  24. from ycmd.responses import ServerError
  25. @contextlib.contextmanager
  26. def MockCompletionRequest( response_method ):
  27. """Mock out the CompletionRequest, replacing the response handler
  28. JsonFromFuture with the |response_method| parameter."""
  29. # We don't want the requests to actually be sent to the server, just have it
  30. # return success.
  31. with patch( 'ycm.client.completer_available_request.'
  32. 'CompleterAvailableRequest.PostDataToHandler',
  33. return_value = True ):
  34. with patch( 'ycm.client.completion_request.CompletionRequest.'
  35. 'PostDataToHandlerAsync',
  36. return_value = MagicMock( return_value=True ) ):
  37. # We set up a fake response.
  38. with patch( 'ycm.client.base_request._JsonFromFuture',
  39. side_effect = response_method ):
  40. yield
  41. @YouCompleteMeInstance()
  42. def SendCompletionRequest_UnicodeWorkingDirectory_test( ycm ):
  43. unicode_dir = PathToTestFile( 'uni¢𐍈d€' )
  44. current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
  45. def ServerResponse( *args ):
  46. return { 'completions': [], 'completion_start_column': 1 }
  47. with CurrentWorkingDirectory( unicode_dir ):
  48. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  49. with MockCompletionRequest( ServerResponse ):
  50. ycm.SendCompletionRequest()
  51. assert_that( ycm.CompletionRequestReady() )
  52. assert_that(
  53. ycm.GetCompletionResponse(),
  54. has_entries( {
  55. 'completions': empty(),
  56. 'completion_start_column': 1
  57. } )
  58. )
  59. @YouCompleteMeInstance()
  60. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  61. def SendCompletionRequest_ResponseContainingError_test( ycm, post_vim_message ):
  62. current_buffer = VimBuffer( 'buffer' )
  63. def ServerResponse( *args ):
  64. return {
  65. 'completions': [ {
  66. 'insertion_text': 'insertion_text',
  67. 'menu_text': 'menu_text',
  68. 'extra_menu_info': 'extra_menu_info',
  69. 'detailed_info': 'detailed_info',
  70. 'kind': 'kind',
  71. 'extra_data': {
  72. 'doc_string': 'doc_string'
  73. }
  74. } ],
  75. 'completion_start_column': 3,
  76. 'errors': [ {
  77. 'exception': {
  78. 'TYPE': 'Exception'
  79. },
  80. 'message': 'message',
  81. 'traceback': 'traceback'
  82. } ]
  83. }
  84. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  85. with MockCompletionRequest( ServerResponse ):
  86. ycm.SendCompletionRequest()
  87. assert_that( ycm.CompletionRequestReady() )
  88. response = ycm.GetCompletionResponse()
  89. post_vim_message.assert_has_exact_calls( [
  90. call( 'Exception: message', truncate = True )
  91. ] )
  92. assert_that(
  93. response,
  94. has_entries( {
  95. 'completions': contains_exactly( has_entries( {
  96. 'word': 'insertion_text',
  97. 'abbr': 'menu_text',
  98. 'menu': 'extra_menu_info',
  99. 'info': 'detailed_info\ndoc_string',
  100. 'kind': 'k',
  101. 'dup': 1,
  102. 'empty': 1
  103. } ) ),
  104. 'completion_start_column': 3
  105. } )
  106. )
  107. @YouCompleteMeInstance()
  108. @patch( 'ycm.client.base_request._logger', autospec = True )
  109. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  110. def SendCompletionRequest_ErrorFromServer_test( ycm,
  111. post_vim_message,
  112. logger ):
  113. current_buffer = VimBuffer( 'buffer' )
  114. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  115. with MockCompletionRequest( ServerError( 'Server error' ) ):
  116. ycm.SendCompletionRequest()
  117. assert_that( ycm.CompletionRequestReady() )
  118. response = ycm.GetCompletionResponse()
  119. logger.exception.assert_called_with( 'Error while handling server '
  120. 'response' )
  121. post_vim_message.assert_has_exact_calls( [
  122. call( 'Server error', truncate = True )
  123. ] )
  124. assert_that(
  125. response,
  126. has_entries( {
  127. 'completions': empty(),
  128. 'completion_start_column': -1
  129. } )
  130. )