Refactored the server tests for simplicity

This commit is contained in:
Strahinja Val Markovic 2013-10-07 13:55:29 -07:00
parent ff7fa74fc9
commit 1db0e720bc

View File

@ -29,28 +29,40 @@ bottle.debug( True )
# TODO: Split this file into multiple files. # TODO: Split this file into multiple files.
# 'contents' should be just one line of text def BuildRequest( **kwargs ):
def RequestDataForFileWithContents( filename, filepath = kwargs[ 'filepath' ] if 'filepath' in kwargs else '/foo'
contents = None, contents = kwargs[ 'contents' ] if 'contents' in kwargs else ''
filetype = None ): filetype = kwargs[ 'filetype' ] if 'filetype' in kwargs else 'foo'
real_contents = contents if contents else ''
filetype_to_use = filetype or 'foo' request = {
return {
'query': '', 'query': '',
'line_num': 0, 'line_num': 0,
'column_num': 0, 'column_num': 0,
'start_column': 0, 'start_column': 0,
'filetypes': [ filetype_to_use ], 'filetypes': [ filetype ],
'filepath': filename, 'filepath': filepath,
'line_value': real_contents, 'line_value': contents,
'file_data': { 'file_data': {
filename: { filepath: {
'contents': real_contents, 'contents': contents,
'filetypes': [ filetype_to_use ] 'filetypes': [ filetype ]
} }
} }
} }
for key, value in kwargs.iteritems():
if key in [ 'contents', 'filetype', 'filepath' ]:
continue
request[ key ] = value
if key == 'line_num':
lines = contents.splitlines()
if len( lines ) > 1:
# NOTE: assumes 0-based line_num
request[ 'line_value' ] = lines[ value ]
return request
# TODO: Make the other tests use this helper too instead of BuildCompletionData # TODO: Make the other tests use this helper too instead of BuildCompletionData
def CompletionEntryMatcher( insertion_text ): def CompletionEntryMatcher( insertion_text ):
@ -64,21 +76,14 @@ def Setup():
@with_setup( Setup ) @with_setup( Setup )
def GetCompletions_IdentifierCompleter_Works_test(): def GetCompletions_IdentifierCompleter_Works_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
event_data = RequestDataForFileWithContents( '/foo/bar', 'foo foogoo ba' ) event_data = BuildRequest( contents = 'foo foogoo ba',
event_data.update( { event_name = 'FileReadyToParse' )
'event_name': 'FileReadyToParse',
} )
app.post_json( '/event_notification', event_data ) app.post_json( '/event_notification', event_data )
completion_data = RequestDataForFileWithContents( '/foo/bar', completion_data = BuildRequest( contents = 'oo foo foogoo ba',
'oo foo foogoo ba' ) query = 'oo',
completion_data.update( { column_num = 2 )
'query': 'oo',
'line_num': 0,
'column_num': 2,
'start_column': 0,
} )
eq_( [ BuildCompletionData( 'foo' ), eq_( [ BuildCompletionData( 'foo' ),
BuildCompletionData( 'foogoo' ) ], BuildCompletionData( 'foogoo' ) ],
@ -102,24 +107,14 @@ int main()
} }
""" """
filename = '/foo.cpp' # 0-based line and column!
completion_data = { completion_data = BuildRequest( filepath = '/foo.cpp',
'compilation_flags': ['-x', 'c++'], filetype = 'cpp',
# 0-based line and column! contents = contents,
'query': '', line_num = 10,
'line_num': 10, column_num = 6,
'column_num': 6, start_column = 6,
'start_column': 6, compilation_flags = ['-x', 'c++'] )
'line_value': ' foo.',
'filetypes': ['cpp'],
'filepath': filename,
'file_data': {
filename: {
'contents': contents,
'filetypes': ['cpp']
}
}
}
results = app.post_json( '/completions', completion_data ).json results = app.post_json( '/completions', completion_data ).json
assert_that( results, has_items( CompletionEntryMatcher( 'c' ), assert_that( results, has_items( CompletionEntryMatcher( 'c' ),
@ -131,11 +126,8 @@ int main()
def GetCompletions_ForceSemantic_Works_test(): def GetCompletions_ForceSemantic_Works_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
completion_data = RequestDataForFileWithContents( 'foo.py', completion_data = BuildRequest( filetype = 'python',
filetype = 'python' ) force_semantic = True )
completion_data.update( {
'force_semantic': True,
} )
results = app.post_json( '/completions', completion_data ).json results = app.post_json( '/completions', completion_data ).json
assert_that( results, has_items( CompletionEntryMatcher( 'abs' ), assert_that( results, has_items( CompletionEntryMatcher( 'abs' ),
@ -146,22 +138,14 @@ def GetCompletions_ForceSemantic_Works_test():
@with_setup( Setup ) @with_setup( Setup )
def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data = BuildRequest( event_name = 'FileReadyToParse',
event_data.update( { syntax_keywords = ['foo', 'bar', 'zoo'] )
'event_name': 'FileReadyToParse',
'syntax_keywords': ['foo', 'bar', 'zoo']
} )
app.post_json( '/event_notification', event_data ) app.post_json( '/event_notification', event_data )
completion_data = RequestDataForFileWithContents( '/foo/bar', completion_data = BuildRequest( contents = 'oo ',
'oo ' ) query = 'oo',
completion_data.update( { column_num = 2 )
'query': 'oo',
'line_num': 0,
'column_num': 2,
'start_column': 0,
} )
eq_( [ BuildCompletionData( 'foo' ), eq_( [ BuildCompletionData( 'foo' ),
BuildCompletionData( 'zoo' ) ], BuildCompletionData( 'zoo' ) ],
@ -171,24 +155,18 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test():
@with_setup( Setup ) @with_setup( Setup )
def GetCompletions_UltiSnipsCompleter_Works_test(): def GetCompletions_UltiSnipsCompleter_Works_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data = BuildRequest(
event_data.update( { event_name = 'BufferVisit',
'event_name': 'BufferVisit', ultisnips_snippets = [
'ultisnips_snippets': [
{'trigger': 'foo', 'description': 'bar'}, {'trigger': 'foo', 'description': 'bar'},
{'trigger': 'zoo', 'description': 'goo'}, {'trigger': 'zoo', 'description': 'goo'},
] ] )
} )
app.post_json( '/event_notification', event_data ) app.post_json( '/event_notification', event_data )
completion_data = RequestDataForFileWithContents( '/foo/bar', 'oo ' ) completion_data = BuildRequest( contents = 'oo ',
completion_data.update( { query = 'oo',
'query': 'oo', column_num = 2 )
'line_num': 0,
'column_num': 2,
'start_column': 0,
} )
eq_( [ BuildCompletionData( 'foo', '<snip> bar' ), eq_( [ BuildCompletionData( 'foo', '<snip> bar' ),
BuildCompletionData( 'zoo', '<snip> goo' ) ], BuildCompletionData( 'zoo', '<snip> goo' ) ],
@ -205,20 +183,12 @@ def foo():
foo() foo()
""" """
goto_data = { goto_data = BuildRequest( completer_target = 'filetype_default',
'completer_target': 'filetype_default', command_arguments = ['GoToDefinition'],
'command_arguments': ['GoToDefinition'], line_num = 4,
'line_num': 4, contents = contents,
'column_num': 0, filetype = 'python',
'filetypes': ['python'], filepath = '/foo.py' )
'filepath': '/foo.py',
'file_data': {
'/foo.py': {
'contents': contents,
'filetypes': ['python']
}
}
}
# 0-based line and column! # 0-based line and column!
eq_( { eq_( {
@ -246,26 +216,17 @@ int main()
} }
""" """
filename = '/foo.cpp' goto_data = BuildRequest( completer_target = 'filetype_default',
goto_data = { command_arguments = ['GoToDefinition'],
'compilation_flags': ['-x', 'c++'], compilation_flags = ['-x', 'c++'],
'completer_target': 'filetype_default', line_num = 9,
'command_arguments': ['GoToDefinition'], column_num = 2,
'line_num': 9, contents = contents,
'column_num': 2, filetype = 'cpp' )
'filetypes': ['cpp'],
'filepath': filename,
'file_data': {
filename: {
'contents': contents,
'filetypes': ['cpp']
}
}
}
# 0-based line and column! # 0-based line and column!
eq_( { eq_( {
'filepath': '/foo.cpp', 'filepath': '/foo',
'line_num': 1, 'line_num': 1,
'column_num': 7 'column_num': 7
}, },
@ -275,10 +236,7 @@ int main()
@with_setup( Setup ) @with_setup( Setup )
def DefinedSubcommands_Works_test(): def DefinedSubcommands_Works_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
subcommands_data = RequestDataForFileWithContents( '/foo/bar' ) subcommands_data = BuildRequest( completer_target = 'python' )
subcommands_data.update( {
'completer_target': 'python',
} )
eq_( [ 'GoToDefinition', eq_( [ 'GoToDefinition',
'GoToDeclaration', 'GoToDeclaration',
@ -289,17 +247,7 @@ def DefinedSubcommands_Works_test():
@with_setup( Setup ) @with_setup( Setup )
def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test():
app = TestApp( ycmd.app ) app = TestApp( ycmd.app )
filename = 'foo.py' subcommands_data = BuildRequest( filetype = 'python' )
subcommands_data = {
'filetypes': ['python'],
'filepath': filename,
'file_data': {
filename: {
'contents': '',
'filetypes': ['python']
}
}
}
eq_( [ 'GoToDefinition', eq_( [ 'GoToDefinition',
'GoToDeclaration', 'GoToDeclaration',
@ -319,21 +267,10 @@ struct Foo {
}; };
""" """
filename = '/foo.cpp' event_data = BuildRequest( compilation_flags = ['-x', 'c++'],
event_data = { event_name = 'FileReadyToParse',
'event_name': 'FileReadyToParse', contents = contents,
'compilation_flags': ['-x', 'c++'], filetype = 'cpp' )
'line_num': 0,
'column_num': 0,
'filetypes': ['cpp'],
'filepath': filename,
'file_data': {
filename: {
'contents': contents,
'filetypes': ['cpp']
}
}
}
results = app.post_json( '/event_notification', event_data ).json results = app.post_json( '/event_notification', event_data ).json
assert_that( results, assert_that( results,
@ -355,20 +292,10 @@ struct Foo {
}; };
""" """
filename = '/foo.cpp' diag_data = BuildRequest( compilation_flags = ['-x', 'c++'],
diag_data = { line_num = 2,
'compilation_flags': ['-x', 'c++'], contents = contents,
'line_num': 2, filetype = 'cpp' )
'column_num': 0,
'filetypes': ['cpp'],
'filepath': filename,
'file_data': {
filename: {
'contents': contents,
'filetypes': ['cpp']
}
}
}
event_data = diag_data.copy() event_data = diag_data.copy()
event_data.update( { event_data.update( {