Skip to content

Commit b8071d5

Browse files
committed
implement some unit tests for hover
1 parent a10a58d commit b8071d5

File tree

9 files changed

+103
-10
lines changed

9 files changed

+103
-10
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ addopts = "-ra -vv"
7474
filterwarnings = "ignore:.*Using or importing the ABCs from 'collections' instead of from 'collections.abc'.*:DeprecationWarning"
7575
testpaths = ["tests"]
7676
junit_suite_name = "robotcode"
77+
log_cli = true
78+
log_cli_level = "DEBUG"
7779

7880
[tool.isort]
7981
profile = "black"

robotcode/language_server/common/parts/workspace.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,20 @@ async def add_file_watchers(
387387
else:
388388
self.did_change_watched_files.add(callback) # type: ignore
389389

390-
await self.parent.register_capability(
391-
entry.id,
392-
"workspace/didChangeWatchedFiles",
393-
DidChangeWatchedFilesRegistrationOptions(
394-
watchers=[FileSystemWatcher(glob_pattern=w.glob_pattern, kind=w.kind) for w in _watchers]
395-
),
396-
)
390+
if (
391+
self.parent.client_capabilities
392+
and self.parent.client_capabilities.workspace
393+
and self.parent.client_capabilities.workspace.did_change_watched_files
394+
and self.parent.client_capabilities.workspace.did_change_watched_files.dynamic_registration
395+
):
396+
await self.parent.register_capability(
397+
entry.id,
398+
"workspace/didChangeWatchedFiles",
399+
DidChangeWatchedFilesRegistrationOptions(
400+
watchers=[FileSystemWatcher(glob_pattern=w.glob_pattern, kind=w.kind) for w in _watchers]
401+
),
402+
)
403+
# TODO: implement own filewatcher if not supported by language server client
397404

398405
def remove() -> None:
399406
if self._loop.is_running():
@@ -414,4 +421,11 @@ async def remove_file_watcher_entry(self, entry: FileWatcherEntry) -> None:
414421
self.did_change_watched_files.remove(entry.call_childrens)
415422
elif len(entry.child_callbacks) == 0:
416423
self.did_change_watched_files.remove(entry.callback) # type: ignore
417-
await self.parent.unregister_capability(entry.id, "workspace/didChangeWatchedFiles")
424+
if (
425+
self.parent.client_capabilities
426+
and self.parent.client_capabilities.workspace
427+
and self.parent.client_capabilities.workspace.did_change_watched_files
428+
and self.parent.client_capabilities.workspace.did_change_watched_files.dynamic_registration
429+
):
430+
await self.parent.unregister_capability(entry.id, "workspace/didChangeWatchedFiles")
431+
# TODO: implement own filewatcher if not supported by language server client

robotcode/utils/uri.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ def _to_path_str(self) -> str:
100100
if self._parts.scheme != "file":
101101
raise InvalidUriError(f"Invalid URI scheme '{str(self)}'.")
102102

103-
if netloc and path and self._parts.scheme == "file":
103+
if netloc and self._parts.scheme == "file":
104104
# unc path: file://shares/c$/far/boo
105-
value = "//{}{}".format(netloc, path)
105+
value = "//{}{}".format(netloc, path or "")
106106

107107
elif _RE_DRIVE_LETTER_PATH.match(path):
108108
# windows drive letter: file:///C:/far/boo

tests/language_server/common/test_window.py

Whitespace-only changes.

tests/language_server/robotframework/__init__.py

Whitespace-only changes.

tests/language_server/robotframework/parts/__init__.py

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from pathlib import Path
2+
from typing import AsyncGenerator
3+
4+
import pytest
5+
6+
from robotcode.language_server.common.text_document import TextDocument
7+
from robotcode.language_server.common.types import (
8+
ClientCapabilities,
9+
MarkupContent,
10+
MarkupKind,
11+
Position,
12+
Range,
13+
)
14+
from robotcode.language_server.robotframework.protocol import (
15+
RobotLanguageServerProtocol,
16+
)
17+
from robotcode.language_server.robotframework.server import RobotLanguageServer
18+
19+
20+
@pytest.fixture
21+
async def protocol() -> AsyncGenerator[RobotLanguageServerProtocol, None]:
22+
root_path = Path().resolve()
23+
server = RobotLanguageServer()
24+
try:
25+
protocol = RobotLanguageServerProtocol(server)
26+
await protocol._initialize(ClientCapabilities(), root_path=str(root_path), root_uri=root_path.as_uri())
27+
28+
yield protocol
29+
finally:
30+
server.close()
31+
32+
33+
@pytest.fixture
34+
async def test_document() -> AsyncGenerator[TextDocument, None]:
35+
data = """\
36+
***Test Cases***
37+
first
38+
Log Hello
39+
"""
40+
yield TextDocument(document_uri="file:///test.robot", language_id="robotframework", version=1, text=data)
41+
42+
43+
@pytest.mark.parametrize(
44+
("position",),
45+
[
46+
(Position(line=2, character=4),),
47+
(Position(line=2, character=5),),
48+
(Position(line=2, character=6),),
49+
],
50+
)
51+
@pytest.mark.asyncio
52+
async def test_hover_should_find_simple_keyword(
53+
protocol: RobotLanguageServerProtocol, test_document: TextDocument, position: Position
54+
) -> None:
55+
56+
result = await protocol._robot_hover.collect(protocol.hover, test_document, position)
57+
assert result
58+
assert result.range == Range(start=Position(line=2, character=4), end=Position(line=2, character=7))
59+
assert isinstance(result.contents, MarkupContent)
60+
assert result.contents.kind == MarkupKind.MARKDOWN
61+
assert "Log" in result.contents.value
62+
63+
64+
@pytest.mark.parametrize(
65+
("position",),
66+
[
67+
(Position(line=2, character=3),),
68+
(Position(line=2, character=7),),
69+
],
70+
)
71+
@pytest.mark.asyncio
72+
async def test_hover_should_not_find_simple_keyword_on_boundaries(
73+
protocol: RobotLanguageServerProtocol, test_document: TextDocument, position: Position
74+
) -> None:
75+
76+
result = await protocol._robot_hover.collect(protocol.hover, test_document, position)
77+
assert result is None

0 commit comments

Comments
 (0)