Skip to content

Commit

Permalink
Merge pull request #1739 from bstaletic/inlay_hints_tests
Browse files Browse the repository at this point in the history
[READY] Add inlay hints tests
  • Loading branch information
mergify[bot] committed Apr 1, 2024
2 parents 743b729 + a05ad3d commit da9c643
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 2 deletions.
186 changes: 186 additions & 0 deletions ycmd/tests/clangd/inlay_hints_test.py
@@ -0,0 +1,186 @@
# Copyright (C) 2024 ycmd contributors
#
# This file is part of ycmd.
#
# ycmd 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.
#
# ycmd 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 ycmd. If not, see <http://www.gnu.org/licenses/>.

import json
import requests
from unittest import TestCase
from hamcrest import assert_that, contains, empty, equal_to, has_entries

from ycmd.tests.clangd import setUpModule, tearDownModule # noqa
from ycmd.tests.clangd import PathToTestFile, SharedYcmd
from ycmd.tests.test_utils import ( BuildRequest,
CombineRequest,
LocationMatcher,
WaitUntilCompleterServerReady )
from ycmd.utils import ReadFile


def RunTest( app, test ):
"""
Method to run a simple completion test and verify the result
Note: Compile commands are extracted from a compile_flags.txt file by clangd
by iteratively looking at the directory containing the source file and its
ancestors.
test is a dictionary containing:
'request': kwargs for BuildRequest
'expect': {
'response': server response code (e.g. requests.codes.ok)
'data': matcher for the server response json
}
"""

request = test[ 'request' ]
filetype = request.get( 'filetype', 'cpp' )
if 'contents' not in request:
contents = ReadFile( request[ 'filepath' ] )
request[ 'contents' ] = contents
request[ 'filetype' ] = filetype

# Because we aren't testing this command, we *always* ignore errors. This
# is mainly because we (may) want to test scenarios where the completer
# throws an exception and the easiest way to do that is to throw from
# within the Settings function.
app.post_json( '/event_notification',
CombineRequest( request, {
'event_name': 'FileReadyToParse',
'filetype': filetype
} ),
expect_errors = True )
WaitUntilCompleterServerReady( app, filetype )

# We also ignore errors here, but then we check the response code ourself.
# This is to allow testing of requests returning errors.
response = app.post_json( '/inlay_hints',
BuildRequest( **request ),
expect_errors = True )

assert_that( response.status_code,
equal_to( test[ 'expect' ][ 'response' ] ) )

print( f'Completer response: { json.dumps( response.json, indent = 2 ) }' )

assert_that( response.json, test[ 'expect' ][ 'data' ] )


class SignatureHelpTest( TestCase ):
@SharedYcmd
def test_none( self, app ):
filepath = PathToTestFile( 'template.cc' )
RunTest( app, {
'request': {
'filetype': 'cpp',
'filepath': filepath,
'range': {
'start': {
'line_num': 0,
'column_num': 0,
'filepath': filepath
},
'end': {
'line_num': 2,
'column_num': 0,
'filepath': filepath
},
}
},
'expect': {
'response': requests.codes.ok,
'data': has_entries( {
'errors': empty(),
'inlay_hints': empty(),
} )
},
} )


@SharedYcmd
def test_basic( self, app ):
filepath = PathToTestFile( 'inlay_hints_basic.cpp' )
RunTest( app, {
'request': {
'filetype' : 'cpp',
'filepath': filepath,
'range': {
'start': {
'line_num': 0,
'column_num': 0,
'filepath': filepath
},
'end': {
'line_num': 2,
'column_num': 0,
'filepath': filepath
},
}
},
'expect': {
'response': requests.codes.ok,
'data': has_entries( {
'errors': empty(),
'inlay_hints': contains(
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 2, 16 ),
'label': 'b:'
} ),
),
} )
},
} )


