123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- # Copyright (C) 2016 YouCompleteMe contributors
- #
- # This file is part of YouCompleteMe.
- #
- # YouCompleteMe is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # YouCompleteMe is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
- from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
- MockVimModule, MockVimBuffers, VimBuffer )
- MockVimModule()
- import contextlib
- from hamcrest import ( assert_that,
- contains_exactly,
- empty,
- equal_to,
- has_entries )
- from unittest import TestCase
- from unittest.mock import call, MagicMock, patch
- from ycm.tests import PathToTestFile, YouCompleteMeInstance
- from ycmd.responses import ServerError
- import json
- @contextlib.contextmanager
- def MockCompletionRequest( response_method ):
- """Mock out the CompletionRequest, replacing the response handler
- JsonFromFuture with the |response_method| parameter."""
- # We don't want the requests to actually be sent to the server, just have it
- # return success.
- with patch( 'ycm.client.completer_available_request.'
- 'CompleterAvailableRequest.PostDataToHandler',
- return_value = True ):
- with patch( 'ycm.client.completion_request.CompletionRequest.'
- 'PostDataToHandlerAsync',
- return_value = MagicMock( return_value=True ) ):
- # We set up a fake response.
- with patch( 'ycm.client.base_request._JsonFromFuture',
- side_effect = response_method ):
- yield
- @contextlib.contextmanager
- def MockResolveRequest( response_method ):
- """Mock out the CompletionRequest, replacing the response handler
- JsonFromFuture with the |response_method| parameter."""
- with patch( 'ycm.client.resolve_completion_request.ResolveCompletionRequest.'
- 'PostDataToHandlerAsync',
- return_value = MagicMock( return_value=True ) ):
- # We set up a fake response.
- with patch( 'ycm.client.base_request._JsonFromFuture',
- side_effect = response_method ):
- yield
- class CompletionTest( TestCase ):
- @YouCompleteMeInstance()
- def test_SendCompletionRequest_UnicodeWorkingDirectory( self, ycm ):
- unicode_dir = PathToTestFile( 'uni¢od€' )
- current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
- def ServerResponse( *args ):
- return { 'completions': [], 'completion_start_column': 1 }
- with CurrentWorkingDirectory( unicode_dir ):
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( ServerResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- assert_that(
- ycm.GetCompletionResponse(),
- has_entries( {
- 'completions': empty(),
- 'completion_start_column': 1
- } )
- )
- @YouCompleteMeInstance()
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_SendCompletionRequest_ResponseContainingError(
- self, ycm, post_vim_message ):
- current_buffer = VimBuffer( 'buffer' )
- def ServerResponse( *args ):
- return {
- 'completions': [ {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string'
- }
- } ],
- 'completion_start_column': 3,
- 'errors': [ {
- 'exception': {
- 'TYPE': 'Exception'
- },
- 'message': 'message',
- 'traceback': 'traceback'
- } ]
- }
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( ServerResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_has_exact_calls( [
- call( 'Exception: message', truncate = True )
- ] )
- assert_that(
- response,
- has_entries( {
- 'completions': contains_exactly( has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } ) ),
- 'completion_start_column': 3
- } )
- )
- @YouCompleteMeInstance()
- @patch( 'ycm.client.base_request._logger', autospec = True )
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_SendCompletionRequest_ErrorFromServer( self,
- ycm,
- post_vim_message,
- logger ):
- current_buffer = VimBuffer( 'buffer' )
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( ServerError( 'Server error' ) ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- logger.exception.assert_called_with( 'Error while handling server '
- 'response' )
- post_vim_message.assert_has_exact_calls( [
- call( 'Server error', truncate = True )
- ] )
- assert_that(
- response,
- has_entries( {
- 'completions': empty(),
- 'completion_start_column': -1
- } )
- )
- @YouCompleteMeInstance()
- @patch( 'ycm.client.base_request._logger', autospec = True )
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_ResolveCompletionRequest_Resolves( self,
- ycm,
- post_vim_message,
- logger ):
- def CompletionResponse( *args ):
- return {
- 'completions': [ {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string',
- 'resolve': 10
- }
- } ],
- 'completion_start_column': 3,
- 'errors': []
- }
- def ResolveResponse( *args ):
- return {
- 'completion': {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string with more info'
- }
- },
- 'errors': []
- }
- current_buffer = VimBuffer( 'buffer' )
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( CompletionResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_not_called()
- assert_that(
- response,
- has_entries( {
- 'completions': contains_exactly( has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } ) ),
- 'completion_start_column': 3
- } )
- )
- item = response[ 'completions' ][ 0 ]
- assert_that( json.loads( item[ 'user_data' ] ),
- has_entries( { 'resolve': 10 } ) )
- with MockResolveRequest( ResolveResponse ):
- assert_that( ycm.ResolveCompletionItem( item ), equal_to( True ) )
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_not_called()
- assert_that(
- response,
- has_entries( {
- 'completion': has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string with more info',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } )
- } )
- )
- item = response[ 'completion' ]
- with MockResolveRequest( ServerError( 'must not be called' ) ):
- assert_that( ycm.ResolveCompletionItem( item ), equal_to( False ) )
- post_vim_message.assert_not_called()
- @YouCompleteMeInstance()
- @patch( 'ycm.client.base_request._logger', autospec = True )
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_ResolveCompletionRequest_ResponseContainsErrors( self,
- ycm,
- post_vim_message,
- logger ):
- def CompletionResponse( *args ):
- return {
- 'completions': [ {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string',
- 'resolve': 10
- }
- } ],
- 'completion_start_column': 3,
- 'errors': []
- }
- def ResolveResponse( *args ):
- return {
- 'completion': {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string with more info'
- }
- },
- 'errors': [ {
- 'exception': {
- 'TYPE': 'Exception'
- },
- 'message': 'message',
- 'traceback': 'traceback'
- } ]
- }
- current_buffer = VimBuffer( 'buffer' )
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( CompletionResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_not_called()
- assert_that(
- response,
- has_entries( {
- 'completions': contains_exactly( has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } ) ),
- 'completion_start_column': 3
- } )
- )
- item = response[ 'completions' ][ 0 ]
- assert_that( json.loads( item[ 'user_data' ] ),
- has_entries( { 'resolve': 10 } ) )
- with MockResolveRequest( ResolveResponse ):
- assert_that( ycm.ResolveCompletionItem( item ), equal_to( True ) )
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_has_exact_calls( [
- call( 'Exception: message', truncate = True )
- ] )
- assert_that(
- response,
- has_entries( {
- 'completion': has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string with more info',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } )
- } )
- )
- @YouCompleteMeInstance()
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_ResolveCompletionItem_NoUserData( self, ycm, post_vim_message ):
- def CompletionResponse( *args ):
- return {
- 'completions': [ {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind'
- } ],
- 'completion_start_column': 3,
- 'errors': []
- }
- current_buffer = VimBuffer( 'buffer' )
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( CompletionResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_not_called()
- assert_that(
- response,
- has_entries( {
- 'completions': contains_exactly( has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } ) ),
- 'completion_start_column': 3
- } )
- )
- item = response[ 'completions' ][ 0 ]
- item.pop( 'user_data' )
- with MockResolveRequest( ServerError( 'must not be called' ) ):
- assert_that( ycm.ResolveCompletionItem( item ), equal_to( False ) )
- post_vim_message.assert_not_called()
- @YouCompleteMeInstance()
- def test_ResolveCompletionItem_NoRequest( self, ycm ):
- assert_that( ycm.GetCurrentCompletionRequest(), equal_to( None ) )
- assert_that( ycm.ResolveCompletionItem( {} ), equal_to( False ) )
- @YouCompleteMeInstance()
- @patch( 'ycm.client.base_request._logger', autospec = True )
- @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
- def test_ResolveCompletionRequest_ServerError(
- self, ycm, post_vim_message, logger ):
- def ServerResponse( *args ):
- return {
- 'completions': [ {
- 'insertion_text': 'insertion_text',
- 'menu_text': 'menu_text',
- 'extra_menu_info': 'extra_menu_info',
- 'detailed_info': 'detailed_info',
- 'kind': 'kind',
- 'extra_data': {
- 'doc_string': 'doc_string',
- 'resolve': 10
- }
- } ],
- 'completion_start_column': 3,
- 'errors': []
- }
- current_buffer = VimBuffer( 'buffer' )
- with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
- with MockCompletionRequest( ServerResponse ):
- ycm.SendCompletionRequest()
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- post_vim_message.assert_not_called()
- assert_that(
- response,
- has_entries( {
- 'completions': contains_exactly( has_entries( {
- 'word': 'insertion_text',
- 'abbr': 'menu_text',
- 'menu': 'extra_menu_info',
- 'info': 'detailed_info\ndoc_string',
- 'kind': 'k',
- 'dup': 1,
- 'empty': 1
- } ) ),
- 'completion_start_column': 3
- } )
- )
- item = response[ 'completions' ][ 0 ]
- assert_that( json.loads( item[ 'user_data' ] ),
- has_entries( { 'resolve': 10 } ) )
- with MockResolveRequest( ServerError( 'Server error' ) ):
- ycm.ResolveCompletionItem( item )
- assert_that( ycm.CompletionRequestReady() )
- response = ycm.GetCompletionResponse()
- logger.exception.assert_called_with( 'Error while handling server '
- 'response' )
- post_vim_message.assert_has_exact_calls( [
- call( 'Server error', truncate = True )
- ] )
|