Skip to content

Commit 51add92

Browse files
committed
add more hover tests, add posibility to skip TODO's in source document
1 parent 0892f8f commit 51add92

File tree

5 files changed

+137
-105
lines changed

5 files changed

+137
-105
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import asyncio
2+
from pathlib import Path
3+
from typing import Any, AsyncGenerator, Generator, cast
4+
5+
import pytest
6+
7+
from robotcode.language_server.common.parts.workspace import HasConfigSection
8+
from robotcode.language_server.common.text_document import TextDocument
9+
from robotcode.language_server.common.types import (
10+
ClientCapabilities,
11+
ClientInfo,
12+
HoverClientCapabilities,
13+
InitializedParams,
14+
MarkupKind,
15+
TextDocumentClientCapabilities,
16+
WorkspaceFolder,
17+
)
18+
from robotcode.language_server.robotframework.configuration import (
19+
RobotCodeConfig,
20+
RobotConfig,
21+
)
22+
from robotcode.language_server.robotframework.protocol import (
23+
RobotLanguageServerProtocol,
24+
)
25+
from robotcode.language_server.robotframework.server import RobotLanguageServer
26+
27+
28+
@pytest.fixture(scope="module")
29+
def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
30+
loop = asyncio.new_event_loop()
31+
yield loop
32+
loop.close()
33+
34+
35+
@pytest.fixture(scope="module")
36+
async def protocol(event_loop: asyncio.AbstractEventLoop) -> AsyncGenerator[RobotLanguageServerProtocol, None]:
37+
root_path = Path().resolve()
38+
server = RobotLanguageServer()
39+
try:
40+
protocol = RobotLanguageServerProtocol(server)
41+
await protocol._initialize(
42+
ClientCapabilities(
43+
text_document=TextDocumentClientCapabilities(
44+
hover=HoverClientCapabilities(content_format=[MarkupKind.MARKDOWN, MarkupKind.PLAINTEXT])
45+
)
46+
),
47+
root_path=str(root_path),
48+
root_uri=root_path.as_uri(),
49+
workspace_folders=[WorkspaceFolder(name="test workspace", uri=root_path.as_uri())],
50+
client_info=ClientInfo(name="TestClient", version="1.0.0"),
51+
)
52+
await protocol._initialized(InitializedParams())
53+
await protocol.workspace._workspace_did_change_configuration(
54+
{
55+
cast(HasConfigSection, RobotCodeConfig)
56+
.__config_section__: RobotCodeConfig(
57+
robot=RobotConfig(
58+
env={"ENV_VAR": "1"},
59+
variables={
60+
"CMD_VAR": "1",
61+
},
62+
)
63+
)
64+
.dict()
65+
}
66+
)
67+
yield protocol
68+
finally:
69+
server.close()
70+
71+
72+
@pytest.fixture(scope="module")
73+
async def test_document(event_loop: asyncio.AbstractEventLoop, request: Any) -> AsyncGenerator[TextDocument, None]:
74+
data_path = Path(request.param)
75+
data = data_path.read_text()
76+
77+
document = TextDocument(
78+
document_uri=data_path.absolute().as_uri(), language_id="robotframework", version=1, text=data
79+
)
80+
try:
81+
yield document
82+
finally:
83+
del document

tests/robotcode/language_server/robotframework/parts/data/hover.robot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ Library Collections
33
# ^^^^^^^^^^^ library import by module name: re.match(r'## Library \*Collections\*.*', value)
44
Library ${CURDIR}/libs/myvariables.py
55
# ^^^^^^^^^^^^^^ library import by path name: re.match(r'## Library \*myvariables.*', value)
6-
# TODO ^^^^^^^^^ variable in library name: value == '(builtin variable) ${CURDIR}'
6+
# TODO ^^^^^^^^^ variable in library import: value == '(builtin variable) ${CURDIR}'
77
Variables ${CURDIR}/libs/myvariables.py
8+
# TODO ^^^^^^^^^ variable in variables import: value == '(builtin variable) ${CURDIR}'
89
Resource ${CURDIR}/resources/firstresource.resource
910
# ^^^^^^^^^^^^^^ library import by path name: re.match(r'## Resource \*firstresource.*', value)
11+
# TODO ^^^^^^^^^ variable in resource import: value == '(builtin variable) ${CURDIR}'
1012

1113
*** Variables ***
1214
${A VAR} i'm a var
@@ -31,3 +33,6 @@ first
3133
Log ${CURDIR}
3234
# ^^^^^^^^^ BuiltIn variable: value == '(builtin variable) ${CURDIR}'
3335
#^^^ Spaces: result is None
36+
Log ${A_VAR_FROM_LIB}
37+
# TODO ^^^^^^^^^^^^^^^^^ BuiltIn variable: value == '(imported variable) ${A_VAR_FROM_LIB}'
38+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
def dummy(): # type: ignore
22
pass
3+
4+
5+
A_VAR_FROM_LIB = 1

tests/robotcode/language_server/robotframework/parts/test_hover.py

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,24 @@
1-
import asyncio
21
import re
32
from pathlib import Path
4-
from typing import Any, AsyncGenerator, Generator, Tuple, cast
53

64
import pytest
75

