Skip to content

Commit f0c1304

Browse files
committed
add tests for folding range
1 parent 043935a commit f0c1304

File tree

5 files changed

+107
-31
lines changed

5 files changed

+107
-31
lines changed

robotcode/language_server/robotframework/parts/folding_range.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ def __init__(self, parent: RobotFoldingRangeProtocolPart) -> None:
3232
super().__init__()
3333
self.parent = parent
3434
self.line_folding_only = True
35-
if self.parent.parent.client_capabilities is not None:
36-
if self.parent.parent.client_capabilities.text_document is not None:
37-
if self.parent.parent.client_capabilities.text_document.folding_range is not None:
38-
if (
39-
self.parent.parent.client_capabilities.text_document.folding_range.line_folding_only
40-
is not None
41-
):
42-
self.line_folding_only = (
43-
self.parent.parent.client_capabilities.text_document.folding_range.line_folding_only
44-
)
35+
if (
36+
self.parent.parent.client_capabilities
37+
and self.parent.parent.client_capabilities.text_document
38+
and self.parent.parent.client_capabilities.text_document.folding_range
39+
and self.parent.parent.client_capabilities.text_document.folding_range.line_folding_only is not None
40+
):
41+
self.line_folding_only = (
42+
self.parent.parent.client_capabilities.text_document.folding_range.line_folding_only
43+
)
4544

4645
self.result: List[FoldingRange] = []
4746

@@ -98,7 +97,7 @@ async def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
9897
self.__append(node, kind="keyword")
9998
await self.generic_visit(node)
10099

101-
async def visit_ForLoop(self, node: ast.AST) -> None: # noqa: N802
100+
async def visit_ForLoop(self, node: ast.AST) -> None: # noqa: N802, pragma: no cover
102101
self.__append(node, kind="for_loop")
103102
await self.generic_visit(node)
104103

tests/robotcode/language_server/common/test_text_document.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,3 @@ async def get_data(self, document: TextDocument, data: str) -> str:
224224
del dummy
225225

226226
assert len(document._cache) == 0
227-
228-
229-
@pytest.mark.asyncio
230-
async def test_document_get_set_cache_with_lock_work() -> None:
231-
text = """\
232-
first
233-
second
234-
third
235-
"""
236-
prefix = "1"
237-
238-
async def get_data(document: TextDocument, data: str) -> str:
239-
await document.remove_cache_entry(get_data)
240-
return prefix + data
241-
242-
document = TextDocument(document_uri="file://test.robot", language_id="robotframework", version=1, text=text)
243-
244-
assert await document.get_cache(get_data, "data") == "1data"

tests/robotcode/language_server/robotframework/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from robotcode.language_server.common.lsp_types import (
99
ClientCapabilities,
1010
ClientInfo,
11+
FoldingRangeClientCapabilities,
1112
HoverClientCapabilities,
1213
InitializedParams,
1314
MarkupKind,
@@ -45,15 +46,18 @@ def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
4546

4647

4748
@pytest.fixture(scope="function")
48-
async def protocol(event_loop: asyncio.AbstractEventLoop) -> AsyncGenerator[RobotLanguageServerProtocol, None]:
49+
async def protocol(
50+
event_loop: asyncio.AbstractEventLoop, request: Any
51+
) -> AsyncGenerator[RobotLanguageServerProtocol, None]:
4952
root_path = Path().resolve()
5053
server = RobotLanguageServer()
5154
try:
5255
protocol = RobotLanguageServerProtocol(server)
5356
await protocol._initialize(
5457
ClientCapabilities(
5558
text_document=TextDocumentClientCapabilities(
56-
hover=HoverClientCapabilities(content_format=[MarkupKind.MARKDOWN, MarkupKind.PLAINTEXT])
59+
hover=HoverClientCapabilities(content_format=[MarkupKind.MARKDOWN, MarkupKind.PLAINTEXT]),
60+
folding_range=FoldingRangeClientCapabilities(range_limit=0, line_folding_only=False),
5761
)
5862
),
5963
root_path=str(root_path),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
*** Settings ***
2+
#^ Settings Start: any(e for e in result if e.start_line == line and e.kind=='section')
3+
Documentation Hallo Welt
4+
... was geht
5+
6+
*** Test Cases ***
7+
#^ Variables End: any(e for e in result if e.end_line == line - 1 and e.kind=='section')
8+
#^ Test Cases Start: any(e for e in result if e.start_line == line and e.kind=='section')
9+
First
10+
#^ Testcase Start: any(e for e in result if e.start_line == line and e.kind=='testcase')
11+
Log Hello from testcase
12+
a keyword
13+
FOR ${i} IN 1 2 3
14+
#^ For Start: any(e for e in result if e.start_line == line and e.kind=='for')
15+
IF ${i}==1
16+
#^ If Start: any(e for e in result if e.start_line == line and e.kind=='if')
17+
Log "one"
18+
ELSE IF ${i}==2
19+
#^ If Start: any(e for e in result if e.start_line == line and e.kind=='if')
20+
Log "two"
21+
ELSE
22+
#^ If Start: any(e for e in result if e.start_line == line and e.kind=='if')
23+
Log "more then two"
24+
END
25+
#^ If End: any(e for e in result if e.end_line == line and e.kind=='if')
26+
27+
Log ${i}
28+
29+
END
30+
#^ For End: any(e for e in result if e.end_line == line and e.kind=='for')
31+
32+
*** Keywords ***
33+
#^ Test Cases End: any(e for e in result if e.end_line == line - 1 and e.kind=='section')
34+
#^ Testcase End: any(e for e in result if e.end_line == line - 1 and e.kind=='testcase')
35+
a keyword
36+
#^ Keyword Start: any(e for e in result if e.start_line == line and e.kind=='keyword')
37+
Log Hello from keyword
38+
39+
*** Comments ***
40+
#^ Keyword End: any(e for e in result if e.end_line == line - 1 and e.kind=='keyword')
41+
#^ Comment Start: any(e for e in result if e.start_line == line and e.kind=='comment')
42+
this is a long long
43+
long long long
44+
long long comment section
45+
46+
#^ Comment End: any(e for e in result if e.end_line == line + 1 and e.kind=='comment')
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import re
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from robotcode.language_server.common.lsp_types import FoldingRange
7+
from robotcode.language_server.common.text_document import TextDocument
8+
from robotcode.language_server.robotframework.protocol import (
9+
RobotLanguageServerProtocol,
10+
)
11+
12+
from ..tools import (
13+
GeneratedTestData,
14+
generate_test_id,
15+
generate_tests_from_source_document,
16+
)
17+
18+
19+
@pytest.mark.parametrize(
20+
("test_document", "data"),
21+
generate_tests_from_source_document(Path(Path(__file__).parent, "data/foldingrange.robot")),
22+
indirect=["test_document"],
23+
ids=generate_test_id,
24+
)
25+
@pytest.mark.asyncio
26+
async def test_foldingrange(
27+
protocol: RobotLanguageServerProtocol,
28+
test_document: TextDocument,
29+
data: GeneratedTestData,
30+
) -> None:
31+
32+
result = await protocol._robot_folding_ranges.collect(protocol._robot_goto, test_document)
33+
34+
assert bool(
35+
eval(
36+
data.expression,
37+
{
38+
"re": re,
39+
"FoldingRange": FoldingRange,
40+
"result": result,
41+
"line": data.line,
42+
"character": data.character,
43+
},
44+
)
45+
), f"{data.expression} -> {repr(result)}"

0 commit comments

Comments
 (0)