Skip to content

Commit

Permalink
Add some tests around ClientCapabilities
Browse files Browse the repository at this point in the history
As far as I can tell, there are no tests that depend on any of the
values contained within `ClientCapabilities`. This commit adds some
tests around the construction of the `TextDocumentSyncOptions` field for
the server's capabilities.

It also fixes a bug that was introduced in the previous commit
  • Loading branch information
alcarney authored and tombh committed Oct 31, 2022
1 parent 8927328 commit af21a78
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pygls/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def get_capability(
return default

# If we reach the desired leaf value but it's None, return the default.
value = default if value is None else value
return
return default if value is None else value


class ServerCapabilitiesBuilder:
Expand Down
136 changes: 136 additions & 0 deletions tests/test_capabilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from typing import Set

import pytest
from lsprotocol.types import (
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DID_CLOSE,
TEXT_DOCUMENT_DID_SAVE,
TEXT_DOCUMENT_WILL_SAVE,
TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL,
ClientCapabilities,
SaveOptions,
TextDocumentClientCapabilities,
TextDocumentSaveRegistrationOptions,
TextDocumentSyncClientCapabilities,
TextDocumentSyncKind,
TextDocumentSyncOptions,
)

from pygls.capabilities import ServerCapabilitiesBuilder


@pytest.mark.parametrize("capabilities,features,options,expected", [
# textDocument/didOpen
(
ClientCapabilities(),
set(),
{},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=False,
)
),
(
ClientCapabilities(),
{TEXT_DOCUMENT_DID_OPEN},
{},
TextDocumentSyncOptions(
open_close=True,
change=TextDocumentSyncKind.Incremental,
save=False,
)
),
(
ClientCapabilities(),
{TEXT_DOCUMENT_DID_CLOSE},
{},
TextDocumentSyncOptions(
open_close=True,
change=TextDocumentSyncKind.Incremental,
save=False,
)
),
# textDocument/willSave & textDocument/willSaveWaitUntil
(
ClientCapabilities(),
{TEXT_DOCUMENT_WILL_SAVE},
{},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=False,
)
),
(
ClientCapabilities(
text_document=TextDocumentClientCapabilities(
synchronization=TextDocumentSyncClientCapabilities(
will_save=True
)
)
),
{TEXT_DOCUMENT_WILL_SAVE},
{},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=False,
will_save=True
)
),
(
ClientCapabilities(
text_document=TextDocumentClientCapabilities(
synchronization=TextDocumentSyncClientCapabilities(
will_save_wait_until=True
)
)
),
{TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL},
{},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=False,
will_save_wait_until=True
)
),
# textDocument/didSave
(
ClientCapabilities(),
{TEXT_DOCUMENT_DID_SAVE},
{},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=True,
)
),
(
ClientCapabilities(),
{TEXT_DOCUMENT_DID_SAVE},
{TEXT_DOCUMENT_DID_SAVE: TextDocumentSaveRegistrationOptions(include_text=True)},
TextDocumentSyncOptions(
open_close=False,
change=TextDocumentSyncKind.Incremental,
save=SaveOptions(include_text=True),
)
)
])
def test_text_doc_sync_capabilities(
capabilities: ClientCapabilities,
features: Set[str],
options,
expected: TextDocumentSyncOptions
):
"""Ensure that `pygls` can correctly construct server capabilities for the
text document sync features."""

builder = ServerCapabilitiesBuilder(
capabilities, features, options, [], TextDocumentSyncKind.Incremental
)

actual = builder.build().text_document_sync
assert expected == actual

0 comments on commit af21a78

Please sign in to comment.