8-
from robotcode.language_server.common.parts.workspace import HasConfigSection
96
from robotcode.language_server.common.text_document import TextDocument
10-
from robotcode.language_server.common.types import (
11-
ClientCapabilities,
12-
ClientInfo,
13-
HoverClientCapabilities,
14-
InitializedParams,
15-
MarkupContent,
16-
MarkupKind,
17-
Position,
18-
TextDocumentClientCapabilities,
19-
WorkspaceFolder,
20-
)
21-
from robotcode.language_server.robotframework.configuration import (
22-
RobotCodeConfig,
23-
RobotConfig,
24-
)
7+
from robotcode.language_server.common.types import MarkupContent, Position
258
from robotcode.language_server.robotframework.protocol import (
269
RobotLanguageServerProtocol,
2710
)
28-
from robotcode.language_server.robotframework.server import RobotLanguageServer
29-
30-
31-
@pytest.fixture(scope="module")
32-
def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
33-
loop = asyncio.new_event_loop()
34-
yield loop
35-
loop.close()
36-
37-
38-
@pytest.fixture(scope="module")
39-
async def protocol() -> AsyncGenerator[RobotLanguageServerProtocol, None]:
40-
root_path = Path().resolve()
41-
server = RobotLanguageServer()
42-
try:
43-
protocol = RobotLanguageServerProtocol(server)
44-
await protocol._initialize(
45-
ClientCapabilities(
46-
text_document=TextDocumentClientCapabilities(
47-
hover=HoverClientCapabilities(content_format=[MarkupKind.MARKDOWN, MarkupKind.PLAINTEXT])
48-
)
49-
),
50-
root_path=str(root_path),
51-
root_uri=root_path.as_uri(),
52-
workspace_folders=[WorkspaceFolder(name="test workspace", uri=root_path.as_uri())],
53-
client_info=ClientInfo(name="TestClient", version="1.0.0"),
54-
)
55-
await protocol._initialized(InitializedParams())
56-
await protocol.workspace._workspace_did_change_configuration(
57-
{
58-
cast(HasConfigSection, RobotCodeConfig)
59-
.__config_section__: RobotCodeConfig(
60-
robot=RobotConfig(
61-
env={"ENV_VAR": "1"},
62-
variables={
63-
"CMD_VAR": "1",
64-
},
65-
)
66-
)
67-
.dict()
68-
}
69-
)
70-
yield protocol
71-
finally:
72-
server.close()
73-
74-
75-
@pytest.fixture(scope="module")
76-
async def test_document(request: Any) -> AsyncGenerator[TextDocument, None]:
77-
data_path = Path(request.param)
78-
data = data_path.read_text()
79-
80-
document = TextDocument(
81-
document_uri=data_path.absolute().as_uri(), language_id="robotframework", version=1, text=data
82-
)
83-
try:
84-
yield document
85-
finally:
86-
del document
87-
88-
89-
TEST_EXPRESSION_LINE = re.compile(r"^\#\s*(?P<position>\^+)\s*(?P<name>[^:]+)\s*:\s*(?P<expression>.+)")
90-
91-
92-
def generate_tests_from_doc(path: str) -> Generator[Tuple[str, str, int, int, str], None, None]:
93-
file = Path(path)
94-
95-
current_line = 0
96-
for line, text in enumerate(file.read_text().splitlines()):
97-
98-
match = TEST_EXPRESSION_LINE.match(text)
99-
if match:
100-
name = match.group("name").strip()
101-
start, end = match.span("position")
102-
expression = match.group("expression").strip()
103-
if name and expression:
104-
if end - start == 1:
105-
yield path, name, current_line, start, expression
106-
else:
107-
yield path, name, current_line, start, expression
108-
if end - start > 2:
109-
yield path, name, current_line, int(start + (end - start) / 2), expression
11011

111-
yield path, name, current_line, end - 1, expression
112-
else:
113-
current_line = line
12+
from ..tools import generate_tests_from_source_document
11413

11514

11615
@pytest.mark.parametrize(
11716
("test_document", "name", "line", "character", "expression"),
118-
generate_tests_from_doc(str(Path(Path(__file__).parent, "data/hover.robot").relative_to(Path(".").absolute()))),
17+
generate_tests_from_source_document(str(Path(Path(__file__).parent, "data/hover.robot"))),
11918
indirect=["test_document"],
12019
)
12120
@pytest.mark.asyncio
21+
@pytest.mark.usefixtures("protocol")
12222
async def test_hover(
12323
protocol: RobotLanguageServerProtocol,
12424
test_document: TextDocument,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import re
2+
from pathlib import Path
3+
from typing import Any, Generator, Tuple, Union
4+
5+
import pytest
6+
7+
TEST_EXPRESSION_LINE = re.compile(
8+
r"^\#\s*(?P<todo>TODO)?\s*(?P<position>\^+)\s*(?P<name>[^:]+)\s*:\s*(?P<expression>.+)"
9+
)
10+
11+
12+
def generate_tests_from_source_document(
13+
path: str,
14+
) -> Generator[Union[Tuple[str, str, int, int, str], Any], None, None]:
15+
file = Path(path).relative_to(Path(".").parent.absolute())
16+
17+
current_line = 0
18+
for line, text in enumerate(file.read_text().splitlines()):
19+
20+
match = TEST_EXPRESSION_LINE.match(text)
21+
if match:
22+
name = match.group("name").strip()
23+
start, end = match.span("position")
24+
expression = match.group("expression").strip()
25+
skip = match.group("todo")
26+
if name and expression:
27+
if skip:
28+
yield pytest.param(
29+
str(file), name, current_line, start, expression, marks=pytest.mark.skip(reason="TODO")
30+
)
31+
else:
32+
if end - start == 1:
33+
yield str(file), name, current_line, start, expression
34+
else:
35+
yield str(file), name, current_line, start, expression
36+
if end - start > 2:
37+
yield str(file), name, current_line, int(start + (end - start) / 2), expression
38+
39+
yield str(file), name, current_line, end - 1, expression
40+
else:
41+
current_line = line

0 commit comments

Comments
 (0)