Add syntax keywords tests

Patch EventNotification instead of BaseRequest in event notification tests.
This commit is contained in:
micbou 2017-05-15 12:41:45 +02:00
parent 3ac2951c7b
commit 26ac0c8730
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
2 changed files with 80 additions and 8 deletions

View File

@ -65,7 +65,7 @@ def _IsReady():
return BaseRequest.GetDataFromHandler( 'ready' ) return BaseRequest.GetDataFromHandler( 'ready' )
def _WaitUntilReady( timeout = 5 ): def WaitUntilReady( timeout = 5 ):
expiration = time.time() + timeout expiration = time.time() + timeout
while True: while True:
try: try:
@ -109,7 +109,7 @@ def YouCompleteMeInstance( custom_options = {} ):
@functools.wraps( test ) @functools.wraps( test )
def Wrapper( *args, **kwargs ): def Wrapper( *args, **kwargs ):
ycm = YouCompleteMe( _MakeUserOptions( custom_options ) ) ycm = YouCompleteMe( _MakeUserOptions( custom_options ) )
_WaitUntilReady() WaitUntilReady()
try: try:
test( ycm, *args, **kwargs ) test( ycm, *args, **kwargs )
finally: finally:

View File

@ -31,11 +31,12 @@ MockVimModule()
import contextlib import contextlib
import os import os
from ycm.tests import PathToTestFile, YouCompleteMeInstance from ycm.tests import PathToTestFile, YouCompleteMeInstance, WaitUntilReady
from ycmd.responses import ( BuildDiagnosticData, Diagnostic, Location, Range, from ycmd.responses import ( BuildDiagnosticData, Diagnostic, Location, Range,
UnknownExtraConf, ServerError ) UnknownExtraConf, ServerError )
from hamcrest import assert_that, contains, has_entries, has_item from hamcrest import ( assert_that, contains, has_entries, has_entry, has_item,
has_items, has_key, is_not )
from mock import call, MagicMock, patch from mock import call, MagicMock, patch
from nose.tools import eq_, ok_ from nose.tools import eq_, ok_
@ -81,7 +82,8 @@ def MockEventNotification( response_method, native_filetype_completer = True ):
# We don't want the event to actually be sent to the server, just have it # We don't want the event to actually be sent to the server, just have it
# return success # return success
with patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync', with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync',
return_value = MagicMock( return_value=True ) ): return_value = MagicMock( return_value=True ) ):
# We set up a fake a Response (as called by EventNotification.Response) # We set up a fake a Response (as called by EventNotification.Response)
@ -358,7 +360,7 @@ def EventNotification_FileReadyToParse_TagFiles_UnicodeWorkingDirectory_test(
contents = [ 'current_buffer_contents' ], contents = [ 'current_buffer_contents' ],
filetype = 'some_filetype' ) filetype = 'some_filetype' )
with patch( 'ycm.client.base_request.BaseRequest.' with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync' ) as post_data_to_handler_async: 'PostDataToHandlerAsync' ) as post_data_to_handler_async:
with CurrentWorkingDirectory( unicode_dir ): with CurrentWorkingDirectory( unicode_dir ):
with MockVimBuffers( [ current_buffer ], current_buffer, ( 6, 5 ) ): with MockVimBuffers( [ current_buffer ], current_buffer, ( 6, 5 ) ):
@ -412,7 +414,7 @@ def EventNotification_BufferVisit_BuildRequestForCurrentAndUnsavedBuffers_test(
filetype = 'some_filetype', filetype = 'some_filetype',
modified = False ) modified = False )
with patch( 'ycm.client.base_request.BaseRequest.' with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync' ) as post_data_to_handler_async: 'PostDataToHandlerAsync' ) as post_data_to_handler_async:
with MockVimBuffers( [ current_buffer, modified_buffer, unmodified_buffer ], with MockVimBuffers( [ current_buffer, modified_buffer, unmodified_buffer ],
current_buffer, current_buffer,
@ -461,7 +463,7 @@ def EventNotification_BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test(
filetype = 'some_filetype', filetype = 'some_filetype',
modified = False ) modified = False )
with patch( 'ycm.client.base_request.BaseRequest.' with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync' ) as post_data_to_handler_async: 'PostDataToHandlerAsync' ) as post_data_to_handler_async:
with MockVimBuffers( [ current_buffer, deleted_buffer ], current_buffer ): with MockVimBuffers( [ current_buffer, deleted_buffer ], current_buffer ):
ycm.OnBufferUnload( deleted_buffer_file ) ycm.OnBufferUnload( deleted_buffer_file )
@ -489,3 +491,73 @@ def EventNotification_BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test(
'event_notification' 'event_notification'
) )
) )
@patch( 'ycm.syntax_parse.SyntaxKeywordsForCurrentBuffer',
return_value = [ 'foo', 'bar' ] )
@YouCompleteMeInstance( { 'seed_identifiers_with_syntax': 1 } )
def EventNotification_FileReadyToParse_SyntaxKeywords_SeedWithCache_test(
ycm, *args ):
current_buffer = VimBuffer( name = 'current_buffer',
filetype = 'some_filetype' )
with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync' ) as post_data_to_handler_async:
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.OnFileReadyToParse()
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
has_entry( 'syntax_keywords', has_items( 'foo', 'bar' ) ),
'event_notification'
)
)
# Do not send again syntax keywords in subsequent requests.
ycm.OnFileReadyToParse()
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
is_not( has_key( 'syntax_keywords' ) ),
'event_notification'
)
)
@patch( 'ycm.syntax_parse.SyntaxKeywordsForCurrentBuffer',
return_value = [ 'foo', 'bar' ] )
@YouCompleteMeInstance( { 'seed_identifiers_with_syntax': 1 } )
def EventNotification_FileReadyToParse_SyntaxKeywords_ClearCacheIfRestart_test(
ycm, *args ):
current_buffer = VimBuffer( name = 'current_buffer',
filetype = 'some_filetype' )
with patch( 'ycm.client.event_notification.EventNotification.'
'PostDataToHandlerAsync' ) as post_data_to_handler_async:
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.OnFileReadyToParse()
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
has_entry( 'syntax_keywords', has_items( 'foo', 'bar' ) ),
'event_notification'
)
)
# Send again the syntax keywords after restarting the server.
ycm.RestartServer()
WaitUntilReady()
ycm.OnFileReadyToParse()
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
has_entry( 'syntax_keywords', has_items( 'foo', 'bar' ) ),
'event_notification'
)
)