From 01aa54ee2d72b44c2dedb0396e9c46f51e6fa96f Mon Sep 17 00:00:00 2001 From: micbou Date: Sat, 10 Dec 2016 23:36:27 +0100 Subject: [PATCH] Add tests --- python/ycm/tests/command_test.py | 44 ++++++++++++ python/ycm/tests/completion_test.py | 93 ++++++++++++++++++++++++-- python/ycm/tests/youcompleteme_test.py | 55 ++++++++++++++- 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100755 python/ycm/tests/command_test.py diff --git a/python/ycm/tests/command_test.py b/python/ycm/tests/command_test.py new file mode 100755 index 00000000..e894647b --- /dev/null +++ b/python/ycm/tests/command_test.py @@ -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 . + +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' ) + ) diff --git a/python/ycm/tests/completion_test.py b/python/ycm/tests/completion_test.py index 45b5ef51..f5fb958c 100644 --- a/python/ycm/tests/completion_test.py +++ b/python/ycm/tests/completion_test.py @@ -25,13 +25,15 @@ from future import standard_library standard_library.install_aliases() from builtins import * # noqa -from ycm.tests.test_utils import ( CurrentWorkingDirectory, MockVimModule, - MockVimBuffers, VimBuffer ) +from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock, + MockVimModule, MockVimBuffers, VimBuffer ) 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 ycmd.responses import ServerError @YouCompleteMeInstance() @@ -40,7 +42,7 @@ def CreateCompletionRequest_UnicodeWorkingDirectory_test( ycm ): current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) ) with CurrentWorkingDirectory( unicode_dir ): - with MockVimBuffers( [ current_buffer ], current_buffer, ( 5, 2 ) ): + with MockVimBuffers( [ current_buffer ], current_buffer ): ycm.CreateCompletionRequest(), results = ycm.GetCompletions() @@ -52,3 +54,86 @@ def CreateCompletionRequest_UnicodeWorkingDirectory_test( ycm ): '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' + } ) + ) diff --git a/python/ycm/tests/youcompleteme_test.py b/python/ycm/tests/youcompleteme_test.py index 763f39a2..be5d762a 100644 --- a/python/ycm/tests/youcompleteme_test.py +++ b/python/ycm/tests/youcompleteme_test.py @@ -30,10 +30,12 @@ MockVimModule() import os 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 ycm.tests import YouCompleteMeInstance +from ycmd.responses import ServerError @YouCompleteMeInstance() @@ -234,3 +236,54 @@ def YouCompleteMe_ToggleLogs_WithoutParameters_test( ycm, post_vim_message ): 'ycmd_\d+_stderr_.+.log\n' '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 ) + ] )