completion_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. from hamcrest import assert_that, contains, empty, has_entries
  29. from mock import call, patch
  30. from ycm.tests import PathToTestFile, YouCompleteMeInstance
  31. from ycmd.responses import ServerError
  32. @YouCompleteMeInstance()
  33. def CreateCompletionRequest_UnicodeWorkingDirectory_test( ycm ):
  34. unicode_dir = PathToTestFile( 'uni¢𐍈d€' )
  35. current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
  36. with CurrentWorkingDirectory( unicode_dir ):
  37. with MockVimBuffers( [ current_buffer ], current_buffer ):
  38. ycm.CreateCompletionRequest(),
  39. results = ycm.GetCompletions()
  40. assert_that(
  41. results,
  42. has_entries( {
  43. 'words': empty(),
  44. 'refresh': 'always'
  45. } )
  46. )
  47. @YouCompleteMeInstance()
  48. @patch( 'ycm.client.base_request._logger', autospec = True )
  49. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  50. def CreateCompletionRequest_ResponseContainingError_test( ycm,
  51. post_vim_message,
  52. logger ):
  53. current_buffer = VimBuffer( 'buffer' )
  54. with MockVimBuffers( [ current_buffer ], current_buffer ):
  55. ycm.CreateCompletionRequest(),
  56. response = {
  57. 'completions': [ {
  58. 'insertion_text': 'insertion_text',
  59. 'menu_text': 'menu_text',
  60. 'extra_menu_info': 'extra_menu_info',
  61. 'detailed_info': 'detailed_info',
  62. 'kind': 'kind',
  63. 'extra_data': {
  64. 'doc_string': 'doc_string'
  65. }
  66. } ],
  67. 'completion_start_column': 3,
  68. 'errors': [ {
  69. 'exception': {
  70. 'TYPE': 'Exception'
  71. },
  72. 'message': 'message',
  73. 'traceback': 'traceback'
  74. } ]
  75. }
  76. with patch( 'ycm.client.completion_request.JsonFromFuture',
  77. return_value = response ):
  78. results = ycm.GetCompletions()
  79. logger.exception.assert_called_with( 'Error while handling server response' )
  80. post_vim_message.assert_has_exact_calls( [
  81. call( 'Exception: message', truncate = True )
  82. ] )
  83. assert_that(
  84. results,
  85. has_entries( {
  86. 'words': contains( has_entries( {
  87. 'word': 'insertion_text',
  88. 'abbr': 'menu_text',
  89. 'menu': 'extra_menu_info',
  90. 'info': 'detailed_info\ndoc_string',
  91. 'kind': 'k',
  92. 'dup': 1,
  93. 'empty': 1
  94. } ) ),
  95. 'refresh': 'always'
  96. } )
  97. )
  98. @YouCompleteMeInstance()
  99. @patch( 'ycm.client.base_request._logger', autospec = True )
  100. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  101. def CreateCompletionRequest_ErrorFromServer_test( ycm,
  102. post_vim_message,
  103. logger ):
  104. current_buffer = VimBuffer( 'buffer' )
  105. with MockVimBuffers( [ current_buffer ], current_buffer ):
  106. ycm.CreateCompletionRequest(),
  107. with patch( 'ycm.client.completion_request.JsonFromFuture',
  108. side_effect = ServerError( 'Server error' ) ):
  109. results = ycm.GetCompletions()
  110. logger.exception.assert_called_with( 'Error while handling server response' )
  111. post_vim_message.assert_has_exact_calls( [
  112. call( 'Server error', truncate = True )
  113. ] )
  114. assert_that(
  115. results,
  116. has_entries( {
  117. 'words': empty(),
  118. 'refresh': 'always'
  119. } )
  120. )