""" TEST_TOOL_01 = """<tool id="test" name="Test Tool 01" version="0.1.0"> <command detect_errors="exit_code"><![CDATA[ TODO: Fill in command template. ]]></command> <inputs> </inputs> <outputs> </outputs> <help><![CDATA[ TODO: Fill in help. ]]></help> </tool> """ TEST_TOOL_01_DOCUMENT = Document("file://test01.xml", TEST_TOOL_01) TEST_TOOL_WITH_MACRO_01 = """ <tool id="test_with_macro" name="Test with macro 01" version="@WRAPPER_VERSION@"> <macros> <import>macros.xml</import> </macros> <expand macro="inputs" /> </tool>""" TEST_TOOL_WITH_MACRO_01_DOCUMENT = Document("file://test_with_macro_01.xml", TEST_TOOL_WITH_MACRO_01) TEST_MACRO_01 = """ <macros> <token name="@WRAPPER_VERSION@">0.1.0</token> <macro name="inputs"> <inputs/>
def test_document_empty_edit(): doc = Document('file:///uri', u'') change = TextDocumentContentChangeEvent( Range(Position(0, 0), Position(0, 0)), 0, u'f') doc.apply_change(change) assert doc.source == u'f'
def test_document_line_edit(): doc = Document('file:///uri', u'itshelloworld') change = TextDocumentContentChangeEvent( Range(Position(0, 3), Position(0, 8)), 0, u'goodbye') doc.apply_change(change) assert doc.source == u'itsgoodbyeworld'
def doc(): return Document('uri', 'source')
def test_document_source_unicode(): document_mem = Document(DOC_URI, u'my source') document_disk = Document(DOC_URI) assert isinstance(document_mem.source, type(document_disk.source))
from ...server import completions, did_close, did_open class FakeServer(): """We don't need real server to unit test features.""" publish_diagnostics = None show_message = None show_message_log = None def __init__(self): self.workspace = Workspace('', None) fake_document_uri = 'file://fake_doc.txt' fake_document_content = 'text' fake_document = Document(fake_document_uri, fake_document_content) server = FakeServer() server.publish_diagnostics = Mock() server.show_message = Mock() server.show_message_log = Mock() server.workspace.get_document = Mock(return_value=fake_document) def _reset_mocks(): server.publish_diagnostics.reset_mock() server.show_message.reset_mock() server.show_message_log.reset_mock() def test_completions():
def doc(): return Document(DOC_URI, DOC)
def didopen(params: DidOpenTextDocumentParams): uri = params.textDocument.uri txt[uri] = Document(uri) with compiler: compiler.compile(uri, params.textDocument.text)
def completion_test(feature, text: str, expected: Optional[Set[str]], unexpected: Optional[Set[str]]): """Check to see if a feature provides the correct completion suggestions. **Only checking CompletionItem labels is supported** This function takes the given ``feature`` and calls it in the same manner as the real language server so that it can simulate real usage without being a full blown integration test. This requires ``suggest_triggers`` to be set and it to have a working ``suggest`` method. Completions will be asked for with the cursor's position to be at the end of the inserted ``text`` in a blank document by default. If your test case requires additional context this can be included in ``text`` delimited by a ``\\f`` character. For example to pass text representing the following scenario (``^`` represents the user's cursor):: .. image:: filename.png :align: center : ^ The ``text`` parameter should be set to ``.. image:: filename.png\\n :align: center\\n\\f :``. It's important to note that newlines **cannot** come after the ``\\f`` character. If you want to test the case where no completions should be suggested, pass ``None`` to both the ``expected`` and ``unexpected`` parameters. Parameters ---------- feature: An instance of the language service feature to test. text: The text to offer completion suggestions for. expected: The set of completion item labels you expect to see in the output. unexpected: The set of completion item labels you do *not* expect to see in the output. """ if "\f" in text: contents, text = text.split("\f") else: contents = "" logger.debug("Context text: '%s'", contents) logger.debug("Insertsion text: '%s'", text) assert "\n" not in text, "Insertion text cannot contain newlines" document = Document("file:///test_doc.rst", contents) position = Position(len(document.lines), len(text) - 1) results = [] for trigger in feature.suggest_triggers: match = trigger.match(text) logger.debug("Match: %s", match) if match: results += feature.suggest(match, document, position) items = {item.label for item in results} logger.debug("Results: %s", items) logger.debug("Expected: %s", expected) logger.debug("Unexpected: %s", unexpected) if expected is None: assert len(items) == 0 else: assert expected == items & expected assert set() == items & unexpected
from mock import Mock from pygls.types import (DidCloseTextDocumentParams, DidOpenTextDocumentParams, TextDocumentIdentifier, TextDocumentItem) from pygls.workspace import Document, Workspace from ...server import completions, did_close, did_open class FakeServer(): """We don't need real server to unit test features.""" def __init__(self): self.workspace = Workspace('', None) fake_document_uri = 'file://fake_doc.txt' fake_document = Document(fake_document_uri, '') server = FakeServer() server.publish_diagnostics = Mock() server.show_message = Mock() server.show_message_log = Mock() server.workspace.get_document = Mock(return_value=fake_document) def _reset_mocks(): server.publish_diagnostics.reset_mock() server.show_message.reset_mock() server.show_message_log.reset_mock() def test_completions():