def Subcommands_RefactorRename_NotPossible_test(app): filepath = PathToTestFile('test.ts') contents = ReadFile(filepath) event_data = BuildRequest(filepath=filepath, filetype='typescript', contents=contents, event_name='BufferVisit') app.post_json('/event_notification', event_data) request = BuildRequest(completer_target='filetype_default', command_arguments=['RefactorRename', 'whatever'], line_num=35, column_num=5, contents=contents, filetype='typescript', filepath=filepath) response = app.post_json('/run_completer_command', request, expect_errors=True).json assert_that( response, ErrorMatcher( RuntimeError, 'Value cannot be renamed: ' 'You cannot rename this element.'))
def Subcommands_GetDoc_test(): tests = [ # from local file [{ 'line_num': 5, 'column_num': 10 }, 'docstring', requests.codes.ok], # from header [{ 'line_num': 6, 'column_num': 10 }, 'docstring', requests.codes.ok], # no docstring [{ 'line_num': 7, 'column_num': 7 }, 'int x = 3', requests.codes.ok], # no hover [{ 'line_num': 8, 'column_num': 1 }, ErrorMatcher(RuntimeError, 'No hover information.'), requests.codes.server_error] ] for subcommand in ['GetDoc', 'GetDocImprecise']: for test in tests: yield (RunGetSemanticTest, PathToTestFile('GetDoc_Clang_test.cc'), 'cpp', test, [subcommand], test[2])
def GetCompletions_Fallback_Exception_test(app): # TESTCASE4 (general_fallback/lang_c.c) # extra conf throws exception RunTest( app, { 'description': '. on struct returns identifier because of error', 'request': { 'filetype': 'c', 'filepath': PathToTestFile('general_fallback', 'lang_c.c'), 'line_num': 62, 'column_num': 20, 'extra_conf_data': { '&filetype': 'c', 'throw': 'testy' }, 'force_semantic': False, }, 'expect': { 'response': http.client.OK, 'data': has_entries({ 'completions': contains( CompletionEntryMatcher('a_parameter', '[ID]'), CompletionEntryMatcher('another_parameter', '[ID]'), ), 'errors': has_item(ErrorMatcher(ValueError, 'testy')) }) }, })
def Subcommands_RefactorRename_Missing_test(app): filepath = PathToTestFile('test.ts') contents = ReadFile(filepath) event_data = BuildRequest(filepath=filepath, filetype='typescript', contents=contents, event_name='BufferVisit') app.post_json('/event_notification', event_data) request = BuildRequest(completer_target='filetype_default', command_arguments=['RefactorRename'], line_num=30, column_num=6, contents=contents, filetype='typescript', filepath=filepath) response = app.post_json('/run_completer_command', request, expect_errors=True).json assert_that( response, ErrorMatcher( ValueError, 'Please specify a new name to rename it to.\n' 'Usage: RefactorRename <new name>'))
def Subcommands_GoToSymbol_test(app, req, rep): if isinstance(rep, tuple): expect = { 'response': requests.codes.ok, 'data': LocationMatcher(PathToTestFile(rep[0]), *rep[1:]) } elif isinstance(rep, list): expect = { 'response': requests.codes.ok, 'data': contains_inanyorder( *[LocationMatcher(PathToTestFile(r[0]), *r[1:]) for r in rep]) } else: expect = { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher(RuntimeError, rep) } RunTest( app, { 'request': { 'command': 'GoToSymbol', 'arguments': [req[3]], 'line_num': req[1], 'column_num': req[2], 'filepath': PathToTestFile(req[0]), }, 'expect': expect })
def GetCompletions_PythonInterpreter_InvalidPythonInExtraConf_test(app): RunTest( app, { 'description': 'Python interpreter path specified in extra conf file ' 'does not exist', 'request': { 'filetype': 'python', 'filepath': PathToTestFile('project', '__main__.py'), 'line_num': 3, 'column_num': 8 }, 'expect': { 'response': requests.codes.ok, 'data': has_entries({ 'completions': empty(), 'errors': contains( ErrorMatcher( RuntimeError, 'Cannot find Python interpreter path ' '/non/existing/python.')) }) } })
def GetCompletions_NonExistingRustSrcPathFromEnvironmentVariable_test(app): response = app.get('/ready', { 'subserver': 'rust' }, expect_errors=True).json assert_that( response, ErrorMatcher(RuntimeError, NON_EXISTING_RUST_SOURCES_PATH_MESSAGE))
def Subcommands_GoTo( app, test, command ): if isinstance( test[ 'response' ], tuple ): if not isinstance( test[ 'response' ][ 0 ], StringMatchesPattern ): expect = { 'response': requests.codes.ok, 'data': LocationMatcher( PathToTestFile( 'goto', test[ 'response' ][ 0 ] ), test[ 'response' ][ 1 ], test[ 'response' ][ 2 ] ) } else: expect = { 'response': requests.codes.ok, 'data': LocationMatcher( test[ 'response' ][ 0 ], test[ 'response' ][ 1 ], test[ 'response' ][ 2 ] ) } else: expect = { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, test[ 'response' ] ) } RunTest( app, { 'description': command + ' jumps to the right location', 'request': { 'command' : command, 'filetype' : 'python', 'filepath' : PathToTestFile( 'goto', test[ 'request' ][ 0 ] ), 'line_num' : test[ 'request' ][ 1 ], 'column_num': test[ 'request' ][ 2 ] }, 'expect': expect, } )
def Subcommands_GoToSymbol_test( app, req, rep ): if isinstance( rep, tuple ): expect = { 'response': requests.codes.ok, 'data': LocationMatcher( PathToTestFile( rep[ 0 ] ), *rep[ 1: ] ) } elif isinstance( rep, list ): # NOTE: We use has_items here because tsserver will include results from # node_modules and all sorts of other random places. expect = { 'response': requests.codes.ok, 'data': has_items( *[ LocationMatcher( PathToTestFile( r[ 0 ] ), *r[ 1: ] ) for r in rep ] ) } else: expect = { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, rep ) } RunTest( app, { 'request': { 'command': 'GoToSymbol', 'arguments': [ req[ 3 ] ], 'line_num': req[ 1 ], 'column_num': req[ 2 ], 'filepath': PathToTestFile( req[ 0 ] ), }, 'expect': expect } )
def Subcommands_GetDocQuick_NoCursor_test(app): filepath = PathToTestFile('GetDoc_Clang.cc') contents = ReadFile(filepath) app.post_json( '/event_notification', BuildRequest(filepath=filepath, filetype='cpp', compilation_flags=['-x', 'c++'], contents=contents, event_name='FileReadyToParse')) event_data = BuildRequest(filepath=filepath, filetype='cpp', compilation_flags=['-x', 'c++'], line_num=1, column_num=1, contents=contents, command_arguments=['GetDocQuick'], completer_target='filetype_default') response = app.post_json('/run_completer_command', event_data, expect_errors=True) eq_(response.status_code, requests.codes.internal_server_error) assert_that(response.json, ErrorMatcher(ValueError, NO_DOCUMENTATION_MESSAGE))
def GetCompletions_NonExistingRustSrcPathFromUserOption_test(app): with UserOption('rust_src_path', '/non/existing/rust/src/path'): response = app.get('/ready', { 'subserver': 'rust' }, expect_errors=True).json assert_that( response, ErrorMatcher(RuntimeError, NON_EXISTING_RUST_SOURCES_PATH_MESSAGE))
def UserDefinedPython_WhenNonExistentPythonIsGiven_ReturnAnError_test( app, *args): response = app.get('/ready', { 'subserver': 'python' }, expect_errors=True).json msg = BINARY_NOT_FOUND_MESSAGE.format('/non/existing/path/python') assert_that(response, ErrorMatcher(RuntimeError, msg)) utils.SafePopen.assert_not_called()
def ShouldEnableJavaCompleter_JavaNotFound_test(app): request_data = BuildRequest(filetype='java') response = app.post_json('/defined_subcommands', request_data, expect_errors=True) assert_that(response.status_code, equal_to(requests.codes.internal_server_error)) assert_that( response.json, ErrorMatcher(ValueError, 'No semantic completer exists for filetypes: ' "['java']"))
def UserDefinedPython_RestartServerWithNonExistingPythonArgument_test( app, *args): python = '/non/existing/python' request = BuildRequest(filetype='python', command_arguments=['RestartServer', python]) response = app.post_json('/run_completer_command', request, expect_errors=True).json msg = BINARY_NOT_FOUND_MESSAGE.format(python) assert_that(response, ErrorMatcher(RuntimeError, msg)) assert_that(utils.SafePopen, was_called_with_python(sys.executable))
def Diagnostics_DoesntWork_test( app ): with PatchCompleter( DummyCompleter, filetype = 'dummy_filetype' ): diag_data = BuildRequest( contents = "foo = 5", line_num = 2, filetype = 'dummy_filetype' ) response = app.post_json( '/detailed_diagnostic', diag_data, expect_errors = True ) eq_( response.status_code, requests.codes.internal_server_error ) assert_that( response.json, ErrorMatcher( NoDiagnosticSupport ) )
def Subcommands_GoToDefinition_NotFound_test(app): filepath = PathToTestFile('goto_file5.py') goto_data = BuildRequest(command_arguments=['GoToDefinition'], line_num=4, contents=ReadFile(filepath), filetype='python', filepath=filepath) response = app.post_json('/run_completer_command', goto_data, expect_errors=True).json assert_that(response, ErrorMatcher(RuntimeError, "Can\'t jump to definition."))
def Subcommands_GoTo_Fail( app, goto_command ): RunTest( app, { 'description': goto_command + ' fails on non-existing method', 'request': { 'command': goto_command, 'line_num': 35, 'column_num': 6, 'filepath': PathToTestFile( 'test.ts' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'Could not find definition.' ) } } )
def Subcommands_GoToImplementation_InvalidLocation_test( app ): RunTest( app, { 'description': 'GoToImplementation on an invalid location raises exception', 'request': { 'command': 'GoToImplementation', 'line_num': 1, 'column_num': 1, 'filepath': PathToTestFile( 'signatures.ts' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'No implementation found.' ) } } )
def Subcommands_GetType_HasNoType_test( app ): RunTest( app, { 'description': 'GetType returns an error on a keyword', 'request': { 'command': 'GetType', 'line_num': 32, 'column_num': 1, 'filepath': PathToTestFile( 'test.ts' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'No content available.' ) } } )
def Subcommands_GetType_UnknownType_test( app ): RunTest( app, { 'description': 'GetType on a unknown type raises an error', 'request': { 'command': 'GetType', 'line_num': 2, 'column_num': 4, 'filepath': PathToTestFile( 'td', 'test.go' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'Unknown type.' ) } } )
def Subcommands_GoTo_Keyword( app, goto_command ): RunTest( app, { 'description': goto_command + ' can\'t jump on keyword', 'request': { 'command': goto_command, 'line_num': 3, 'column_num': 3, 'filepath': PathToTestFile( 'goto.go' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'Can\'t find a definition.' ) } } )
def test_Subcommands_GetDoc_UnknownType( self, app ): RunTest( app, { 'description': 'GetDoc on a unknown type raises an error', 'request': { 'command': 'GetDoc', 'line_num': 2, 'column_num': 4, 'filepath': PathToTestFile( 'td', 'test.go' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'No documentation available.' ) } } )
def Subcommands_GoToType_Fail_test( app ): RunTest( app, { 'description': 'GoToType fails outside the buffer', 'request': { 'command': 'GoToType', 'line_num': 39, 'column_num': 8, 'filepath': PathToTestFile( 'test.ts' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, 'Could not find type definition.' ) } } )
def RunGoToTest_all(app, folder, command, test): filepath = PathToTestFile(folder, test['req'][0]) common_request = { 'completer_target': 'filetype_default', 'filepath': filepath, 'command_arguments': [command], 'contents': ReadFile(filepath), 'filetype': 'cpp' } request = common_request request.update({ 'line_num': test['req'][1], 'column_num': test['req'][2], }) response = test['res'] if isinstance(response, list): expect = { 'response': requests.codes.ok, 'data': contains(*[ LocationMatcher( PathToTestFile(folder, os.path.normpath(location[0])), location[1], location[2]) for location in response ]) } elif isinstance(response, tuple): expect = { 'response': requests.codes.ok, 'data': LocationMatcher( PathToTestFile(folder, os.path.normpath(response[0])), response[1], response[2]) } else: expect = { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher(RuntimeError, test['res']) } RunAfterInitialized(app, { 'request': request, 'route': '/run_completer_command', 'expect': expect })
def Subcommands_GetDocWithNoItendifier_test( app ): RunTest( app, { 'description': 'GetDoc works when no identifier', 'request': { 'command': 'GetDoc', 'filepath': PathToTestFile( 'simple_test.js' ), 'line_num': 12, 'column_num': 1, }, 'expect': { 'response': http.client.INTERNAL_SERVER_ERROR, 'data': ErrorMatcher( RuntimeError, 'TernError: No type found ' 'at the given position.' ), } } )
def MiscHandlers_SignatureHelp_ComputeSignatureThrows_test( app, *args ): with PatchCompleter( DummyCompleter, filetype = 'dummy_filetype' ): request_data = BuildRequest( filetype = 'dummy_filetype' ) response = app.post_json( '/signature_help', request_data ).json print( response ) assert_that( response, has_entries( { 'signature_help': has_entries( { 'activeSignature': 0, 'activeParameter': 0, 'signatures': empty() } ), 'errors': contains( ErrorMatcher( RuntimeError, '' ) ) } ) )
def Subcommands_FixIt_ParseError_test( app ): RunTest( app, { 'description': 'Parse error leads to ResponseFailedException', 'request': { 'command': 'FixIt', 'line_num': 1, 'column_num': 1, 'filepath': PathToTestFile( 'unicode', 'unicode.go' ), }, 'expect': { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( ResponseFailedException, matches_regexp( '^Request failed: \\d' ) ) } } )
def RunGoToTest_all( app, folder, command, test ): req = test[ 'req' ] filepath = PathToTestFile( folder, req[ 0 ] ) request = { 'completer_target' : 'filetype_default', 'filepath' : filepath, 'contents' : ReadFile( filepath ), 'filetype' : 'cpp', 'line_num' : req[ 1 ], 'column_num' : req[ 2 ], 'command_arguments': [ command ] + ( [] if len( req ) < 4 else req[ 3 ] ), } response = test[ 'res' ] if isinstance( response, list ): expect = { 'response': requests.codes.ok, 'data': contains_exactly( *[ LocationMatcher( PathToTestFile( folder, os.path.normpath( location[ 0 ] ) ), location[ 1 ], location[ 2 ], **( {} if len( location ) < 4 else location[ 3 ] ) ) for location in response ] ) } elif isinstance( response, tuple ): expect = { 'response': requests.codes.ok, 'data': LocationMatcher( PathToTestFile( folder, os.path.normpath( response[ 0 ] ) ), response[ 1 ], response[ 2 ], **( {} if len( response ) < 4 else response[ 3 ] ) ) } else: expect = { 'response': requests.codes.internal_server_error, 'data': ErrorMatcher( RuntimeError, test[ 'res' ] ) } RunAfterInitialized( app, { 'request': request, 'route' : '/run_completer_command', 'expect' : expect } )
def ServerManagement_OpenProject_RelativePathNoPath_test(app): response = app.post_json( '/run_completer_command', BuildRequest( filetype='java', command_arguments=[ 'OpenProject', ], ), expect_errors=True, ) assert_that(response.status_code, equal_to(requests.codes.internal_server_error)) assert_that( response.json, ErrorMatcher(ValueError, 'Usage: OpenProject <project directory>'))
def ServerManagement_OpenProject_RelativePathNoWD_test(app): response = app.post_json( '/run_completer_command', BuildRequest( filetype='java', command_arguments=[ 'OpenProject', os.path.join('..', 'simple_maven_project'), ], ), expect_errors=True, ) assert_that(response.status_code, equal_to(requests.codes.internal_server_error)) assert_that(response.json, ErrorMatcher(ValueError, 'Project directory must be absolute'))