Skip to content

Commit 05a4341

Browse files
authored
Add broad spectrum tests for python (#273)
1 parent e547d3d commit 05a4341

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

noxfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def tests(session: nox.Session):
2323
"""Run tests for generator and generated code in all languages."""
2424
_install_requirements(session)
2525

26+
session.log("Running test data generator.")
27+
session.run("python", "-m", "generator", "--plugin", "testdata")
28+
2629
session.log("Running tests: generator and generated Python code.")
2730
session.run("pytest", "./tests")
2831

packages/python/lsprotocol/_hooks.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,88 @@ def _notebook_sync_option_selector_hook(
634634
object_, lsp_types.NotebookDocumentSyncOptionsNotebookSelectorType2
635635
)
636636

637+
def _semantic_token_registration_options_hook(
638+
object_: Any, _: type
639+
) -> Optional[
640+
Union[OptionalPrimitive, lsp_types.SemanticTokensRegistrationOptionsFullType1]
641+
]:
642+
if object_ is None:
643+
return None
644+
if isinstance(object_, (bool, int, str, float)):
645+
return object_
646+
return converter.structure(
647+
object_, lsp_types.SemanticTokensRegistrationOptionsFullType1
648+
)
649+
650+
def _inline_completion_provider_hook(
651+
object_: Any, _: type
652+
) -> Optional[Union[OptionalPrimitive, lsp_types.InlineCompletionOptions]]:
653+
if object_ is None:
654+
return None
655+
if isinstance(object_, (bool, int, str, float)):
656+
return object_
657+
return converter.structure(object_, lsp_types.InlineCompletionOptions)
658+
659+
def _inline_completion_list_hook(
660+
object_: Any, _: type
661+
) -> Optional[
662+
Union[lsp_types.InlineCompletionList, List[lsp_types.InlineCompletionItem]]
663+
]:
664+
if object_ is None:
665+
return None
666+
if isinstance(object_, list):
667+
return [
668+
converter.structure(item, lsp_types.InlineCompletionItem)
669+
for item in object_
670+
]
671+
return converter.structure(object_, lsp_types.InlineCompletionList)
672+
673+
def _string_value_hook(
674+
object_: Any, _: type
675+
) -> Union[OptionalPrimitive, lsp_types.StringValue]:
676+
if object_ is None:
677+
return None
678+
if isinstance(object_, (bool, int, str, float)):
679+
return object_
680+
return converter.structure(object_, lsp_types.StringValue)
681+
682+
def _symbol_list_hook(
683+
object_: Any, _: type
684+
) -> Optional[
685+
Union[List[lsp_types.SymbolInformation], List[lsp_types.WorkspaceSymbol]]
686+
]:
687+
if object_ is None:
688+
return None
689+
assert isinstance(object_, list)
690+
if len(object_) == 0:
691+
return [] # type: ignore[return-value]
692+
if "location" in object_[0]:
693+
return [
694+
converter.structure(item, lsp_types.SymbolInformation)
695+
for item in object_
696+
]
697+
else:
698+
return [
699+
converter.structure(item, lsp_types.WorkspaceSymbol) for item in object_
700+
]
701+
702+
def _notebook_sync_registration_option_selector_hook(
703+
object_: Any, _: type
704+
) -> Union[
705+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType1,
706+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType2,
707+
]:
708+
if "notebook" in object_:
709+
return converter.structure(
710+
object_,
711+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType1,
712+
)
713+
else:
714+
return converter.structure(
715+
object_,
716+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType2,
717+
)
718+
637719
structure_hooks = [
638720
(
639721
Optional[
@@ -940,6 +1022,41 @@ def _notebook_sync_option_selector_hook(
9401022
],
9411023
_position_encoding_hook,
9421024
),
1025+
(
1026+
Optional[Union[bool, lsp_types.SemanticTokensRegistrationOptionsFullType1]],
1027+
_semantic_token_registration_options_hook,
1028+
),
1029+
(
1030+
Optional[Union[bool, lsp_types.InlineCompletionOptions]],
1031+
_inline_completion_provider_hook,
1032+
),
1033+
(
1034+
Optional[
1035+
Union[
1036+
lsp_types.InlineCompletionList, List[lsp_types.InlineCompletionItem]
1037+
]
1038+
],
1039+
_inline_completion_list_hook,
1040+
),
1041+
(
1042+
Union[str, lsp_types.StringValue],
1043+
_string_value_hook,
1044+
),
1045+
(
1046+
Optional[
1047+
Union[
1048+
List[lsp_types.SymbolInformation], List[lsp_types.WorkspaceSymbol]
1049+
]
1050+
],
1051+
_symbol_list_hook,
1052+
),
1053+
(
1054+
Union[
1055+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType1,
1056+
lsp_types.NotebookDocumentSyncRegistrationOptionsNotebookSelectorType2,
1057+
],
1058+
_notebook_sync_registration_option_selector_hook,
1059+
),
9431060
]
9441061
for type_, hook in structure_hooks:
9451062
converter.register_structure_hook(type_, hook)

tests/python/test_generated_data.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
import pathlib
6+
from typing import Generator, List, Union
7+
8+
import pytest
9+
10+
import lsprotocol.converters as cv
11+
import lsprotocol.types as lsp
12+
13+
TEST_DATA_ROOT = pathlib.Path(__file__).parent.parent.parent / "packages" / "testdata"
14+
15+
16+
def get_all_json_files(root: Union[pathlib.Path, str]) -> List[pathlib.Path]:
17+
root_path = pathlib.Path(root)
18+
return list(root_path.glob("**/*.json"))
19+
20+
21+
converter = cv.get_converter()
22+
23+
24+
@pytest.mark.parametrize("json_file", get_all_json_files(TEST_DATA_ROOT))
25+
def test_generated_data(json_file: str) -> None:
26+
type_name, result_type, _ = json_file.name.split("-", 2)
27+
lsp_type = getattr(lsp, type_name)
28+
data = json.loads(json_file.read_text(encoding="utf-8"))
29+
30+
try:
31+
converter.structure(data, lsp_type)
32+
assert result_type == "True", "Expected error, but succeeded structuring"
33+
except Exception as e:
34+
assert result_type == "False", "Expected success, but failed structuring"

0 commit comments

Comments
 (0)