Skip to content

Commit

Permalink
Refactor the completion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Théophane Hufschmitt committed Jun 19, 2024
1 parent 2f68efa commit a16b342
Showing 1 changed file with 39 additions and 42 deletions.
81 changes: 39 additions & 42 deletions tests/lsp/test_nls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def open_file(self, client: LanguageClient, file_path: str, file_content: Option
)
return file_uri

async def complete(self, client: LanguageClient, file_uri: str, pos: lsp.Position):
"""
Trigger an autocompletion in the given file at the given position
"""
results = await client.text_document_completion_async(
params=lsp.CompletionParams(
text_document=lsp.TextDocumentIdentifier(file_uri),
position=pos,
)
)
assert results is not None

if isinstance(results, lsp.CompletionList):
items = results.items
else:
items = results
return items

@pytest.mark.asyncio
async def test_completion_at_toplevel(self, client):
"""
Expand All @@ -71,22 +89,15 @@ async def test_completion_at_toplevel(self, client):

test_uri = self.open_file(client, test_file, test_file_content)

results = await client.text_document_completion_async(
params=lsp.CompletionParams(
text_document=lsp.TextDocumentIdentifier(uri=test_uri),
position=lsp.Position(line=12, character=0), # Empty line in the `config` record
)
completion_items = await self.complete(
client,
test_uri,
lsp.Position(line=12, character=0) # Empty line in the `config` record
)
assert results is not None

if isinstance(results, lsp.CompletionList):
items = results.items
else:
items = results

labels = [item.label for item in items]
labels = [item.label for item in completion_items]
assert "files" in labels
files_item = [item for item in items if item.label == "files"][0]
files_item = [item for item in completion_items if item.label == "files"][0]
assert files_item.documentation.value != ""

@pytest.mark.asyncio
Expand All @@ -109,23 +120,15 @@ async def test_completion_sub_field(self, client: LanguageClient):
| organist.modules.T
"""
test_uri = self.open_file(client, test_file, test_file_content)

results = await client.text_document_completion_async(
params=lsp.CompletionParams(
text_document=lsp.TextDocumentIdentifier(uri=test_uri),
position=lsp.Position(line=8, character=17), # The `c` in `files.foo.c`
)
completion_items = await self.complete(
client,
test_uri,
lsp.Position(line=8, character=17) # The `c` in `files.foo.c`
)
assert results is not None

if isinstance(results, lsp.CompletionList):
items = results.items
else:
items = results

labels = [item.label for item in items]
labels = [item.label for item in completion_items]
assert "content" in labels
content_item = [item for item in items if item.label == "content"][0]
content_item = [item for item in completion_items if item.label == "content"][0]
assert content_item.documentation.value != ""

@pytest.mark.asyncio
Expand All @@ -148,21 +151,15 @@ async def test_completion_with_custom_module(self, client: LanguageClient):
| organist.modules.T
"""
test_uri = self.open_file(client, test_file, test_file_content)

results = await client.text_document_completion_async(
params=lsp.CompletionParams(
text_document=lsp.TextDocumentIdentifier(uri=test_uri),
position=lsp.Position(line=8, character=0), # empty line in the `config` record
)
completion_items = await self.complete(
client,
test_uri,
lsp.Position(line=8, character=0) # Empty line in the `config` record
)
assert results is not None

if isinstance(results, lsp.CompletionList):
items = results.items
else:
items = results

labels = [item.label for item in items]
labels = [item.label for item in completion_items]
assert "direnv" in labels
content_item = [item for item in items if item.label == "direnv"][0]
# assert content_item.documentation.value != "" ## No documentation for direnv yet

## No documentation for direnv yet
# content_item = [item for item in completion_items if item.label == "direnv"][0]
# assert content_item.documentation.value != ""

0 comments on commit a16b342

Please sign in to comment.