コード例 #1
0
def test_variables_completions_integrated(language_server_tcp, ws_root_path,
                                          data_regression):
    from robotframework_ls.workspace import Document

    language_server = language_server_tcp
    language_server.initialize(ws_root_path, process_id=os.getpid())
    uri = "untitled:Untitled-1"
    language_server.open_doc(uri, 1)
    contents = """
*** Variables ***
${NAME}         Robot Framework
${VERSION}      2.0
${ROBOT}        ${NAME} ${VERSION}

*** Test Cases ***
List Variable
    Log    ${NAME}
    Should Contain    ${"""
    language_server.change_doc(uri, 2, contents)

    doc = Document("", source=contents)
    line, col = doc.get_last_line_col()
    completions = language_server.get_completions(uri, line, col)
    del completions["id"]
    data_regression.check(completions, "variable_completions")
コード例 #2
0
def test_formatting_basic(data_regression):
    from robotframework_ls.impl.formatting import (
        robot_source_format,
        create_text_edit_from_diff,
    )
    from robotframework_ls.workspace import Document

    contents = u"""
***Settings***
[Documentation]Some doc

***Test Case***
Check
    Call  1  2"""
    new_contents = robot_source_format(contents)
    assert u"*** Settings ***" in new_contents

    text_edits = create_text_edit_from_diff(contents, new_contents)

    doc = Document("", contents)
    doc.apply_text_edits(text_edits)
    data_regression.check(doc.source, basename="test_formatting_basic_formatted_doc")
    data_regression.check(
        [x.to_dict() for x in text_edits], basename="test_formatting_basic_text_edits"
    )
コード例 #3
0
def test_document_multiline_edit():
    old = ["def hello(a, b):\n", "    print a\n", "    print b\n"]
    doc = Document("file:///uri", u"".join(old))
    change = TextDocumentContentChangeEvent(
        Range(Position(1, 4), Position(2, 11)), 0, u"print a, b")
    doc.apply_change(change)

    assert doc.get_internal_lines() == ("def hello(a, b):\n",
                                        "    print a, b\n")
コード例 #4
0
def test_document_end_of_file_edit():
    old = ["print 'a'\n", "print 'b'\n"]
    doc = Document("file:///uri", u"".join(old))

    change = TextDocumentContentChangeEvent(
        Range(Position(2, 0), Position(2, 0)), 0, u"o")
    doc.apply_change(change)

    assert doc.get_internal_lines() == ("print 'a'\n", "print 'b'\n", "o")
コード例 #5
0
def create_text_edit_from_diff(contents, new_contents):
    from difflib import SequenceMatcher
    from robotframework_ls.lsp import TextEdit
    from robotframework_ls.workspace import Document

    d = Document("", contents)

    s = SequenceMatcher(None, contents, new_contents)
    lst = []
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        # print(
        #     "%7s a[%d:%d] (%s) b[%d:%d] (%s)"
        #     % (tag, i1, i2, contents[i1:i2], j1, j2, new_contents[j1:j2])
        # )

        if tag in ("replace", "insert"):
            lst.append(TextEdit(_create_range(d, i1, i2), new_contents[j1:j2]))

        elif tag == "delete":
            lst.append(TextEdit(_create_range(d, i1, i2), ""))

        elif tag == "equal":
            pass

        else:
            raise AssertionError("Unhandled: %s" % (tag, ))

    return lst
コード例 #6
0
def test_offset_to_line_col_1():
    d = Document(uri="", source="my\nfo\nba")
    assert d.offset_to_line_col(0) == (0, 0)
    assert d.offset_to_line_col(1) == (0, 1)
    assert d.offset_to_line_col(2) == (0, 2)

    assert d.offset_to_line_col(3) == (1, 0)
    assert d.offset_to_line_col(4) == (1, 1)
    assert d.offset_to_line_col(5) == (1, 2)

    assert d.offset_to_line_col(6) == (2, 0)
    assert d.offset_to_line_col(7) == (2, 1)
    assert d.offset_to_line_col(8) == (2, 2)

    # Note: block below is out of bounds
    assert d.offset_to_line_col(9) == (2, 3)
    assert d.offset_to_line_col(10) == (2, 4)
コード例 #7
0
def test_get_last_line_col():
    d = Document(uri="", source="")
    assert d.get_last_line_col() == (0, 0)
    d.source = "my"
    assert d.get_last_line_col() == (0, 2)
    d.source = "my\n"
    assert d.get_last_line_col() == (1, 0)
コード例 #8
0
def test_offset_to_line_col_2():
    d = Document(uri="", source="\n\n\n")
    with pytest.raises(ValueError):
        assert d.offset_to_line_col(-1)

    assert d.offset_to_line_col(0) == (0, 0)
    assert d.offset_to_line_col(1) == (1, 0)
    assert d.offset_to_line_col(2) == (2, 0)

    # Note: block below is out of bounds
    assert d.offset_to_line_col(3) == (3, 0)
    assert d.offset_to_line_col(4) == (3, 1)
コード例 #9
0
 def _clear_caches(self):
     Document._clear_caches(self)
     self.get_ast.cache_clear(self)
コード例 #10
0
 def __init__(self, uri, source=None, version=None, generate_ast=True):
     Document.__init__(self, uri, source=source, version=version)
     self._generate_ast = generate_ast
     self._ast = None
コード例 #11
0
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))
コード例 #12
0
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"
コード例 #13
0
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"
コード例 #14
0
def doc():
    return Document(DOC_URI, DOC)
コード例 #15
0
def test_get_line():
    d = Document(uri="", source="")
    assert d.get_last_line() == ""
    d.source = "my\nfoo"
    assert d.get_line(0) == "my"
    assert d.get_last_line() == "foo"
    assert d.get_line_count() == 2

    d.source = "my\nfoo\n"
    assert d.get_line(0) == "my"
    assert d.get_line(1) == "foo"
    assert d.get_line(2) == ""
    assert d.get_last_line() == ""

    assert list(d.iter_lines()) == ["my\n", "foo\n", ""]
    assert list(d.iter_lines(False)) == ["my", "foo", ""]