def EventNotification_BufferVisit_BuildRequestForCurrentAndUnsavedBuffers_test( ycm, *args): 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) 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) 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=False) with patch('icm.client.event_notification.EventNotification.' 'PostDataToHandlerAsync') as post_data_to_handler_async: with MockVimBuffers( [current_buffer, modified_buffer, unmodified_buffer], current_buffer, (3, 5)): ycm.OnBufferVisit() assert_that( # Positional arguments passed to PostDataToHandlerAsync. post_data_to_handler_async.call_args[0], contains( has_entries({ 'filepath': current_buffer_file, 'line_num': 3, 'column_num': 6, 'file_data': has_entries({ current_buffer_file: has_entries({ 'contents': 'current_buffer_contents\n', 'filetypes': ['some_filetype'] }), modified_buffer_file: has_entries({ 'contents': 'modified_buffer_contents\n', 'filetypes': ['some_filetype'] }) }), 'event_name': 'BufferVisit' }), 'event_notification'))
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) 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('icm.client.event_notification.EventNotification.' 'PostDataToHandlerAsync') as post_data_to_handler_async: with MockVimBuffers([current_buffer, deleted_buffer], current_buffer): 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_contents\n', 'filetypes': ['some_filetype'] }), deleted_buffer_file: has_entries({ 'contents': 'deleted_buffer_contents\n', 'filetypes': ['some_filetype'] }) }), 'event_name': 'BufferUnload' }), 'event_notification'))
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', 'line_num': 19, 'column_num': 2 } } current_buffer = VimBuffer('buffer', filetype='cpp', number=3) with MockVimBuffers([current_buffer], current_buffer): with patch('icm.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)
def YouCompleteMe_GetDefinedSubcommands_ListFromServer_test(ycm): current_buffer = VimBuffer('buffer') with MockVimBuffers([current_buffer], current_buffer): with patch('icm.client.base_request.JsonFromFuture', return_value=['SomeCommand', 'AnotherCommand']): assert_that(ycm.GetDefinedSubcommands(), contains('SomeCommand', 'AnotherCommand'))
def YouCompleteMe_DebugInfo_ServerRunning_test(ycm): dir_of_script = os.path.dirname(os.path.abspath(__file__)) buf_name = os.path.join(dir_of_script, 'testdata', 'test.cpp') extra_conf = os.path.join(dir_of_script, 'testdata', '.ycm_extra_conf.py') _LoadExtraConfFile(extra_conf) current_buffer = VimBuffer(buf_name, filetype='cpp') with MockVimBuffers([current_buffer], current_buffer): assert_that( ycm.DebugInfo(), matches_regexp( 'Client logfile: .+\n' 'Server Python interpreter: .+\n' 'Server Python version: .+\n' 'Server has Clang support compiled in: ' '(?P<CLANG>True)?(?(CLANG)|False)\n' 'Clang version: .+\n' 'Extra configuration file found and loaded\n' 'Extra configuration path: .*testdata[/\\\\]\\.ycm_extra_conf\\.py\n' '(?(CLANG)C-family completer debug information:\n' ' Compilation database path: None\n' ' Flags: \\[\'_TEMP_FILE_\'.*\\]\n)' 'Server running at: .+\n' 'Server process ID: \d+\n' 'Server logfiles:\n' ' .+\n' ' .+'))
def MockArbitraryBuffer(filetype): """Used via the with statement, set up a single buffer with an arbitrary name and no contents. Its filetype is set to the supplied filetype.""" # Arbitrary, but valid, single buffer open. current_buffer = VimBuffer(os.path.realpath('TEST_BUFFER'), window=1, filetype=filetype) with MockVimBuffers([current_buffer], current_buffer): yield
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)
def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test( ycm, post_vim_message): current_buffer = VimBuffer('buffer') with MockVimBuffers([current_buffer], current_buffer): with patch('icm.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)])
def CreateCompletionRequest_UnicodeWorkingDirectory_test(ycm): unicode_dir = PathToTestFile('uni¢𐍈d€') current_buffer = VimBuffer(PathToTestFile('uni¢𐍈d€', 'current_buffer')) with CurrentWorkingDirectory(unicode_dir): with MockVimBuffers([current_buffer], current_buffer): ycm.CreateCompletionRequest(), results = ycm.GetCompletions() assert_that(results, has_entries({'words': empty(), 'refresh': 'always'}))
def YouCompleteMe_GetDefinedSubcommands_ErrorFromServer_test( ycm, post_vim_message, logger): current_buffer = VimBuffer('buffer') with MockVimBuffers([current_buffer], current_buffer): with patch('icm.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())
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('icm.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' }))
def YouCompleteMe_DebugInfo_ServerNotRunning_test(ycm): StopServer(ycm) current_buffer = VimBuffer('current_buffer') with MockVimBuffers([current_buffer], current_buffer): assert_that( ycm.DebugInfo(), matches_regexp('Client logfile: .+\n' 'Server errored, no debug info from server\n' 'Server running at: .+\n' 'Server process ID: \d+\n' 'Server logfiles:\n' ' .+\n' ' .+'))
def SendCommandRequest_test(ycm): current_buffer = VimBuffer('buffer') with MockVimBuffers([current_buffer], current_buffer): with patch('icm.youcompleteme.SendCommandRequest') as send_request: ycm.SendCommandRequest(['GoTo'], 'python') send_request.assert_called_once_with( ['GoTo'], 'python', {'extra_conf_data': { 'tempname()': '_TEMP_FILE_' }}) with patch('icm.client.base_request.JsonFromFuture', return_value='Some response'): assert_that(ycm.SendCommandRequest(['GoTo'], 'python'), equal_to('Some response'))
def CreateCompletionRequest_ErrorFromServer_test(ycm, post_vim_message, logger): current_buffer = VimBuffer('buffer') with MockVimBuffers([current_buffer], current_buffer): ycm.CreateCompletionRequest(), with patch('icm.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'}))
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('icm.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([])
def YouCompleteMe_ToggleLogs_WithoutParameters_test(ycm, post_vim_message): if not OnMac(): return # We test on a Python buffer because the Python completer has subserver # logfiles. python_buffer = VimBuffer('buffer.py', filetype='swift') with MockVimBuffers([python_buffer], python_buffer): ycm.ToggleLogs() assert_that( # Argument passed to PostVimMessage. post_vim_message.call_args[0][0], matches_regexp('Available logfiles are:\n' 'swiftyswift_http_\d+_stderr_.+.log\n' 'swiftyswift_http_\d+_stdout_.+.log\n' 'ycm_.+.log\n' 'ycmd_\d+_stderr_.+.log\n' 'ycmd_\d+_stdout_.+.log'))
def YouCompleteMe_ToggleLogs_WithParameters_test(ycm, open_filename, close_buffers_for_filename): logfile_buffer = VimBuffer(ycm._client_logfile, window=1) with MockVimBuffers([logfile_buffer], logfile_buffer): ycm.ToggleLogs(os.path.basename(ycm._client_logfile), 'nonexisting_logfile', os.path.basename(ycm._server_stdout)) open_filename.assert_has_exact_calls([ call( ycm._server_stdout, { 'size': 12, 'watch': True, 'fix': True, 'focus': False, 'position': 'end' }) ]) close_buffers_for_filename.assert_has_exact_calls( [call(ycm._client_logfile)])
def EventNotification_FileReadyToParse_SyntaxKeywords_SeedWithCache_test( ycm, *args): current_buffer = VimBuffer(name='current_buffer', filetype='some_filetype') with patch('icm.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'))
def EventNotification_FileReadyToParse_TagFiles_UnicodeWorkingDirectory_test( ycm, *args): unicode_dir = PathToTestFile('uni¢𐍈d€') current_buffer_file = PathToTestFile('uni¢𐍈d€', 'current_buffer') current_buffer = VimBuffer(name=current_buffer_file, contents=['current_buffer_contents'], filetype='some_filetype') with patch('icm.client.event_notification.EventNotification.' 'PostDataToHandlerAsync') as post_data_to_handler_async: with CurrentWorkingDirectory(unicode_dir): with MockVimBuffers([current_buffer], current_buffer, (6, 5)): ycm.OnFileReadyToParse() assert_that( # Positional arguments passed to PostDataToHandlerAsync. post_data_to_handler_async.call_args[0], contains( has_entries({ 'filepath': current_buffer_file, 'line_num': 6, 'column_num': 6, 'file_data': has_entries({ current_buffer_file: has_entries({ 'contents': 'current_buffer_contents\n', 'filetypes': ['some_filetype'] }) }), 'event_name': 'FileReadyToParse', 'tag_files': has_item(PathToTestFile('uni¢𐍈d€', 'tags')) }), 'event_notification'))
def EventNotification_FileReadyToParse_SyntaxKeywords_ClearCacheIfRestart_test( ycm, *args): current_buffer = VimBuffer(name='current_buffer', filetype='some_filetype') with patch('icm.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'))
def YouCompleteMe_UpdateDiagnosticInterface_PrioritizeErrorsOverWarnings_test( ycm, vim_command, post_vim_message, *args): contents = """int main() { int x, y; x == y }""" # List of diagnostics returned by ycmd for the above code. diagnostics = [ { 'kind': 'ERROR', 'text': "expected ';' after expression", 'location': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 9 }, # Looks strange but this is really what ycmd is returning. 'location_extent': { 'start': { 'filepath': '', 'line_num': 0, 'column_num': 0, }, 'end': { 'filepath': '', 'line_num': 0, 'column_num': 0, } }, 'ranges': [], 'fixit_available': True }, { 'kind': 'WARNING', 'text': 'equality comparison result unused', 'location': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 7, }, 'location_extent': { 'start': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 5, }, 'end': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 7, } }, 'ranges': [{ 'start': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 3, }, 'end': { 'filepath': 'buffer', 'line_num': 3, 'column_num': 9, } }], 'fixit_available': True } ] current_buffer = VimBuffer('buffer', filetype='c', contents=contents.splitlines(), number=5, window=2) test_utils.VIM_MATCHES = [] with MockVimBuffers([current_buffer], current_buffer, (3, 1)): with patch('icm.client.event_notification.EventNotification.Response', return_value=diagnostics): ycm.OnFileReadyToParse() ycm.HandleFileParseRequest(block=True) # Error match is added after warning matches. assert_that( test_utils.VIM_MATCHES, contains(VimMatch('IcmWarningSection', '\%3l\%5c\_.\{-}\%3l\%7c'), VimMatch('IcmWarningSection', '\%3l\%3c\_.\{-}\%3l\%9c'), VimMatch('IcmErrorSection', '\%3l\%8c'))) # Only the error sign is placed. vim_command.assert_has_exact_calls([ call('sign define icm_dummy_sign'), call('sign place 3 name=icm_dummy_sign line=3 buffer=5'), call('sign place 1 name=IcmError line=3 buffer=5'), call('sign undefine icm_dummy_sign'), call('sign unplace 3 buffer=5') ]) # When moving the cursor on the diagnostics, the error is displayed to the # user, not the warning. ycm.OnCursorMoved() post_vim_message.assert_has_exact_calls([ call("expected ';' after expression (FixIt)", truncate=True, warning=False) ])