Add tests

This commit is contained in:
micbou 2016-12-10 23:36:27 +01:00
parent fd41d52dfe
commit 01aa54ee2d
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
3 changed files with 187 additions and 5 deletions

View File

@ -0,0 +1,44 @@
# 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.tests.test_utils import ( MockVimModule, MockVimBuffers, VimBuffer )
MockVimModule()
from hamcrest import assert_that, equal_to
from mock import patch
from ycm.tests import YouCompleteMeInstance
@YouCompleteMeInstance()
def SendCommandRequest_test( ycm ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.base_request.JsonFromFuture',
return_value = 'Some response' ):
assert_that(
ycm.SendCommandRequest( 'GoTo', 'python' ),
equal_to( 'Some response' )
)

View File

@ -25,13 +25,15 @@ from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
from builtins import * # noqa from builtins import * # noqa
from ycm.tests.test_utils import ( CurrentWorkingDirectory, MockVimModule, from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
MockVimBuffers, VimBuffer ) MockVimModule, MockVimBuffers, VimBuffer )
MockVimModule() MockVimModule()
from hamcrest import assert_that, empty, has_entries from hamcrest import assert_that, contains, empty, has_entries
from mock import call, patch
from ycm.tests import PathToTestFile, YouCompleteMeInstance from ycm.tests import PathToTestFile, YouCompleteMeInstance
from ycmd.responses import ServerError
@YouCompleteMeInstance() @YouCompleteMeInstance()
@ -40,7 +42,7 @@ def CreateCompletionRequest_UnicodeWorkingDirectory_test( ycm ):
current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) ) current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
with CurrentWorkingDirectory( unicode_dir ): with CurrentWorkingDirectory( unicode_dir ):
with MockVimBuffers( [ current_buffer ], current_buffer, ( 5, 2 ) ): with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.CreateCompletionRequest(), ycm.CreateCompletionRequest(),
results = ycm.GetCompletions() results = ycm.GetCompletions()
@ -52,3 +54,86 @@ def CreateCompletionRequest_UnicodeWorkingDirectory_test( ycm ):
'refresh': 'always' 'refresh': 'always'
} ) } )
) )
@YouCompleteMeInstance()
@patch( 'ycm.client.base_request._logger', autospec = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
def CreateCompletionRequest_ResponseContainingError_test( ycm,
post_vim_message,
logger ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.CreateCompletionRequest(),
response = {
'completions': [ {
'insertion_text': 'insertion_text',
'menu_text': 'menu_text',
'extra_menu_info': 'extra_menu_info',
'detailed_info': 'detailed_info',
'kind': 'kind',
'extra_data': {
'doc_string': 'doc_string'
}
} ],
'completion_start_column': 3,
'errors': [ {
'exception': {
'TYPE': 'Exception'
},
'message': 'message',
'traceback': 'traceback'
} ]
}
with patch( 'ycm.client.completion_request.JsonFromFuture',
return_value = response ):
results = ycm.GetCompletions()
logger.exception.assert_called_with( 'Error while handling server response' )
post_vim_message.assert_has_exact_calls( [
call( 'Exception: message', truncate = True )
] )
assert_that(
results,
has_entries( {
'words': contains( has_entries( {
'word': 'insertion_text',
'abbr': 'menu_text',
'menu': 'extra_menu_info',
'info': 'detailed_info\ndoc_string',
'kind': 'k',
'dup': 1,
'empty': 1
} ) ),
'refresh': 'always'
} )
)
@YouCompleteMeInstance()
@patch( 'ycm.client.base_request._logger', autospec = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
def CreateCompletionRequest_ErrorFromServer_test( ycm,
post_vim_message,
logger ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.CreateCompletionRequest(),
with patch( 'ycm.client.completion_request.JsonFromFuture',
side_effect = ServerError( 'Server error' ) ):
results = ycm.GetCompletions()
logger.exception.assert_called_with( 'Error while handling server response' )
post_vim_message.assert_has_exact_calls( [
call( 'Server error', truncate = True )
] )
assert_that(
results,
has_entries( {
'words': empty(),
'refresh': 'always'
} )
)

View File

@ -30,10 +30,12 @@ MockVimModule()
import os import os
import sys import sys
from hamcrest import assert_that, is_in, is_not, has_length, matches_regexp from hamcrest import ( assert_that, contains, empty, is_in, is_not, has_length,
matches_regexp )
from mock import call, MagicMock, patch from mock import call, MagicMock, patch
from ycm.tests import YouCompleteMeInstance from ycm.tests import YouCompleteMeInstance
from ycmd.responses import ServerError
@YouCompleteMeInstance() @YouCompleteMeInstance()
@ -234,3 +236,54 @@ def YouCompleteMe_ToggleLogs_WithoutParameters_test( ycm, post_vim_message ):
'ycmd_\d+_stderr_.+.log\n' 'ycmd_\d+_stderr_.+.log\n'
'ycmd_\d+_stdout_.+.log' ) 'ycmd_\d+_stdout_.+.log' )
) )
@YouCompleteMeInstance()
def YouCompleteMe_GetDefinedSubcommands_ListFromServer_test( ycm ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.base_request.JsonFromFuture',
return_value = [ 'SomeCommand', 'AnotherCommand' ] ):
assert_that(
ycm.GetDefinedSubcommands(),
contains(
'SomeCommand',
'AnotherCommand'
)
)
@YouCompleteMeInstance()
@patch( 'ycm.client.base_request._logger', autospec = True )
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
def YouCompleteMe_GetDefinedSubcommands_ErrorFromServer_test( ycm,
post_vim_message,
logger ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.base_request.JsonFromFuture',
side_effect = ServerError( 'Server error' ) ):
result = ycm.GetDefinedSubcommands()
logger.exception.assert_called_with( 'Error while handling server response' )
post_vim_message.assert_has_exact_calls( [
call( 'Server error', truncate = False )
] )
assert_that( result, empty() )
@YouCompleteMeInstance()
@patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test(
ycm, post_vim_message ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
with patch( 'ycm.client.base_request.JsonFromFuture',
return_value = { 'message': 'some_detailed_diagnostic' } ):
ycm.ShowDetailedDiagnostic(),
post_vim_message.assert_has_exact_calls( [
call( 'some_detailed_diagnostic', warning = False )
] )