diff --git a/python/ycm/tests/__init__.py b/python/ycm/tests/__init__.py index e69de29b..50a099de 100644 --- a/python/ycm/tests/__init__.py +++ b/python/ycm/tests/__init__.py @@ -0,0 +1,105 @@ +# 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 . + +from __future__ import unicode_literals +from __future__ import print_function +from __future__ import division +from __future__ import absolute_import +from future import standard_library +standard_library.install_aliases() +from builtins import * # noqa + +from ycm.test_utils import MockVimModule +MockVimModule() + +import functools +import requests +import time + +from ycm.client.base_request import BaseRequest +from ycm.youcompleteme import YouCompleteMe +from ycmd import user_options_store +from ycmd.utils import WaitUntilProcessIsTerminated + +# The default options which are only relevant to the client, not the server and +# thus are not part of default_options.json, but are required for a working +# YouCompleteMe object. +DEFAULT_CLIENT_OPTIONS = { + 'server_log_level': 'info', + 'extra_conf_vim_data': [], + 'show_diagnostics_ui': 1, + 'enable_diagnostic_signs': 1, + 'enable_diagnostic_highlighting': 0, + 'always_populate_location_list': 0, +} + + +def _MakeUserOptions( custom_options = {} ): + options = dict( user_options_store.DefaultOptions() ) + options.update( DEFAULT_CLIENT_OPTIONS ) + options.update( custom_options ) + return options + + +def _IsReady(): + return BaseRequest.GetDataFromHandler( 'ready' ) + + +def _WaitUntilReady( timeout = 5 ): + expiration = time.time() + timeout + while True: + try: + if time.time() > expiration: + raise RuntimeError( 'Waited for the server to be ready ' + 'for {0} seconds, aborting.'.format( timeout ) ) + if _IsReady(): + return + except requests.exceptions.ConnectionError: + pass + finally: + time.sleep( 0.1 ) + + +def YouCompleteMeInstance( custom_options = {} ): + """Defines a decorator function for tests that passes a unique YouCompleteMe + instance as a parameter. This instance is initialized with the default options + `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give + additional options and/or override the already existing ones. + + Do NOT attach it to test generators but directly to the yielded tests. + + Example usage: + + from ycm.tests import YouCompleteMeInstance + + @YouCompleteMeInstance( { 'server_log_level': 'debug', + 'server_keep_logfiles': 1 } ) + def Debug_test( ycm ): + ... + """ + def Decorator( test ): + @functools.wraps( test ) + def Wrapper( *args, **kwargs ): + ycm = YouCompleteMe( _MakeUserOptions( custom_options ) ) + _WaitUntilReady() + try: + test( ycm, *args, **kwargs ) + finally: + ycm.OnVimLeave() + WaitUntilProcessIsTerminated( ycm._server_popen ) + return Wrapper + return Decorator diff --git a/python/ycm/tests/event_notification_test.py b/python/ycm/tests/event_notification_test.py index 863631d2..cff9bfb4 100644 --- a/python/ycm/tests/event_notification_test.py +++ b/python/ycm/tests/event_notification_test.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015 YouCompleteMe contributors +# Copyright (C) 2015-2016 YouCompleteMe contributors # # This file is part of YouCompleteMe. # @@ -29,7 +29,7 @@ MockVimModule() import contextlib import os -from ycm.tests.server_test import Server_test +from ycm.tests import YouCompleteMeInstance from ycmd.responses import ( BuildDiagnosticData, Diagnostic, Location, Range, UnknownExtraConf, ServerError ) @@ -110,278 +110,282 @@ def MockEventNotification( response_method, native_filetype_completer = True ): yield -class EventNotification_test( Server_test ): +@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock ) +@YouCompleteMeInstance() +def EventNotification_FileReadyToParse_NonDiagnostic_Error_test( + ycm, post_vim_message ): - @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock ) - def FileReadyToParse_NonDiagnostic_Error_test( self, post_vim_message ): - # This test validates the behaviour of YouCompleteMe.HandleFileParseRequest - # in combination with YouCompleteMe.OnFileReadyToParse when the completer - # raises an exception handling FileReadyToParse event notification - ERROR_TEXT = 'Some completer response text' + # This test validates the behaviour of YouCompleteMe.HandleFileParseRequest + # in combination with YouCompleteMe.OnFileReadyToParse when the completer + # raises an exception handling FileReadyToParse event notification + ERROR_TEXT = 'Some completer response text' - def ErrorResponse( *args ): - raise ServerError( ERROR_TEXT ) + def ErrorResponse( *args ): + raise ServerError( ERROR_TEXT ) - with MockArbitraryBuffer( 'javascript' ): - with MockEventNotification( ErrorResponse ): - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() + with MockArbitraryBuffer( 'javascript' ): + with MockEventNotification( ErrorResponse ): + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() - # The first call raises a warning - post_vim_message.assert_has_exact_calls( [ - call( ERROR_TEXT, truncate = False ) + # The first call raises a warning + post_vim_message.assert_has_exact_calls( [ + call( ERROR_TEXT, truncate = False ) + ] ) + + # Subsequent calls don't re-raise the warning + ycm.HandleFileParseRequest() + post_vim_message.assert_has_exact_calls( [ + call( ERROR_TEXT, truncate = False ) + ] ) + + # But it does if a subsequent event raises again + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + post_vim_message.assert_has_exact_calls( [ + call( ERROR_TEXT, truncate = False ), + call( ERROR_TEXT, truncate = False ) + ] ) + + +@patch( 'vim.command' ) +@YouCompleteMeInstance() +def EventNotification_FileReadyToParse_NonDiagnostic_Error_NonNative_test( + ycm, vim_command ): + + with MockArbitraryBuffer( 'javascript' ): + with MockEventNotification( None, False ): + ycm.OnFileReadyToParse() + ycm.HandleFileParseRequest() + vim_command.assert_not_called() + + +@patch( 'ycm.client.event_notification._LoadExtraConfFile', + new_callable = ExtendedMock ) +@patch( 'ycm.client.event_notification._IgnoreExtraConfFile', + new_callable = ExtendedMock ) +@YouCompleteMeInstance() +def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test( + ycm, ignore_extra_conf, load_extra_conf ): + + # This test validates the behaviour of YouCompleteMe.HandleFileParseRequest + # in combination with YouCompleteMe.OnFileReadyToParse when the completer + # raises the (special) UnknownExtraConf exception + + FILE_NAME = 'a_file' + MESSAGE = ( 'Found ' + FILE_NAME + '. Load? \n\n(Question can be ' + 'turned off with options, see YCM docs)' ) + + def UnknownExtraConfResponse( *args ): + raise UnknownExtraConf( FILE_NAME ) + + with MockArbitraryBuffer( 'javascript' ): + with MockEventNotification( UnknownExtraConfResponse ): + + # When the user accepts the extra conf, we load it + with patch( 'ycm.vimsupport.PresentDialog', + return_value = 0, + new_callable = ExtendedMock ) as present_dialog: + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ), + ] ) + load_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), ] ) # Subsequent calls don't re-raise the warning - self._server_state.HandleFileParseRequest() - post_vim_message.assert_has_exact_calls( [ - call( ERROR_TEXT, truncate = False ) + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ) + ] ) + load_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), ] ) # But it does if a subsequent event raises again - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() - post_vim_message.assert_has_exact_calls( [ - call( ERROR_TEXT, truncate = False ), - call( ERROR_TEXT, truncate = False ) + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ), + PresentDialog_Confirm_Call( MESSAGE ), + ] ) + load_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), + call( FILE_NAME ), + ] ) + + # When the user rejects the extra conf, we reject it + with patch( 'ycm.vimsupport.PresentDialog', + return_value = 1, + new_callable = ExtendedMock ) as present_dialog: + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ), + ] ) + ignore_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), + ] ) + + # Subsequent calls don't re-raise the warning + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ) + ] ) + ignore_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), + ] ) + + # But it does if a subsequent event raises again + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + + present_dialog.assert_has_exact_calls( [ + PresentDialog_Confirm_Call( MESSAGE ), + PresentDialog_Confirm_Call( MESSAGE ), + ] ) + ignore_extra_conf.assert_has_exact_calls( [ + call( FILE_NAME ), + call( FILE_NAME ), ] ) - @patch( 'vim.command' ) - def FileReadyToParse_NonDiagnostic_Error_NonNative_test( self, vim_command ): - with MockArbitraryBuffer( 'javascript' ): - with MockEventNotification( None, False ): - self._server_state.OnFileReadyToParse() - self._server_state.HandleFileParseRequest() - vim_command.assert_not_called() +@YouCompleteMeInstance() +def EventNotification_FileReadyToParse_Diagnostic_Error_Native_test( ycm ): + _Check_FileReadyToParse_Diagnostic_Error( ycm ) + _Check_FileReadyToParse_Diagnostic_Warning( ycm ) + _Check_FileReadyToParse_Diagnostic_Clean( ycm ) - @patch( 'ycm.client.event_notification._LoadExtraConfFile', - new_callable = ExtendedMock ) - @patch( 'ycm.client.event_notification._IgnoreExtraConfFile', - new_callable = ExtendedMock ) - def FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test( - self, - ignore_extra_conf, - load_extra_conf, - *args ): +@patch( 'vim.command' ) +def _Check_FileReadyToParse_Diagnostic_Error( ycm, vim_command ): + # Tests Vim sign placement and error/warning count python API + # when one error is returned. + def DiagnosticResponse( *args ): + start = Location( 1, 2, 'TEST_BUFFER' ) + end = Location( 1, 4, 'TEST_BUFFER' ) + extent = Range( start, end ) + diagnostic = Diagnostic( [], start, extent, 'expected ;', 'ERROR' ) + return [ BuildDiagnosticData( diagnostic ) ] - # This test validates the behaviour of YouCompleteMe.HandleFileParseRequest - # in combination with YouCompleteMe.OnFileReadyToParse when the completer - # raises the (special) UnknownExtraConf exception + with MockArbitraryBuffer( 'cpp' ): + with MockEventNotification( DiagnosticResponse ): + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + vim_command.assert_has_calls( [ + PlaceSign_Call( 1, 1, 1, True ) + ] ) + eq_( ycm.GetErrorCount(), 1 ) + eq_( ycm.GetWarningCount(), 0 ) - FILE_NAME = 'a_file' - MESSAGE = ( 'Found ' + FILE_NAME + '. Load? \n\n(Question can be ' - 'turned off with options, see YCM docs)' ) - - def UnknownExtraConfResponse( *args ): - raise UnknownExtraConf( FILE_NAME ) - - with MockArbitraryBuffer( 'javascript' ): - with MockEventNotification( UnknownExtraConfResponse ): - - # When the user accepts the extra conf, we load it - with patch( 'ycm.vimsupport.PresentDialog', - return_value = 0, - new_callable = ExtendedMock ) as present_dialog: - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ), - ] ) - load_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - ] ) - - # Subsequent calls don't re-raise the warning - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ) - ] ) - load_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - ] ) - - # But it does if a subsequent event raises again - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ), - PresentDialog_Confirm_Call( MESSAGE ), - ] ) - load_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - call( FILE_NAME ), - ] ) - - # When the user rejects the extra conf, we reject it - with patch( 'ycm.vimsupport.PresentDialog', - return_value = 1, - new_callable = ExtendedMock ) as present_dialog: - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ), - ] ) - ignore_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - ] ) - - # Subsequent calls don't re-raise the warning - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ) - ] ) - ignore_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - ] ) - - # But it does if a subsequent event raises again - self._server_state.OnFileReadyToParse() - assert self._server_state.FileParseRequestReady() - self._server_state.HandleFileParseRequest() - - present_dialog.assert_has_exact_calls( [ - PresentDialog_Confirm_Call( MESSAGE ), - PresentDialog_Confirm_Call( MESSAGE ), - ] ) - ignore_extra_conf.assert_has_exact_calls( [ - call( FILE_NAME ), - call( FILE_NAME ), - ] ) + # Consequent calls to HandleFileParseRequest shouldn't mess with + # existing diagnostics, when there is no new parse request. + vim_command.reset_mock() + ok_( not ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + vim_command.assert_not_called() + eq_( ycm.GetErrorCount(), 1 ) + eq_( ycm.GetWarningCount(), 0 ) - def FileReadyToParse_Diagnostic_Error_Native_test( self ): - self._Check_FileReadyToParse_Diagnostic_Error() - self._Check_FileReadyToParse_Diagnostic_Warning() - self._Check_FileReadyToParse_Diagnostic_Clean() +@patch( 'vim.command' ) +def _Check_FileReadyToParse_Diagnostic_Warning( ycm, vim_command ): + # Tests Vim sign placement/unplacement and error/warning count python API + # when one warning is returned. + # Should be called after _Check_FileReadyToParse_Diagnostic_Error + def DiagnosticResponse( *args ): + start = Location( 2, 2, 'TEST_BUFFER' ) + end = Location( 2, 4, 'TEST_BUFFER' ) + extent = Range( start, end ) + diagnostic = Diagnostic( [], start, extent, 'cast', 'WARNING' ) + return [ BuildDiagnosticData( diagnostic ) ] + + with MockArbitraryBuffer( 'cpp' ): + with MockEventNotification( DiagnosticResponse ): + ycm.OnFileReadyToParse() + ok_( ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + vim_command.assert_has_calls( [ + PlaceSign_Call( 2, 2, 1, False ), + UnplaceSign_Call( 1, 1 ) + ] ) + eq_( ycm.GetErrorCount(), 0 ) + eq_( ycm.GetWarningCount(), 1 ) + + # Consequent calls to HandleFileParseRequest shouldn't mess with + # existing diagnostics, when there is no new parse request. + vim_command.reset_mock() + ok_( not ycm.FileParseRequestReady() ) + ycm.HandleFileParseRequest() + vim_command.assert_not_called() + eq_( ycm.GetErrorCount(), 0 ) + eq_( ycm.GetWarningCount(), 1 ) - @patch( 'vim.command' ) - def _Check_FileReadyToParse_Diagnostic_Error( self, vim_command ): - # Tests Vim sign placement and error/warning count python API - # when one error is returned. - def DiagnosticResponse( *args ): - start = Location( 1, 2, 'TEST_BUFFER' ) - end = Location( 1, 4, 'TEST_BUFFER' ) - extent = Range( start, end ) - diagnostic = Diagnostic( [], start, extent, 'expected ;', 'ERROR' ) - return [ BuildDiagnosticData( diagnostic ) ] - - with MockArbitraryBuffer( 'cpp' ): - with MockEventNotification( DiagnosticResponse ): - self._server_state.OnFileReadyToParse() - ok_( self._server_state.FileParseRequestReady() ) - self._server_state.HandleFileParseRequest() - vim_command.assert_has_calls( [ - PlaceSign_Call( 1, 1, 1, True ) - ] ) - eq_( self._server_state.GetErrorCount(), 1 ) - eq_( self._server_state.GetWarningCount(), 0 ) - - # Consequent calls to HandleFileParseRequest shouldn't mess with - # existing diagnostics, when there is no new parse request. - vim_command.reset_mock() - ok_( not self._server_state.FileParseRequestReady() ) - self._server_state.HandleFileParseRequest() - vim_command.assert_not_called() - eq_( self._server_state.GetErrorCount(), 1 ) - eq_( self._server_state.GetWarningCount(), 0 ) +@patch( 'vim.command' ) +def _Check_FileReadyToParse_Diagnostic_Clean( ycm, vim_command ): + # Tests Vim sign unplacement and error/warning count python API + # when there are no errors/warnings left. + # Should be called after _Check_FileReadyToParse_Diagnostic_Warning + with MockArbitraryBuffer( 'cpp' ): + with MockEventNotification( MagicMock( return_value = [] ) ): + ycm.OnFileReadyToParse() + ycm.HandleFileParseRequest() + vim_command.assert_has_calls( [ + UnplaceSign_Call( 2, 1 ) + ] ) + eq_( ycm.GetErrorCount(), 0 ) + eq_( ycm.GetWarningCount(), 0 ) - @patch( 'vim.command' ) - def _Check_FileReadyToParse_Diagnostic_Warning( self, vim_command ): - # Tests Vim sign placement/unplacement and error/warning count python API - # when one warning is returned. - # Should be called after _Check_FileReadyToParse_Diagnostic_Error - def DiagnosticResponse( *args ): - start = Location( 2, 2, 'TEST_BUFFER' ) - end = Location( 2, 4, 'TEST_BUFFER' ) - extent = Range( start, end ) - diagnostic = Diagnostic( [], start, extent, 'cast', 'WARNING' ) - return [ BuildDiagnosticData( diagnostic ) ] +@patch( 'ycm.youcompleteme.YouCompleteMe._AddUltiSnipsDataIfNeeded' ) +@YouCompleteMeInstance() +def EventNotification_BufferVisit_BuildRequestForCurrentAndUnsavedBuffers_test( + ycm, *args ): - with MockArbitraryBuffer( 'cpp' ): - with MockEventNotification( DiagnosticResponse ): - self._server_state.OnFileReadyToParse() - ok_( self._server_state.FileParseRequestReady() ) - self._server_state.HandleFileParseRequest() - vim_command.assert_has_calls( [ - PlaceSign_Call( 2, 2, 1, False ), - UnplaceSign_Call( 1, 1 ) - ] ) - eq_( self._server_state.GetErrorCount(), 0 ) - eq_( self._server_state.GetWarningCount(), 1 ) + current_buffer_file = os.path.realpath( 'current_buffer' ) + current_buffer = VimBuffer( name = current_buffer_file, + number = 1, + contents = [ 'current_buffer_contents' ], + filetype = 'some_filetype', + modified = False ) - # Consequent calls to HandleFileParseRequest shouldn't mess with - # existing diagnostics, when there is no new parse request. - vim_command.reset_mock() - ok_( not self._server_state.FileParseRequestReady() ) - self._server_state.HandleFileParseRequest() - vim_command.assert_not_called() - eq_( self._server_state.GetErrorCount(), 0 ) - eq_( self._server_state.GetWarningCount(), 1 ) + modified_buffer_file = os.path.realpath( 'modified_buffer' ) + modified_buffer = VimBuffer( name = modified_buffer_file, + number = 2, + contents = [ 'modified_buffer_contents' ], + filetype = 'some_filetype', + modified = True ) - - @patch( 'vim.command' ) - def _Check_FileReadyToParse_Diagnostic_Clean( self, vim_command ): - # Tests Vim sign unplacement and error/warning count python API - # when there are no errors/warnings left. - # Should be called after _Check_FileReadyToParse_Diagnostic_Warning - with MockArbitraryBuffer( 'cpp' ): - with MockEventNotification( MagicMock( return_value = [] ) ): - self._server_state.OnFileReadyToParse() - self._server_state.HandleFileParseRequest() - vim_command.assert_has_calls( [ - UnplaceSign_Call( 2, 1 ) - ] ) - eq_( self._server_state.GetErrorCount(), 0 ) - eq_( self._server_state.GetWarningCount(), 0 ) - - - @patch( 'ycm.youcompleteme.YouCompleteMe._AddUltiSnipsDataIfNeeded' ) - @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync', - new_callable = ExtendedMock ) - def BufferVisit_BuildRequestForCurrentAndUnsavedBuffers_test( - self, post_data_to_handler_async, *args ): - - current_buffer_file = os.path.realpath( 'current_buffer' ) - current_buffer = VimBuffer( name = current_buffer_file, - number = 1, - contents = [ 'current_buffer_content' ], - filetype = 'some_filetype', - modified = False ) - - modified_buffer_file = os.path.realpath( 'modified_buffer' ) - modified_buffer = VimBuffer( name = modified_buffer_file, - number = 2, - contents = [ 'modified_buffer_content' ], + unmodified_buffer_file = os.path.realpath( 'unmodified_buffer' ) + unmodified_buffer = VimBuffer( name = unmodified_buffer_file, + number = 3, + contents = [ 'unmodified_buffer_contents' ], filetype = 'some_filetype', - modified = True ) - - unmodified_buffer_file = os.path.realpath( 'unmodified_buffer' ) - unmodified_buffer = VimBuffer( name = unmodified_buffer_file, - number = 3, - contents = [ 'unmodified_buffer_content' ], - filetype = 'some_filetype', - modified = False ) + modified = False ) + with patch( 'ycm.client.base_request.BaseRequest.' + 'PostDataToHandlerAsync' ) as post_data_to_handler_async: with patch( 'vim.buffers', [ current_buffer, modified_buffer, unmodified_buffer ] ): with patch( 'vim.current.buffer', current_buffer ): with patch( 'vim.current.window.cursor', ( 3, 5 ) ): - self._server_state.OnBufferVisit() + ycm.OnBufferVisit() assert_that( # Positional arguments passed to PostDataToHandlerAsync. @@ -393,11 +397,11 @@ class EventNotification_test( Server_test ): 'column_num': 6, 'file_data': has_entries( { current_buffer_file: has_entries( { - 'contents': 'current_buffer_content\n', + 'contents': 'current_buffer_contents\n', 'filetypes': [ 'some_filetype' ] } ), modified_buffer_file: has_entries( { - 'contents': 'modified_buffer_content\n', + 'contents': 'modified_buffer_contents\n', 'filetypes': [ 'some_filetype' ] } ) } ), @@ -408,49 +412,49 @@ class EventNotification_test( Server_test ): ) - @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync', - new_callable = ExtendedMock ) - def BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test( - self, post_data_to_handler_async ): +@YouCompleteMeInstance() +def EventNotification_BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test( + ycm ): + current_buffer_file = os.path.realpath( 'current_buffer' ) + current_buffer = VimBuffer( name = current_buffer_file, + number = 1, + contents = [ 'current_buffer_contents' ], + filetype = 'some_filetype', + modified = True ) - current_buffer_file = os.path.realpath( 'current_buffer' ) - current_buffer = VimBuffer( name = current_buffer_file, - number = 1, - contents = [ 'current_buffer_content' ], - filetype = 'some_filetype', - modified = True ) - - deleted_buffer_file = os.path.realpath( 'deleted_buffer' ) - deleted_buffer = VimBuffer( name = deleted_buffer_file, - number = 2, - contents = [ 'deleted_buffer_content' ], - filetype = 'some_filetype', - modified = False ) + deleted_buffer_file = os.path.realpath( 'deleted_buffer' ) + deleted_buffer = VimBuffer( name = deleted_buffer_file, + number = 2, + contents = [ 'deleted_buffer_contents' ], + filetype = 'some_filetype', + modified = False ) + with patch( 'ycm.client.base_request.BaseRequest.' + 'PostDataToHandlerAsync' ) as post_data_to_handler_async: with patch( 'vim.buffers', [ current_buffer, deleted_buffer ] ): with patch( 'vim.current.buffer', current_buffer ): - self._server_state.OnBufferUnload( deleted_buffer_file ) + ycm.OnBufferUnload( deleted_buffer_file ) - assert_that( - # Positional arguments passed to PostDataToHandlerAsync. - post_data_to_handler_async.call_args[ 0 ], - contains( - has_entries( { - 'filepath': deleted_buffer_file, - 'line_num': 1, - 'column_num': 1, - 'file_data': has_entries( { - current_buffer_file: has_entries( { - 'contents': 'current_buffer_content\n', - 'filetypes': [ 'some_filetype' ] - } ), - deleted_buffer_file: has_entries( { - 'contents': 'deleted_buffer_content\n', - 'filetypes': [ 'some_filetype' ] - } ) + assert_that( + # Positional arguments passed to PostDataToHandlerAsync. + post_data_to_handler_async.call_args[ 0 ], + contains( + has_entries( { + 'filepath': deleted_buffer_file, + 'line_num': 1, + 'column_num': 1, + 'file_data': has_entries( { + current_buffer_file: has_entries( { + 'contents': 'current_buffer_contents\n', + 'filetypes': [ 'some_filetype' ] } ), - 'event_name': 'BufferUnload' + deleted_buffer_file: has_entries( { + 'contents': 'deleted_buffer_contents\n', + 'filetypes': [ 'some_filetype' ] + } ) } ), - 'event_notification' - ) + 'event_name': 'BufferUnload' + } ), + 'event_notification' ) + ) diff --git a/python/ycm/tests/omni_completer_test.py b/python/ycm/tests/omni_completer_test.py index 7f53a591..cc5497ea 100644 --- a/python/ycm/tests/omni_completer_test.py +++ b/python/ycm/tests/omni_completer_test.py @@ -33,9 +33,8 @@ from hamcrest import contains_string from ycm.test_utils import MockVimModule, ExtendedMock MockVimModule() -from ycm.omni_completer import OmniCompleter from ycm.test_utils import ExpectedFailure -from ycm.tests.server_test import MakeUserOptions, Server_test +from ycm.tests import YouCompleteMeInstance from ycmd.utils import ToBytes from ycmd.request_wrap import RequestWrap @@ -74,240 +73,309 @@ def BuildRequestWrap( line_num, column_num, contents ): return RequestWrap( BuildRequest( line_num, column_num, contents ) ) -class OmniCompleter_test( Server_test ): +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_List_test( ycm ): + contents = 'test.' + request_data = BuildRequestWrap( line_num = 1, + column_num = 6, + contents = contents ) - def OmniCompleter_GetCompletions_Cache_List_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - contents = 'test.' - request_data = BuildRequestWrap( line_num = 1, - column_num = 6, - contents = contents ) + omnifunc_result = [ ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'cdef' ) ] + + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'')" ), + ] ) + + eq_( results, omnifunc_result ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_ListFilter_test( ycm ): + contents = 'test.t' + request_data = BuildRequestWrap( line_num = 1, + column_num = 7, + contents = contents ) - omnifunc_result = [ ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'cdef' ) ] + eq_( request_data[ 'query' ], 't' ) - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'')" ), - ] ) + omnifunc_result = [ ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'cdef' ) ] - eq_( results, omnifunc_result ) + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'t')" ), + ] ) + + eq_( results, [] ) - def OmniCompleter_GetCompletions_Cache_ListFilter_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) - - contents = 'test.t' - request_data = BuildRequestWrap( line_num = 1, - column_num = 7, - contents = contents ) - - eq_( request_data[ 'query' ], 't' ) - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'cdef' ) ] - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'t')" ), - ] ) - - eq_( results, [] ) +@YouCompleteMeInstance( { 'cache_omnifunc': 0 } ) +def OmniCompleter_GetCompletions_NoCache_List_test( ycm ): + contents = 'test.' + request_data = BuildRequestWrap( line_num = 1, + column_num = 6, + contents = contents ) - def OmniCompleter_GetCompletions_NoCache_List_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - contents = 'test.' - request_data = BuildRequestWrap( line_num = 1, - column_num = 6, - contents = contents ) + omnifunc_result = [ ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'cdef' ) ] + + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'')" ), + ] ) + + eq_( results, omnifunc_result ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) +@YouCompleteMeInstance( { 'cache_omnifunc': 0 } ) +def OmniCompleter_GetCompletions_NoCache_ListFilter_test( ycm ): + contents = 'test.t' + request_data = BuildRequestWrap( line_num = 1, + column_num = 7, + contents = contents ) - omnifunc_result = [ ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'cdef' ) ] + eq_( request_data[ 'query' ], 't' ) - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'')" ), - ] ) + omnifunc_result = [ ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'cdef' ) ] - eq_( results, omnifunc_result ) + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'t')" ), + ] ) + + # actual result is that the results are not filtered, as we expect the + # omniufunc or vim itself to do this filtering + eq_( results, omnifunc_result ) - def OmniCompleter_GetCompletions_NoCache_ListFilter_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) +@ExpectedFailure( 'We ignore the result of the call to findstart and use our ' + 'own interpretation of where the identifier should be', + contains_string( "test_omnifunc(0,'t')" ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletsions_UseFindStart_test( ycm ): + contents = 'test.t' + request_data = BuildRequestWrap( line_num = 1, + column_num = 7, + contents = contents ) - contents = 'test.t' - request_data = BuildRequestWrap( line_num = 1, - column_num = 7, - contents = contents ) + eq_( request_data[ 'query' ], 't' ) - eq_( request_data[ 'query' ], 't' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) + omnifunc_result = [ ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'cdef' ) ] - omnifunc_result = [ ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'cdef' ) ] + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 1, omnifunc_result ] ) as vim_eval: + results = ycm._omnicomp.ComputeCandidates( request_data ) - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'t')" ), - ] ) + # Fails here: actual result is that the findstart result (1) is ignored + # and we use the 't' query as we normally would on the server side + call( "test_omnifunc(0,'test.t')" ), + ] ) - # actual result is that the results are not filtered, as we expect the - # omniufunc or vim itself to do this filtering - eq_( results, omnifunc_result ) + eq_( results, omnifunc_result ) - @ExpectedFailure( 'We ignore the result of the call to findstart and use our ' - 'own interpretation of where the identifier should be', - contains_string( "test_omnifunc(0,'t')" ) ) - def OmniCompleter_GetCompletsions_UseFindStart_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_Object_test( ycm ): + contents = 'test.t' + request_data = BuildRequestWrap( line_num = 1, + column_num = 7, + contents = contents ) - contents = 'test.t' - request_data = BuildRequestWrap( line_num = 1, - column_num = 7, - contents = contents ) + eq_( request_data[ 'query' ], 't' ) - eq_( request_data[ 'query' ], 't' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) + omnifunc_result = { + 'words': [ + ToBytesOnPY2( 'a' ), + ToBytesOnPY2( 'b' ), + ToBytesOnPY2( 'CDtEF' ) + ] + } - omnifunc_result = [ ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'cdef' ) ] + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 1, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'t')" ), + ] ) - # Fails here: actual result is that the findstart result (1) is ignored - # and we use the 't' query as we normally would on the server side - call( "test_omnifunc(0,'test.t')" ), - ] ) - - eq_( results, omnifunc_result ) + eq_( results, [ ToBytesOnPY2( 'CDtEF' ) ] ) - def OmniCompleter_GetCompletions_Cache_Object_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_ObjectList_test( ycm ): + contents = 'test.tt' + request_data = BuildRequestWrap( line_num = 1, + column_num = 8, + contents = contents ) - contents = 'test.t' - request_data = BuildRequestWrap( line_num = 1, - column_num = 7, - contents = contents ) + eq_( request_data[ 'query' ], 'tt' ) - eq_( request_data[ 'query' ], 't' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = { - 'words': [ - ToBytesOnPY2( 'a' ), - ToBytesOnPY2( 'b' ), - ToBytesOnPY2( 'CDtEF' ) - ] + omnifunc_result = [ + { + 'word': ToBytesOnPY2( 'a' ), + 'abbr': ToBytesOnPY2( 'ABBR'), + 'menu': ToBytesOnPY2( 'MENU' ), + 'info': ToBytesOnPY2( 'INFO' ), + 'kind': ToBytesOnPY2( 'K' ) + }, + { + 'word': ToBytesOnPY2( 'test' ), + 'abbr': ToBytesOnPY2( 'ABBRTEST'), + 'menu': ToBytesOnPY2( 'MENUTEST' ), + 'info': ToBytesOnPY2( 'INFOTEST' ), + 'kind': ToBytesOnPY2( 'T' ) } + ] - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'t')" ), - ] ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'tt')" ), + ] ) - eq_( results, [ ToBytesOnPY2( 'CDtEF' ) ] ) + eq_( results, [ omnifunc_result[ 1 ] ] ) - def OmniCompleter_GetCompletions_Cache_ObjectList_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 0 } ) +def OmniCompleter_GetCompletions_NoCache_ObjectList_test( ycm ): + contents = 'test.tt' + request_data = BuildRequestWrap( line_num = 1, + column_num = 8, + contents = contents ) - contents = 'test.tt' - request_data = BuildRequestWrap( line_num = 1, - column_num = 8, - contents = contents ) + eq_( request_data[ 'query' ], 'tt' ) - eq_( request_data[ 'query' ], 'tt' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) + omnifunc_result = [ + { + 'word': ToBytesOnPY2( 'a' ), + 'abbr': ToBytesOnPY2( 'ABBR'), + 'menu': ToBytesOnPY2( 'MENU' ), + 'info': ToBytesOnPY2( 'INFO' ), + 'kind': ToBytesOnPY2( 'K' ) + }, + { + 'word': ToBytesOnPY2( 'test' ), + 'abbr': ToBytesOnPY2( 'ABBRTEST'), + 'menu': ToBytesOnPY2( 'MENUTEST' ), + 'info': ToBytesOnPY2( 'INFOTEST' ), + 'kind': ToBytesOnPY2( 'T' ) + } + ] - omnifunc_result = [ + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'tt')" ), + ] ) + + # We don't filter the result - we expect the omnifunc to do that + # based on the query we supplied (Note: that means no fuzzy matching!) + eq_( results, omnifunc_result ) + + +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_ObjectListObject_test( ycm ): + contents = 'test.tt' + request_data = BuildRequestWrap( line_num = 1, + column_num = 8, + contents = contents ) + + eq_( request_data[ 'query' ], 'tt' ) + + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) + + omnifunc_result = { + 'words': [ { 'word': ToBytesOnPY2( 'a' ), 'abbr': ToBytesOnPY2( 'ABBR'), @@ -323,39 +391,38 @@ class OmniCompleter_test( Server_test ): 'kind': ToBytesOnPY2( 'T' ) } ] + } - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'tt')" ), - ] ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'tt')" ), + ] ) - eq_( results, [ omnifunc_result[ 1 ] ] ) + eq_( results, [ omnifunc_result[ 'words' ][ 1 ] ] ) - def OmniCompleter_GetCompletions_NoCache_ObjectList_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 0 } ) +def OmniCompleter_GetCompletions_NoCache_ObjectListObject_test( ycm ): + contents = 'test.tt' + request_data = BuildRequestWrap( line_num = 1, + column_num = 8, + contents = contents ) - contents = 'test.tt' - request_data = BuildRequestWrap( line_num = 1, - column_num = 8, - contents = contents ) + eq_( request_data[ 'query' ], 'tt' ) - eq_( request_data[ 'query' ], 'tt' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ + omnifunc_result = { + 'words': [ { 'word': ToBytesOnPY2( 'a' ), 'abbr': ToBytesOnPY2( 'ABBR'), @@ -371,277 +438,209 @@ class OmniCompleter_test( Server_test ): 'kind': ToBytesOnPY2( 'T' ) } ] + } - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'tt')" ), - ] ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'tt')" ), + ] ) - # We don't filter the result - we expect the omnifunc to do that - # based on the query we supplied (Note: that means no fuzzy matching!) - eq_( results, omnifunc_result ) + # No FilterAndSortCandidates for cache_omnifunc=0 (we expect the omnifunc + # to do the filtering?) + eq_( results, omnifunc_result[ 'words' ] ) - def OmniCompleter_GetCompletions_Cache_ObjectListObject_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_List_Unicode_test( ycm ): + contents = '†åsty_π.' + request_data = BuildRequestWrap( line_num = 1, + column_num = 13, + contents = contents ) - contents = 'test.tt' - request_data = BuildRequestWrap( line_num = 1, - column_num = 8, - contents = contents ) - eq_( request_data[ 'query' ], 'tt' ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) + omnifunc_result = [ ToBytesOnPY2( '†est' ), + ToBytesOnPY2( 'å_unicode_identifier' ), + ToBytesOnPY2( 'πππππππ yummy πie' ) ] - omnifunc_result = { - 'words': [ - { - 'word': ToBytesOnPY2( 'a' ), - 'abbr': ToBytesOnPY2( 'ABBR'), - 'menu': ToBytesOnPY2( 'MENU' ), - 'info': ToBytesOnPY2( 'INFO' ), - 'kind': ToBytesOnPY2( 'K' ) - }, - { - 'word': ToBytesOnPY2( 'test' ), - 'abbr': ToBytesOnPY2( 'ABBRTEST'), - 'menu': ToBytesOnPY2( 'MENUTEST' ), - 'info': ToBytesOnPY2( 'INFOTEST' ), - 'kind': ToBytesOnPY2( 'T' ) - } - ] + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'')" ), + ] ) + + eq_( results, omnifunc_result ) + + +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_NoCache_List_Unicode_test( ycm ): + contents = '†åsty_π.' + request_data = BuildRequestWrap( line_num = 1, + column_num = 13, + contents = contents ) + + + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) + + omnifunc_result = [ ToBytesOnPY2( '†est' ), + ToBytesOnPY2( 'å_unicode_identifier' ), + ToBytesOnPY2( 'πππππππ yummy πie' ) ] + + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'')" ), + ] ) + + eq_( results, omnifunc_result ) + + +@ExpectedFailure( 'Filtering on unicode is not supported by the server' ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_List_Filter_Unicode_test( ycm ): + contents = '†åsty_π.ππ' + request_data = BuildRequestWrap( line_num = 1, + column_num = 17, + contents = contents ) + + + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) + + omnifunc_result = [ ToBytesOnPY2( '†est' ), + ToBytesOnPY2( 'å_unicode_identifier' ), + ToBytesOnPY2( 'πππππππ yummy πie' ) ] + + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'ππ')" ), + ] ) + + # Fails here: Filtering on unicode is not supported + eq_( results, [ omnifunc_result[ 2 ] ] ) + + +@YouCompleteMeInstance( { 'cache_omnifunc': 0 } ) +def OmniCompleter_GetCompletions_NoCache_List_Filter_Unicode_test( ycm ): + contents = '†åsty_π.ππ' + request_data = BuildRequestWrap( line_num = 1, + column_num = 17, + contents = contents ) + + + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) + + omnifunc_result = [ ToBytesOnPY2( 'πππππππ yummy πie' ) ] + + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: + + results = ycm._omnicomp.ComputeCandidates( request_data ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'ππ')" ), + ] ) + + eq_( results, omnifunc_result ) + + +@ExpectedFailure( 'Filtering on unicode is not supported by the server' ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_ObjectList_Unicode_test( ycm ): + contents = '†åsty_π.ππ' + request_data = BuildRequestWrap( line_num = 1, + column_num = 17, + contents = contents ) + + + eq_( request_data[ 'query' ], 'ππ' ) + + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) + + omnifunc_result = [ + { + 'word': ToBytesOnPY2( 'ålpha∫et' ), + 'abbr': ToBytesOnPY2( 'å∫∫®'), + 'menu': ToBytesOnPY2( 'µ´~¨á' ), + 'info': ToBytesOnPY2( '^~fo' ), + 'kind': ToBytesOnPY2( '˚' ) + }, + { + 'word': ToBytesOnPY2( 'π†´ß†π' ), + 'abbr': ToBytesOnPY2( 'ÅııÂʉÍÊ'), + 'menu': ToBytesOnPY2( '˜‰ˆËʉÍÊ' ), + 'info': ToBytesOnPY2( 'ȈÏØʉÍÊ' ), + 'kind': ToBytesOnPY2( 'Ê' ) } + ] - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'tt')" ), - ] ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'ππ')" ), + ] ) - eq_( results, [ omnifunc_result[ 'words' ][ 1 ] ] ) + # Fails here: Filtering on unicode is not supported + eq_( results, [ omnifunc_result[ 1 ] ] ) - def OmniCompleter_GetCompletions_NoCache_ObjectListObject_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) - - contents = 'test.tt' - request_data = BuildRequestWrap( line_num = 1, - column_num = 8, - contents = contents ) - - eq_( request_data[ 'query' ], 'tt' ) - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = { - 'words': [ - { - 'word': ToBytesOnPY2( 'a' ), - 'abbr': ToBytesOnPY2( 'ABBR'), - 'menu': ToBytesOnPY2( 'MENU' ), - 'info': ToBytesOnPY2( 'INFO' ), - 'kind': ToBytesOnPY2( 'K' ) - }, - { - 'word': ToBytesOnPY2( 'test' ), - 'abbr': ToBytesOnPY2( 'ABBRTEST'), - 'menu': ToBytesOnPY2( 'MENUTEST' ), - 'info': ToBytesOnPY2( 'INFOTEST' ), - 'kind': ToBytesOnPY2( 'T' ) - } - ] - } - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'tt')" ), - ] ) - - # No FilterAndSortCandidates for cache_omnifunc=0 (we expect the omnifunc - # to do the filtering?) - eq_( results, omnifunc_result[ 'words' ] ) +@YouCompleteMeInstance( { 'cache_omnifunc': 1 } ) +def OmniCompleter_GetCompletions_Cache_ObjectListObject_Unicode_test( ycm ): + contents = '†åsty_π.t' + request_data = BuildRequestWrap( line_num = 1, + column_num = 14, + contents = contents ) - def OmniCompleter_GetCompletions_Cache_List_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) + eq_( request_data[ 'query' ], 't' ) - contents = '†åsty_π.' - request_data = BuildRequestWrap( line_num = 1, - column_num = 13, - contents = contents ) + # Make sure there is an omnifunc set up. + with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): + ycm._omnicomp.OnFileReadyToParse( request_data ) - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ ToBytesOnPY2( '†est' ), - ToBytesOnPY2( 'å_unicode_identifier' ), - ToBytesOnPY2( 'πππππππ yummy πie' ) ] - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'')" ), - ] ) - - eq_( results, omnifunc_result ) - - - def OmniCompleter_GetCompletions_NoCache_List_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) - - contents = '†åsty_π.' - request_data = BuildRequestWrap( line_num = 1, - column_num = 13, - contents = contents ) - - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ ToBytesOnPY2( '†est' ), - ToBytesOnPY2( 'å_unicode_identifier' ), - ToBytesOnPY2( 'πππππππ yummy πie' ) ] - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'')" ), - ] ) - - eq_( results, omnifunc_result ) - - - @ExpectedFailure( 'Filtering on unicode is not supported by the server' ) - def OmniCompleter_GetCompletions_Cache_List_Filter_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) - - contents = '†åsty_π.ππ' - request_data = BuildRequestWrap( line_num = 1, - column_num = 17, - contents = contents ) - - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ ToBytesOnPY2( '†est' ), - ToBytesOnPY2( 'å_unicode_identifier' ), - ToBytesOnPY2( 'πππππππ yummy πie' ) ] - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'ππ')" ), - ] ) - - # Fails here: Filtering on unicode is not supported - eq_( results, [ omnifunc_result[ 2 ] ] ) - - - def OmniCompleter_GetCompletions_NoCache_List_Filter_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 0 - } ) ) - - contents = '†åsty_π.ππ' - request_data = BuildRequestWrap( line_num = 1, - column_num = 17, - contents = contents ) - - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ ToBytesOnPY2( 'πππππππ yummy πie' ) ] - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'ππ')" ), - ] ) - - eq_( results, omnifunc_result ) - - - @ExpectedFailure( 'Filtering on unicode is not supported by the server' ) - def OmniCompleter_GetCompletions_Cache_ObjectList_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) - - contents = '†åsty_π.ππ' - request_data = BuildRequestWrap( line_num = 1, - column_num = 17, - contents = contents ) - - - eq_( request_data[ 'query' ], 'ππ' ) - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = [ + omnifunc_result = { + 'words': [ { 'word': ToBytesOnPY2( 'ålpha∫et' ), 'abbr': ToBytesOnPY2( 'å∫∫®'), @@ -655,87 +654,36 @@ class OmniCompleter_test( Server_test ): 'menu': ToBytesOnPY2( '˜‰ˆËʉÍÊ' ), 'info': ToBytesOnPY2( 'ȈÏØʉÍÊ' ), 'kind': ToBytesOnPY2( 'Ê' ) + }, + { + 'word': ToBytesOnPY2( 'test' ), + 'abbr': ToBytesOnPY2( 'ÅııÂʉÍÊ'), + 'menu': ToBytesOnPY2( '˜‰ˆËʉÍÊ' ), + 'info': ToBytesOnPY2( 'ȈÏØʉÍÊ' ), + 'kind': ToBytesOnPY2( 'Ê' ) } ] + } - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: + # And get the completions + with patch( 'vim.eval', + new_callable = ExtendedMock, + side_effect = [ 6, omnifunc_result ] ) as vim_eval: - results = omni_completer.ComputeCandidates( request_data ) + results = ycm._omnicomp.ComputeCandidates( request_data ) - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'ππ')" ), - ] ) + vim_eval.assert_has_exact_calls( [ + call( 'test_omnifunc(1,"")' ), + call( "test_omnifunc(0,'t')" ), + ] ) - # Fails here: Filtering on unicode is not supported - eq_( results, [ omnifunc_result[ 1 ] ] ) - - - def OmniCompleter_GetCompletions_Cache_ObjectListObject_Unicode_test( self ): - omni_completer = OmniCompleter( MakeUserOptions( { - 'cache_omnifunc': 1 - } ) ) - - contents = '†åsty_π.t' - request_data = BuildRequestWrap( line_num = 1, - column_num = 14, - contents = contents ) - - - eq_( request_data[ 'query' ], 't' ) - - # Make sure there is an omnifunc set up. - with patch( 'vim.eval', return_value = ToBytesOnPY2( 'test_omnifunc' ) ): - omni_completer.OnFileReadyToParse( request_data ) - - omnifunc_result = { - 'words': [ - { - 'word': ToBytesOnPY2( 'ålpha∫et' ), - 'abbr': ToBytesOnPY2( 'å∫∫®'), - 'menu': ToBytesOnPY2( 'µ´~¨á' ), - 'info': ToBytesOnPY2( '^~fo' ), - 'kind': ToBytesOnPY2( '˚' ) - }, - { - 'word': ToBytesOnPY2( 'π†´ß†π' ), - 'abbr': ToBytesOnPY2( 'ÅııÂʉÍÊ'), - 'menu': ToBytesOnPY2( '˜‰ˆËʉÍÊ' ), - 'info': ToBytesOnPY2( 'ȈÏØʉÍÊ' ), - 'kind': ToBytesOnPY2( 'Ê' ) - }, - { - 'word': ToBytesOnPY2( 'test' ), - 'abbr': ToBytesOnPY2( 'ÅııÂʉÍÊ'), - 'menu': ToBytesOnPY2( '˜‰ˆËʉÍÊ' ), - 'info': ToBytesOnPY2( 'ȈÏØʉÍÊ' ), - 'kind': ToBytesOnPY2( 'Ê' ) - } - ] - } - - # And get the completions - with patch( 'vim.eval', - new_callable = ExtendedMock, - side_effect = [ 6, omnifunc_result ] ) as vim_eval: - - results = omni_completer.ComputeCandidates( request_data ) - - vim_eval.assert_has_exact_calls( [ - call( 'test_omnifunc(1,"")' ), - call( "test_omnifunc(0,'t')" ), - ] ) - - # Note: the filtered results are all unicode objects (not bytes) because - # they are passed through the FilterAndSortCandidates machinery - # (via the server) - eq_( results, [ { - 'word': 'test', - 'abbr': 'ÅııÂʉÍÊ', - 'menu': '˜‰ˆËʉÍÊ', - 'info': 'ȈÏØʉÍÊ', - 'kind': 'Ê' - } ] ) + # Note: the filtered results are all unicode objects (not bytes) because + # they are passed through the FilterAndSortCandidates machinery + # (via the server) + eq_( results, [ { + 'word': 'test', + 'abbr': 'ÅııÂʉÍÊ', + 'menu': '˜‰ˆËʉÍÊ', + 'info': 'ȈÏØʉÍÊ', + 'kind': 'Ê' + } ] ) diff --git a/python/ycm/tests/postcomplete_test.py b/python/ycm/tests/postcomplete_test.py index 67253d2d..8f10243e 100644 --- a/python/ycm/tests/postcomplete_test.py +++ b/python/ycm/tests/postcomplete_test.py @@ -34,7 +34,7 @@ from mock import MagicMock, DEFAULT, patch from nose.tools import eq_, ok_ from ycm import vimsupport -from ycm.tests.server_test import Server_test +from ycm.tests import YouCompleteMeInstance from ycmd.utils import ToBytes @@ -66,379 +66,387 @@ def BuildCompletion( namespace = None, insertion_text = 'Test', } -class PostComplete_test( Server_test ): - - @contextlib.contextmanager - def _SetupForCsharpCompletionDone( self, completions ): - with patch( 'ycm.vimsupport.InsertNamespace' ): - with patch( 'ycm.vimsupport.TextBeforeCursor', - return_value = ' Test' ): - request = MagicMock() - request.Done = MagicMock( return_value = True ) - request.RawResponse = MagicMock( return_value = completions ) - self._server_state._latest_completion_request = request - yield - - - @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'cs' ] ) - def GetCompleteDoneHooks_ResultOnCsharp_test( self, *args ): - result = self._server_state.GetCompleteDoneHooks() - eq_( 1, len( list( result ) ) ) - - - @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) - def GetCompleteDoneHooks_EmptyOnOtherFiletype_test( self, *args ): - result = self._server_state.GetCompleteDoneHooks() - eq_( 0, len( list( result ) ) ) - - - @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) - def OnCompleteDone_WithActionCallsIt_test( self, *args ): - action = MagicMock() - self._server_state._complete_done_hooks[ 'txt' ] = action - self._server_state.OnCompleteDone() - - ok_( action.called ) - - - @patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) - def OnCompleteDone_NoActionNoError_test( self, *args ): - self._server_state.OnCompleteDone() - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Test' ) ) - def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - eq_( list( result ), completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'A' ) ) - def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'AAA' ) ] - self._server_state._FilterToMatchingCompletions( completions, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Test' ) ) - def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - eq_( list( result ), completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( ' Quote' ) ) - def FilterToCompletedCompletions_NewVim_NonMatchIsntReturned_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'A' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - assert_that( list( result ), empty() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( '†es†' ) ) - def FilterToCompletedCompletions_NewVim_Unicode_test( self, *args ): - completions = [ BuildCompletion( insertion_text = '†es†' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - eq_( list( result ), completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ) - def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - eq_( list( result ), completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' ) - def FilterToCompletedCompletions_OldVim_ShortTextDoesntRaise_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'AAA' ) ] - self._server_state._FilterToMatchingCompletions( completions, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' ) - def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - eq_( list( result ), completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( self, - *args ): - completions = [ BuildCompletion( insertion_text = 'A' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - assert_that( list( result ), empty() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniçø∂¢' ) - def FilterToCompletedCompletions_OldVim_Unicode_test( self, *args ): - completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ] - result = self._server_state._FilterToMatchingCompletions( completions, - False ) - assert_that( list( result ), empty() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Te' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, True ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ShortTextDoesntRaise_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = "AAA" ) ] - self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_NonMatchIsntReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'A' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_Unicode_test( - self, *args ): - completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, True ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Te' ) ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, True ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'X' ) ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'AAA' ) ] - self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Test' ) ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = 'Test' ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( ' Quote' ) ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_NonMatchIsntReturned_test( # noqa - self, *args ): - completions = [ BuildCompletion( insertion_text = "A" ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, False ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Uniç' ) ) - @patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' ) - def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_Unicode_test( - self, *args ): - completions = [ BuildCompletion( insertion_text = "Uniçø∂¢" ) ] - result = self._server_state._HasCompletionsThatCouldBeCompletedWithMoreText( - completions ) - eq_( result, True ) - - - def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test( self ): - eq_( None, self._server_state._GetRequiredNamespaceImport( {} ) ) - - - def GetRequiredNamespaceImport_ReturnNamespaceFromExtraData_test( self ): - namespace = 'A_NAMESPACE' - eq_( namespace, self._server_state._GetRequiredNamespaceImport( - BuildCompletion( namespace ) - ) ) - - - def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfNotDone_test( self ): - with self._SetupForCsharpCompletionDone( [] ): - self._server_state._latest_completion_request.Done = MagicMock( - return_value = False ) - eq_( [], self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Te' ) ) - def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_NewVim_test( # noqa - self, *args ): - completions = [ BuildCompletion( None ) ] - with self._SetupForCsharpCompletionDone( completions ): - eq_( [], self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_OldVim_test( # noqa - self, *args ): - completions = [ BuildCompletion( None ) ] - with self._SetupForCsharpCompletionDone( completions ): - with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Te' ): - eq_( [], self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatches_NewVim_test( - self, *args ): - info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] - completions = [ BuildCompletion( *info ) ] - with self._SetupForCsharpCompletionDone( completions ): - with patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): - eq_( completions, - self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatchesEvenIfPartial_NewVim_test( # noqa - self, *args ): - info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] - completions = [ BuildCompletion( *info ), - BuildCompletion( insertion_text = 'TestTest' ) ] - with self._SetupForCsharpCompletionDone( completions ): - with patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): - eq_( [ completions[ 0 ] ], - self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - def GetCompletionsUserMayHaveCompleted_DontReturnMatchIfNontExactMatchesAndPartial_NewVim_test( # noqa - self, *args ): - info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] - completions = [ BuildCompletion( insertion_text = info[ 0 ] ), - BuildCompletion( insertion_text = 'TestTest' ) ] - with self._SetupForCsharpCompletionDone( completions ): - with patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): - eq_( [], self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) - @patch( 'ycm.vimsupport.GetVariableValue', - GetVariableValue_CompleteItemIs( 'Test' ) ) - def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_NewVim_test( - self, *args ): - completions = [ BuildCompletion( None ) ] - with self._SetupForCsharpCompletionDone( completions ): - eq_( completions, - self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_OldVim_test( - self, *args ): - completions = [ BuildCompletion( None ) ] - with self._SetupForCsharpCompletionDone( completions ): - eq_( completions, - self._server_state.GetCompletionsUserMayHaveCompleted() ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - def PostCompleteCsharp_EmptyDoesntInsertNamespace_test( self, *args ): - with self._SetupForCsharpCompletionDone( [] ): - self._server_state._OnCompleteDone_Csharp() - ok_( not vimsupport.InsertNamespace.called ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - def PostCompleteCsharp_ExistingWithoutNamespaceDoesntInsertNamespace_test( - self, *args ): - completions = [ BuildCompletion( None ) ] - with self._SetupForCsharpCompletionDone( completions ): - self._server_state._OnCompleteDone_Csharp() - ok_( not vimsupport.InsertNamespace.called ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - def PostCompleteCsharp_ValueDoesInsertNamespace_test( self, *args ): - namespace = 'A_NAMESPACE' - completions = [ BuildCompletion( namespace ) ] - with self._SetupForCsharpCompletionDone( completions ): - self._server_state._OnCompleteDone_Csharp() - vimsupport.InsertNamespace.assert_called_once_with( namespace ) - - - @patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) - @patch( 'ycm.vimsupport.PresentDialog', return_value = 1 ) - def PostCompleteCsharp_InsertSecondNamespaceIfSelected_test( self, *args ): - namespace = 'A_NAMESPACE' - namespace2 = 'ANOTHER_NAMESPACE' - completions = [ - BuildCompletion( namespace ), - BuildCompletion( namespace2 ), - ] - with self._SetupForCsharpCompletionDone( completions ): - self._server_state._OnCompleteDone_Csharp() - vimsupport.InsertNamespace.assert_called_once_with( namespace2 ) +@contextlib.contextmanager +def _SetupForCsharpCompletionDone( ycm, completions ): + with patch( 'ycm.vimsupport.InsertNamespace' ): + with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ): + request = MagicMock() + request.Done = MagicMock( return_value = True ) + request.RawResponse = MagicMock( return_value = completions ) + ycm._latest_completion_request = request + yield + + +@patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'cs' ] ) +@YouCompleteMeInstance() +def GetCompleteDoneHooks_ResultOnCsharp_test( ycm, *args ): + result = ycm.GetCompleteDoneHooks() + eq_( 1, len( list( result ) ) ) + + +@patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) +@YouCompleteMeInstance() +def GetCompleteDoneHooks_EmptyOnOtherFiletype_test( ycm, *args ): + result = ycm.GetCompleteDoneHooks() + eq_( 0, len( list( result ) ) ) + + +@patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) +@YouCompleteMeInstance() +def OnCompleteDone_WithActionCallsIt_test( ycm, *args ): + action = MagicMock() + ycm._complete_done_hooks[ 'txt' ] = action + ycm.OnCompleteDone() + ok_( action.called ) + + +@patch( 'ycm.vimsupport.CurrentFiletypes', return_value = [ 'txt' ] ) +@YouCompleteMeInstance() +def OnCompleteDone_NoActionNoError_test( ycm, *args ): + ycm.OnCompleteDone() + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Test' ) ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + eq_( list( result ), completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'A' ) ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'AAA' ) ] + ycm._FilterToMatchingCompletions( completions, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Test' ) ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + eq_( list( result ), completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( ' Quote' ) ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_NewVim_NonMatchIsntReturned_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'A' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + assert_that( list( result ), empty() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( '†es†' ) ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_NewVim_Unicode_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = '†es†' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + eq_( list( result ), completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + eq_( list( result ), completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_OldVim_ShortTextDoesntRaise_test( ycm, + *args ): + completions = [ BuildCompletion( insertion_text = 'AAA' ) ] + ycm._FilterToMatchingCompletions( completions, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( ycm, + *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + eq_( list( result ), completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( ycm, + *args ): + completions = [ BuildCompletion( insertion_text = 'A' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + assert_that( list( result ), empty() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniçø∂¢' ) +@YouCompleteMeInstance() +def FilterToCompletedCompletions_OldVim_Unicode_test( ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ] + result = ycm._FilterToMatchingCompletions( completions, False ) + assert_that( list( result ), empty() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Te' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, True ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ShortTextDoesntRaise_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = "AAA" ) ] + ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_NonMatchIsntReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'A' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_Unicode_test( + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, True ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Te' ) ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, True ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'X' ) ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'AAA' ) ] + ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Test' ) ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Test' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( ' Quote' ) ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_NonMatchIsntReturned_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( insertion_text = "A" ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, False ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Uniç' ) ) +@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' ) +@YouCompleteMeInstance() +def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_Unicode_test( + ycm, *args ): + completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ] + result = ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions ) + eq_( result, True ) + + +@YouCompleteMeInstance() +def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test( ycm ): + eq_( None, ycm._GetRequiredNamespaceImport( {} ) ) + + +@YouCompleteMeInstance() +def GetRequiredNamespaceImport_ReturnNamespaceFromExtraData_test( ycm ): + namespace = 'A_NAMESPACE' + eq_( namespace, ycm._GetRequiredNamespaceImport( + BuildCompletion( namespace ) + ) ) + + +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfNotDone_test( ycm ): + with _SetupForCsharpCompletionDone( ycm, [] ): + ycm._latest_completion_request.Done = MagicMock( return_value = False ) + eq_( [], ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Te' ) ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_NewVim_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( None ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + eq_( [], ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnEmptyIfPendingMatches_OldVim_test( # noqa + ycm, *args ): + completions = [ BuildCompletion( None ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + with patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Te' ): + eq_( [], ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatches_NewVim_test( + ycm, *args ): + info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] + completions = [ BuildCompletion( *info ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + with patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): + eq_( completions, ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnMatchIfExactMatchesEvenIfPartial_NewVim_test( # noqa + ycm, *args ): + info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] + completions = [ BuildCompletion( *info ), + BuildCompletion( insertion_text = 'TestTest' ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + with patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): + eq_( [ completions[ 0 ] ], ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_DontReturnMatchIfNontExactMatchesAndPartial_NewVim_test( # noqa + ycm, *args ): + info = [ 'NS', 'Test', 'Abbr', 'Menu', 'Info', 'Kind' ] + completions = [ BuildCompletion( insertion_text = info[ 0 ] ), + BuildCompletion( insertion_text = 'TestTest' ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + with patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( *info[ 1: ] ) ): + eq_( [], ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True ) +@patch( 'ycm.vimsupport.GetVariableValue', + GetVariableValue_CompleteItemIs( 'Test' ) ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_NewVim_test( + ycm, *args ): + completions = [ BuildCompletion( None ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + eq_( completions, ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@YouCompleteMeInstance() +def GetCompletionsUserMayHaveCompleted_ReturnMatchIfMatches_OldVim_test( + ycm, *args ): + completions = [ BuildCompletion( None ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + eq_( completions, ycm.GetCompletionsUserMayHaveCompleted() ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@YouCompleteMeInstance() +def PostCompleteCsharp_EmptyDoesntInsertNamespace_test( ycm, *args ): + with _SetupForCsharpCompletionDone( ycm, [] ): + ycm._OnCompleteDone_Csharp() + ok_( not vimsupport.InsertNamespace.called ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@YouCompleteMeInstance() +def PostCompleteCsharp_ExistingWithoutNamespaceDoesntInsertNamespace_test( + ycm, *args ): + completions = [ BuildCompletion( None ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + ycm._OnCompleteDone_Csharp() + ok_( not vimsupport.InsertNamespace.called ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@YouCompleteMeInstance() +def PostCompleteCsharp_ValueDoesInsertNamespace_test( ycm, *args ): + namespace = 'A_NAMESPACE' + completions = [ BuildCompletion( namespace ) ] + with _SetupForCsharpCompletionDone( ycm, completions ): + ycm._OnCompleteDone_Csharp() + vimsupport.InsertNamespace.assert_called_once_with( namespace ) + + +@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False ) +@patch( 'ycm.vimsupport.PresentDialog', return_value = 1 ) +@YouCompleteMeInstance() +def PostCompleteCsharp_InsertSecondNamespaceIfSelected_test( ycm, *args ): + namespace = 'A_NAMESPACE' + namespace2 = 'ANOTHER_NAMESPACE' + completions = [ + BuildCompletion( namespace ), + BuildCompletion( namespace2 ), + ] + with _SetupForCsharpCompletionDone( ycm, completions ): + ycm._OnCompleteDone_Csharp() + vimsupport.InsertNamespace.assert_called_once_with( namespace2 ) diff --git a/python/ycm/tests/server_test.py b/python/ycm/tests/server_test.py deleted file mode 100644 index a1ddfce2..00000000 --- a/python/ycm/tests/server_test.py +++ /dev/null @@ -1,86 +0,0 @@ -# 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 . - -from __future__ import unicode_literals -from __future__ import print_function -from __future__ import division -from __future__ import absolute_import -from future import standard_library -standard_library.install_aliases() -from builtins import * # noqa - -from ycm.test_utils import MockVimModule -MockVimModule() - -import requests -import time - -from ycm.client.base_request import BaseRequest -from ycm.youcompleteme import YouCompleteMe -from ycmd import user_options_store - -# The default options which are only relevant to the client, not the server and -# thus are not part of default_options.json, but are required for a working -# YouCompleteMe or OmniCompleter object. -DEFAULT_CLIENT_OPTIONS = { - 'server_log_level': 'info', - 'extra_conf_vim_data': [], - 'show_diagnostics_ui': 1, - 'enable_diagnostic_signs': 1, - 'enable_diagnostic_highlighting': 0, - 'always_populate_location_list': 0, -} - - -def MakeUserOptions( custom_options = {} ): - options = dict( user_options_store.DefaultOptions() ) - options.update( DEFAULT_CLIENT_OPTIONS ) - options.update( custom_options ) - return options - - -class Server_test(): - - def _IsReady( self ): - return BaseRequest.GetDataFromHandler( 'ready' ) - - - def _WaitUntilReady( self, timeout = 5 ): - total_slept = 0 - while True: - try: - if total_slept > timeout: - raise RuntimeError( 'Waited for the server to be ready ' - 'for {0} seconds, aborting.'.format( - timeout ) ) - - if self._IsReady(): - return - except requests.exceptions.ConnectionError: - pass - finally: - time.sleep( 0.1 ) - total_slept += 0.1 - - - def setUp( self ): - self._server_state = YouCompleteMe( MakeUserOptions() ) - self._WaitUntilReady() - - - def tearDown( self ): - self._server_state.OnVimLeave() diff --git a/python/ycm/tests/youcompleteme_test.py b/python/ycm/tests/youcompleteme_test.py index afce5dfd..682c5d39 100644 --- a/python/ycm/tests/youcompleteme_test.py +++ b/python/ycm/tests/youcompleteme_test.py @@ -29,10 +29,9 @@ MockVimModule() import sys from hamcrest import assert_that, is_in, is_not -from ycm.tests.server_test import Server_test +from ycm.tests import YouCompleteMeInstance -class YouCompleteMe_test( Server_test ): - - def YcmCoreNotImported_test( self ): - assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) ) +@YouCompleteMeInstance() +def YouCompleteMe_YcmCoreNotImported_test( ycm ): + assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) )