Auto merge of #2386 - micbou:refactor-tests, r=Valloric

[READY] Refactor tests using a YouCompleteMe instance

This PR adds a decorator function, similar to the `IsolatedYcmd` and `SharedYcmd` decorators from ycmd, that passes a YouCompleteMe object to tests. Its options can be customized with the parameter of the decorator. I need this to write new tests.

There is one thing annoying with this change is that Vim does not highlight a decorator function with no character between the parentheses so `@YouCompleteMeInstance()` will not be highlighted. I've contacted the maintainer of the Python syntax file about this bug. Anyway, that's not a good reason to not make this change.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/2386)
<!-- Reviewable:end -->
This commit is contained in:
Homu 2016-10-15 10:30:12 +09:00
commit 5cf5f04dd7
6 changed files with 1262 additions and 1284 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -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'
)
)

File diff suppressed because it is too large Load Diff

View File

@ -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 )

View File

@ -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 <http://www.gnu.org/licenses/>.
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()

View File

@ -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 ) ) )