@SharedYcmd
def test_multiple( self, app ):
filepath = PathToTestFile( 'inlay_hints_multiple.cpp' )
RunTest( app, {
'request': {
'filetype' : 'cpp',
'filepath': filepath,
'range': {
'start': {
'line_num': 0,
'column_num': 0,
'filepath': filepath
},
'end': {
'line_num': 2,
'column_num': 0,
'filepath': filepath
},
}
},
'expect': {
'response': requests.codes.ok,
'data': has_entries( {
'errors': empty(),
'inlay_hints': contains(
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 2, 16 ),
'label': 'a:'
} ),
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 2, 19 ),
'label': 'b:'
} )
),
} )
},
} )
4 changes: 2 additions & 2 deletions ycmd/tests/clangd/semantic_highlighting_test.py
@@ -1,4 +1,4 @@
# Copyright (C) 2021 ycmd contributors
# Copyright (C) 2024 ycmd contributors
#
# This file is part of ycmd.
#
Expand Down Expand Up @@ -79,7 +79,7 @@ def RunTest( app, test ):


class SignatureHelpTest( TestCase ):
@IsolatedYcmd
@IsolatedYcmd()
def test_none( self, app ):
RunTest( app, {
'request': {
Expand Down
2 changes: 2 additions & 0 deletions ycmd/tests/clangd/testdata/inlay_hints_basic.cpp
@@ -0,0 +1,2 @@
void f(int b);
int main() { f(2); }
2 changes: 2 additions & 0 deletions ycmd/tests/clangd/testdata/inlay_hints_multiple.cpp
@@ -0,0 +1,2 @@
void f(int a, int b);
int main() { f(5, 2); }
125 changes: 125 additions & 0 deletions ycmd/tests/go/inlay_hints_test.py
@@ -0,0 +1,125 @@
# Copyright (C) 2024 ycmd contributors
#
# This file is part of ycmd.
#
# ycmd 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.
#
# ycmd 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 ycmd. If not, see <http://www.gnu.org/licenses/>.

import json
import requests
from unittest import TestCase
from hamcrest import assert_that, contains, empty, equal_to, has_entries

from ycmd.tests.go import setUpModule, tearDownModule # noqa
from ycmd.tests.go import PathToTestFile, SharedYcmd
from ycmd.tests.test_utils import ( BuildRequest,
CombineRequest,
LocationMatcher,
WaitUntilCompleterServerReady )
from ycmd.utils import ReadFile


def RunTest( app, test ):
"""
Method to run a simple completion test and verify the result
Note: Compile commands are extracted from a compile_flags.txt file by go
by iteratively looking at the directory containing the source file and its
ancestors.
test is a dictionary containing:
'request': kwargs for BuildRequest
'expect': {
'response': server response code (e.g. requests.codes.ok)
'data': matcher for the server response json
}
"""

request = test[ 'request' ]
filetype = request.get( 'filetype', 'go' )
if 'contents' not in request:
contents = ReadFile( request[ 'filepath' ] )
request[ 'contents' ] = contents
request[ 'filetype' ] = filetype

# Because we aren't testing this command, we *always* ignore errors. This
# is mainly because we (may) want to test scenarios where the completer
# throws an exception and the easiest way to do that is to throw from
# within the Settings function.
app.post_json( '/event_notification',
CombineRequest( request, {
'event_name': 'FileReadyToParse',
'filetype': filetype
} ),
expect_errors = True )
WaitUntilCompleterServerReady( app, filetype )

# We also ignore errors here, but then we check the response code ourself.
# This is to allow testing of requests returning errors.
response = app.post_json( '/inlay_hints',
BuildRequest( **request ),
expect_errors = True )

assert_that( response.status_code,
equal_to( test[ 'expect' ][ 'response' ] ) )

print( f'Completer response: { json.dumps( response.json, indent = 2 ) }' )

assert_that( response.json, test[ 'expect' ][ 'data' ] )


class SignatureHelpTest( TestCase ):
@SharedYcmd
def test_basic( self, app ):
filepath = PathToTestFile( 'td', 'signature_help.go' )
RunTest( app, {
'request': {
'filetype' : 'go',
'filepath': filepath,
'range': {
'start': {
'line_num': 1,
'column_num': 1,
'filepath': filepath
},
'end': {
'line_num': 14,
'column_num': 1,
'filepath': filepath
},
}
},
'expect': {
'response': requests.codes.ok,
'data': has_entries( {
'errors': empty(),
'inlay_hints': contains(
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 10, 14 ),
'label': 'a...:'
} ),
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 10, 18 ),
'label': 'x:'
} ),
has_entries( {
'kind': 'Parameter',
'position': LocationMatcher( filepath, 10, 22 ),
'label': 'y:'
} ),
),
} )
},
} )

0 comments on commit da9c643

Please sign in to comment.