Add tests for diagnostic commands

This commit is contained in:
micbou 2017-02-20 00:52:21 +01:00
parent 0d476a0164
commit 414782bc74
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
3 changed files with 168 additions and 5 deletions

View File

@ -267,10 +267,12 @@ class Response_Detection_test( object ):
# GoToResponse_QuickFix_test, so here we just check that the right call is
# made
with patch( 'ycm.vimsupport.SetQuickFixList' ) as set_qf_list:
with patch( 'ycm.vimsupport.OpenQuickFixList' ) as open_qf_list:
request = CommandRequest( [ command ] )
request._response = response
request.RunPostCommandActionsIfNeeded()
ok_( set_qf_list.called )
ok_( open_qf_list.called )
basic_goto = {
'filepath': 'test',

View File

@ -27,7 +27,8 @@ from builtins import * # noqa
from ycm.tests import PathToTestFile
from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
MockVimCommand, MockVimModule, VimBuffer )
MockVimBuffers, MockVimCommand,
MockVimModule, VimBuffer )
MockVimModule()
from ycm import vimsupport
@ -39,6 +40,47 @@ import os
import json
@patch( 'vim.eval', new_callable = ExtendedMock )
def SetLocationList_test( vim_eval ):
diagnostics = [ {
'bufnr': 3,
'filename': 'some_filename',
'lnum': 5,
'col': 22,
'type': 'E',
'valid': 1
} ]
vimsupport.SetLocationList( diagnostics )
vim_eval.assert_called_once_with(
'setloclist( 0, {0} )'.format( json.dumps( diagnostics ) ) )
@patch( 'ycm.vimsupport.VariableExists', return_value = True )
@patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
@patch( 'vim.command', new_callable = ExtendedMock )
def OpenLocationList_test( vim_command, fitting_height, variable_exists ):
vimsupport.OpenLocationList( focus = False, autoclose = True )
vim_command.assert_has_exact_calls( [
call( 'botright lopen' ),
call( 'au WinLeave <buffer> q' ),
call( 'doautocmd User YcmLocationOpened' ),
call( 'silent! wincmd p' )
] )
fitting_height.assert_called_once_with()
variable_exists.assert_called_once_with( '#User#YcmLocationOpened' )
@patch( 'ycm.vimsupport.GetIntValue', return_value = 120 )
@patch( 'vim.command' )
def SetFittingHeightForCurrentWindow_test( vim_command, *args ):
# Create a buffer with one line that is longer than the window width.
current_buffer = VimBuffer( 'buffer',
contents = [ 'a' * 140 ] )
with MockVimBuffers( [ current_buffer ], current_buffer ):
vimsupport.SetFittingHeightForCurrentWindow()
vim_command.assert_called_once_with( '2wincmd _' )
def AssertBuffersAreEqualAsBytes( result_buffer, expected_buffer ):
eq_( len( result_buffer ), len( expected_buffer ) )
for result_line, expected_line in zip( result_buffer, expected_buffer ):

View File

@ -295,3 +295,122 @@ def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test(
post_vim_message.assert_has_exact_calls( [
call( 'some_detailed_diagnostic', warning = False )
] )
@YouCompleteMeInstance()
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
def YouCompleteMe_ShowDiagnostics_FiletypeNotSupported_test( ycm,
post_vim_message ):
current_buffer = VimBuffer( 'buffer', filetype = 'not_supported' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.ShowDiagnostics()
post_vim_message.assert_called_once_with(
'Native filetype completion not supported for current file, '
'cannot force recompilation.', warning = False )
@YouCompleteMeInstance()
@patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
return_value = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
@patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
def YouCompleteMe_ShowDiagnostics_NoDiagnosticsDetected_test(
ycm, set_location_list, post_vim_message, *args ):
current_buffer = VimBuffer( 'buffer', filetype = 'cpp' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.event_notification.EventNotification.Response',
return_value = {} ):
ycm.ShowDiagnostics()
post_vim_message.assert_has_exact_calls( [
call( 'Forcing compilation, this will block Vim until done.',
warning = False ),
call( 'Diagnostics refreshed', warning = False ),
call( 'No warnings or errors detected.', warning = False )
] )
set_location_list.assert_called_once_with( [] )
@YouCompleteMeInstance( { 'log_level': 'debug',
'keep_logfiles': 1,
'open_loclist_on_ycm_diags': 0 } )
@patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
return_value = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
@patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList_test(
ycm, set_location_list, post_vim_message, *args ):
diagnostic = {
'kind': 'ERROR',
'text': 'error text',
'location': {
'filepath': 'buffer',
'column_num': 2,
'line_num': 19
}
}
current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.event_notification.EventNotification.Response',
return_value = [ diagnostic ] ):
ycm.ShowDiagnostics()
post_vim_message.assert_has_exact_calls( [
call( 'Forcing compilation, this will block Vim until done.',
warning = False ),
call( 'Diagnostics refreshed', warning = False )
] )
set_location_list.assert_called_once_with( [ {
'bufnr': 3,
'lnum': 19,
'col': 2,
'text': 'error text',
'type': 'E',
'valid': 1
} ] )
@YouCompleteMeInstance( { 'open_loclist_on_ycm_diags': 1 } )
@patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
return_value = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
@patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
@patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList_test(
ycm, open_location_list, set_location_list, post_vim_message, *args ):
diagnostic = {
'kind': 'ERROR',
'text': 'error text',
'location': {
'filepath': 'buffer',
'column_num': 2,
'line_num': 19
}
}
current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.event_notification.EventNotification.Response',
return_value = [ diagnostic ] ):
ycm.ShowDiagnostics()
post_vim_message.assert_has_exact_calls( [
call( 'Forcing compilation, this will block Vim until done.',
warning = False ),
call( 'Diagnostics refreshed', warning = False )
] )
set_location_list.assert_called_once_with( [ {
'bufnr': 3,
'lnum': 19,
'col': 2,
'text': 'error text',
'type': 'E',
'valid': 1
} ] )
open_location_list.assert_called_once_with( focus = True )