From a05ad3dd3f57b6b41be5ba71fea2d9d819260f7d Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Mon, 1 Apr 2024 14:42:16 +0200 Subject: [PATCH] Add inlay hints tests --- ycmd/tests/clangd/inlay_hints_test.py | 186 ++++++++++++++++++ .../clangd/semantic_highlighting_test.py | 4 +- .../clangd/testdata/inlay_hints_basic.cpp | 2 + .../clangd/testdata/inlay_hints_multiple.cpp | 2 + ycmd/tests/go/inlay_hints_test.py | 125 ++++++++++++ ycmd/tests/rust/inlay_hints_test.py | 115 +++++++++++ 6 files changed, 432 insertions(+), 2 deletions(-) create mode 100644 ycmd/tests/clangd/inlay_hints_test.py create mode 100644 ycmd/tests/clangd/testdata/inlay_hints_basic.cpp create mode 100644 ycmd/tests/clangd/testdata/inlay_hints_multiple.cpp create mode 100644 ycmd/tests/go/inlay_hints_test.py create mode 100644 ycmd/tests/rust/inlay_hints_test.py diff --git a/ycmd/tests/clangd/inlay_hints_test.py b/ycmd/tests/clangd/inlay_hints_test.py new file mode 100644 index 0000000000..5e9af44e2d --- /dev/null +++ b/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 . + +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:' + } ) + ), + } ) + }, + } ) diff --git a/ycmd/tests/clangd/semantic_highlighting_test.py b/ycmd/tests/clangd/semantic_highlighting_test.py index 875c6269db..4ac49f198c 100644 --- a/ycmd/tests/clangd/semantic_highlighting_test.py +++ b/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. # @@ -79,7 +79,7 @@ def RunTest( app, test ): class SignatureHelpTest( TestCase ): - @IsolatedYcmd + @IsolatedYcmd() def test_none( self, app ): RunTest( app, { 'request': { diff --git a/ycmd/tests/clangd/testdata/inlay_hints_basic.cpp b/ycmd/tests/clangd/testdata/inlay_hints_basic.cpp new file mode 100644 index 0000000000..f224cfafca --- /dev/null +++ b/ycmd/tests/clangd/testdata/inlay_hints_basic.cpp @@ -0,0 +1,2 @@ +void f(int b); +int main() { f(2); } diff --git a/ycmd/tests/clangd/testdata/inlay_hints_multiple.cpp b/ycmd/tests/clangd/testdata/inlay_hints_multiple.cpp new file mode 100644 index 0000000000..dfaf42f3e0 --- /dev/null +++ b/ycmd/tests/clangd/testdata/inlay_hints_multiple.cpp @@ -0,0 +1,2 @@ +void f(int a, int b); +int main() { f(5, 2); } diff --git a/ycmd/tests/go/inlay_hints_test.py b/ycmd/tests/go/inlay_hints_test.py new file mode 100644 index 0000000000..13a7679560 --- /dev/null +++ b/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 . + +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:' + } ), + ), + } ) + }, + } ) diff --git a/ycmd/tests/rust/inlay_hints_test.py b/ycmd/tests/rust/inlay_hints_test.py new file mode 100644 index 0000000000..ed46b46601 --- /dev/null +++ b/ycmd/tests/rust/inlay_hints_test.py @@ -0,0 +1,115 @@ +# 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 . + +import json +import requests +from unittest import TestCase +from hamcrest import assert_that, contains, empty, equal_to, has_entries + +from ycmd.tests.rust import setUpModule, tearDownModule # noqa +from ycmd.tests.rust 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 rust + 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', 'rust' ) + 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( 'common', 'src', 'test.rs' ) + RunTest( app, { + 'request': { + 'filetype' : 'rust', + 'filepath': filepath, + 'range': { + 'start': { + 'line_num': 1, + 'column_num': 1, + 'filepath': filepath + }, + 'end': { + 'line_num': 15, + 'column_num': 1, + 'filepath': filepath + }, + } + }, + 'expect': { + 'response': requests.codes.ok, + 'data': has_entries( { + 'errors': empty(), + 'inlay_hints': contains( + has_entries( { + 'kind': 'Type', + 'position': LocationMatcher( filepath, 12, 10 ), + 'label': ': Builder ' + } ), + ), + } ) + }, + } )