def test_definitions(config):
    # Over 'a' in print a
    cursor_pos = {'line': 3, 'character': 6}

    # The definition of 'a'
    def_range = {
        'start': {
            'line': 0,
            'character': 4
        },
        'end': {
            'line': 0,
            'character': 5
        }
    }

    doc = Document(DOC_URI, DOC)
    assert [{
        'uri': DOC_URI,
        'range': def_range
    }] == pyls_definitions(config, doc, cursor_pos)
def test_rope_rename(tmp_workspace, config):  # pylint: disable=redefined-outer-name
    position = {"line": 0, "character": 6}
    DOC_URI = uris.from_fs_path(os.path.join(tmp_workspace.root_path, DOC_NAME))
    doc = Document(DOC_URI, tmp_workspace)

    result = pyls_rename(config, tmp_workspace, doc, position, "ShouldBeRenamed")
    assert len(result.keys()) == 1

    changes = result.get("documentChanges")
    assert len(changes) == 1
    changes = changes[0]

    assert changes.get("edits") == [
        {
            "range": {
                "start": {"line": 0, "character": 0},
                "end": {"line": 5, "character": 0},
            },
            "newText": "class ShouldBeRenamed():\n    pass\n\nclass Test2(ShouldBeRenamed):\n    pass\n",
        }
    ]
Ejemplo n.º 3
0
def test_symbols_all_scopes(config):
    doc = Document(DOC_URI, DOC)
    symbols = pyls_document_symbols(config, doc)

    # All eight symbols (import sys, a, B, __init__, x, y, main, y)
    assert len(symbols) == 8

    def sym(name):
        return [s for s in symbols if s['name'] == name][0]

    # Check we have some sane mappings to VSCode constants
    assert sym('a')['kind'] == SymbolKind.Variable
    assert sym('B')['kind'] == SymbolKind.Class
    assert sym('__init__')['kind'] == SymbolKind.Function
    assert sym('main')['kind'] == SymbolKind.Function

    # Not going to get too in-depth here else we're just testing Jedi
    assert sym('a')['location']['range']['start'] == {
        'line': 2,
        'character': 0
    }
def test_document_path_completions(tmpdir, workspace_other_root_path):
    # Create a dummy module out of the workspace's root_path and try to get
    # completions for it in another file placed next to it.
    module_content = '''
def foo():
    pass
'''

    p = tmpdir.join("mymodule.py")
    p.write(module_content)

    # Content of doc to test completion
    doc_content = """import mymodule
mymodule.f"""
    doc_path = str(tmpdir) + os.path.sep + 'myfile.py'
    doc_uri = uris.from_fs_path(doc_path)
    doc = Document(doc_uri, workspace_other_root_path, doc_content)

    com_position = {'line': 1, 'character': 10}
    completions = pyls_jedi_completions(doc._config, doc, com_position)
    assert completions[0]['label'] == 'foo()'
Ejemplo n.º 5
0
def test_assignment(config):
    # Over 's' in self.members[id]
    cursor_pos = {'line': 11, 'character': 19}

    # The assignment of 'self.members'
    def_range = {
        'start': {
            'line': 8,
            'character': 13
        },
        'end': {
            'line': 8,
            'character': 20
        }
    }

    doc = Document(DOC_URI, DOC)
    assert [{
        'uri': DOC_URI,
        'range': def_range
    }] == pyls_definitions(config, doc, cursor_pos)
Ejemplo n.º 6
0
def test_cell_block_symbols(config, workspace):
    document = Document(DOC_URI, workspace, DOC)
    symbols = pyls_document_symbols(config, workspace, document)
    expected = [
        ('Unnamed cell 1', 1, 22, 225), ('Imports', 2, 2, 224),
        ('Other cell', 6, 6, 225), ('Unnamed comment 1', 7, 7, 224),
        ('Block comment on a', 9, 9, 224), ('Cell inside a', 10, 14, 225),
        ('Cell', 12, 14, 225), ('Unnamed cell 2', 15, 22, 225),
        ('Pass inside b', 17, 17, 224), ('25', 20, 20, 225),
        ('Unnamed comment 2', 21, 21, 224), ('Empty cell', 23, 24, 225),
        ('Unnamed comment 3', 24, 24, 224)
    ]
    test_results = []
    for symbol in symbols:
        name = symbol['name']
        location = symbol['location']
        sym_range = location['range']
        start = sym_range['start']['line']
        end = sym_range['end']['line']
        kind = symbol['kind']
        test_results.append((name, start, end, kind))
    assert expected == test_results
Ejemplo n.º 7
0
def test_pycodestyle(config):
    doc = Document(DOC_URI, DOC)
    diags = pycodestyle_lint.pyls_lint(config, doc)

    assert all([d['source'] == 'pycodestyle' for d in diags])

    # One we're expecting is:
    msg = 'W191 indentation contains tabs'
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'W191'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 3, 'character': 0}
    assert mod_import['range']['end'] == {'line': 3, 'character': 6}

    msg = 'W391 blank line at end of file'
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'W391'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 7, 'character': 0}
    assert mod_import['range']['end'] == {'line': 7, 'character': 1}
Ejemplo n.º 8
0
def test_references_builtin(tmp_workspace):  # pylint: disable=redefined-outer-name
    # Over 'UnicodeError':
    position = {'line': 4, 'character': 7}
    doc2_uri = uris.from_fs_path(
        os.path.join(str(tmp_workspace.root_path), DOC2_NAME))
    doc2 = Document(doc2_uri, tmp_workspace)

    refs = pyls_references(doc2, position)
    assert len(refs) >= 1

    expected = {
        'start': {
            'line': 4,
            'character': 7
        },
        'end': {
            'line': 4,
            'character': 19
        }
    }
    ranges = [r['range'] for r in refs]
    assert expected in ranges
Ejemplo n.º 9
0
def test_symbols(config):
    doc = Document(DOC_URI, DOC)
    config.update({'plugins': {'jedi_symbols': {'all_scopes': False}}})
    symbols = pyls_document_symbols(config, doc)

    # All four symbols (import sys, a, B, main)
    assert len(symbols) == 4

    def sym(name):
        return [s for s in symbols if s['name'] == name][0]

    # Check we have some sane mappings to VSCode constants
    assert sym('sys')['kind'] == SymbolKind.Module
    assert sym('a')['kind'] == SymbolKind.Variable
    assert sym('B')['kind'] == SymbolKind.Class
    assert sym('main')['kind'] == SymbolKind.Function

    # Not going to get too in-depth here else we're just testing Jedi
    assert sym('a')['location']['range']['start'] == {
        'line': 2,
        'character': 0
    }
def test_pycodestyle(config, workspace):
    doc = Document(DOC_URI, workspace, DOC)
    diags = pycodestyle_lint.pyls_lint(config, doc)

    assert all([d['source'] == 'pycodestyle' for d in diags])

    # One we're expecting is:
    msg = 'W191 indentation contains tabs'
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'W191'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 3, 'character': 0}
    assert mod_import['range']['end'] == {'line': 3, 'character': 6}

    msg = 'W391 blank line at end of file'
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'W391'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 10, 'character': 0}
    assert mod_import['range']['end'] == {'line': 10, 'character': 1}

    msg = "E201 whitespace after '('"
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'E201'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 2, 'character': 10}
    assert mod_import['range']['end'] == {'line': 2, 'character': 14}

    msg = "E128 continuation line under-indented for visual indent"
    mod_import = [d for d in diags if d['message'] == msg][0]

    assert mod_import['code'] == 'E128'
    assert mod_import['severity'] == lsp.DiagnosticSeverity.Warning
    assert mod_import['range']['start'] == {'line': 5, 'character': 1}
    assert mod_import['range']['end'] == {'line': 5, 'character': 10}
def test_jedi_method_completion(config, workspace):
    # Over the 'y' in 'print Hello().every'
    com_position = {'line': 20, 'character': 19}
    doc = Document(DOC_URI, workspace, DOC)

    config.capabilities['textDocument'] = {'completion': {'completionItem': {'snippetSupport': True}}}
    config.update({'plugins': {'jedi_completion': {'include_params': True}}})

    completions = pyls_jedi_completions(config, doc, com_position)
    everyone_method = [completion for completion in completions if completion['label'] == 'everyone(a, b, c, d)'][0]

    # Ensure we only generate snippets for positional args
    assert everyone_method['insertTextFormat'] == lsp.InsertTextFormat.Snippet
    assert everyone_method['insertText'] == 'everyone(${1:a}, ${2:b})$0'

    # Disable param snippets
    config.update({'plugins': {'jedi_completion': {'include_params': False}}})

    completions = pyls_jedi_completions(config, doc, com_position)
    everyone_method = [completion for completion in completions if completion['label'] == 'everyone(a, b, c, d)'][0]

    assert 'insertTextFormat' not in everyone_method
    assert everyone_method['insertText'] == 'everyone'
Ejemplo n.º 12
0
def test_pydocstyle(config):
    doc = Document(DOC_URI, DOC)
    diags = pydocstyle_lint.pyls_lint(config, doc)

    assert all([d['source'] == 'pydocstyle' for d in diags])

    # One we're expecting is:
    assert diags[0] == {
        'code': 'D100',
        'message': 'D100: Missing docstring in public module',
        'severity': lsp.DiagnosticSeverity.Warning,
        'range': {
            'start': {
                'line': 0,
                'character': 0
            },
            'end': {
                'line': 0,
                'character': 11
            },
        },
        'source': 'pydocstyle'
    }
Ejemplo n.º 13
0
def test_folding(workspace):
    doc = Document(DOC_URI, workspace, DOC)
    ranges = pyls_folding_range(doc)
    expected = [{'startLine': 1, 'endLine': 6},
                {'startLine': 2, 'endLine': 3},
                {'startLine': 5, 'endLine': 6},
                {'startLine': 8, 'endLine': 11},
                {'startLine': 12, 'endLine': 20},
                {'startLine': 13, 'endLine': 14},
                {'startLine': 15, 'endLine': 16},
                {'startLine': 17, 'endLine': 18},
                {'startLine': 19, 'endLine': 20},
                {'startLine': 22, 'endLine': 35},
                {'startLine': 23, 'endLine': 35},
                {'startLine': 24, 'endLine': 25},
                {'startLine': 27, 'endLine': 29},
                {'startLine': 28, 'endLine': 29},
                {'startLine': 30, 'endLine': 31},
                {'startLine': 32, 'endLine': 34},
                {'startLine': 33, 'endLine': 34},
                {'startLine': 38, 'endLine': 39},
                {'startLine': 41, 'endLine': 43},
                {'startLine': 42, 'endLine': 43},
                {'startLine': 45, 'endLine': 54},
                {'startLine': 47, 'endLine': 51},
                {'startLine': 49, 'endLine': 51},
                {'startLine': 50, 'endLine': 51},
                {'startLine': 52, 'endLine': 54},
                {'startLine': 53, 'endLine': 54},
                {'startLine': 56, 'endLine': 57},
                {'startLine': 59, 'endLine': 65},
                {'startLine': 60, 'endLine': 61},
                {'startLine': 62, 'endLine': 63},
                {'startLine': 64, 'endLine': 65},
                {'startLine': 67, 'endLine': 68}]
    assert ranges == expected
Ejemplo n.º 14
0
def test_no_change():
    doc = Document(DOC_URI, GOOD_DOC)
    assert not pyls_format_document(doc)
Ejemplo n.º 15
0
def doc():
    return Document(DOC_URI, DOC)
Ejemplo n.º 16
0
def config_document(workspace):
    path = fixtures_dir / "config" / "config.txt"
    uri = f"file:/{path}"
    return Document(uri, workspace)
Ejemplo n.º 17
0
def invalid_document(workspace):
    path = fixtures_dir / "invalid.txt"
    uri = f"file:/{path}"
    return Document(uri, workspace)
Ejemplo n.º 18
0
def formatted_pyi_document(workspace):
    path = fixtures_dir / "formatted.pyi"
    uri = f"file:/{path}"
    return Document(uri, workspace)
Ejemplo n.º 19
0
def unformatted_document(workspace):
    path = fixtures_dir / "unformatted.txt"
    uri = f"file:/{path}"
    return Document(uri, workspace)
Ejemplo n.º 20
0
def test_parse_line_without_line(workspace):
    doc = Document(DOC_URI, workspace)
    diag = plugin.parse_line(TEST_LINE_WITHOUT_LINE, doc)
    assert diag['message'] == '"Request" has no attribute "id"'
    assert diag['range']['start'] == {'line': 0, 'character': 0}
    assert diag['range']['end'] == {'line': 0, 'character': 6}
Ejemplo n.º 21
0
def test_rope_import_completion(config, workspace):
    com_position = {'line': 0, 'character': 7}
    doc = Document(DOC_URI, DOC)
    items = pyls_rope_completions(config, workspace, doc, com_position)
    assert items is None
Ejemplo n.º 22
0
def doc(workspace):  # pylint: disable=redefined-outer-name
    return Document(DOC_URI, workspace, DOC)
Ejemplo n.º 23
0
def test_pydocstyle_empty_source():
    doc = Document(DOC_URI, "")
    diags = pydocstyle_lint.pyls_lint(doc)
    assert diags[0]['message'] == 'D100: Missing docstring in public module'
    assert len(diags) == 1
Ejemplo n.º 24
0
def test_format():
    doc = Document(DOC_URI, DOC)
    res = pyls_format_document(doc)

    assert len(res) == 1
    assert res[0]['newText'] == "A = ['h', 'w', 'a']\n\nB = ['h', 'w']\n"
Ejemplo n.º 25
0
def test_pydocstyle_invalid_source():
    doc = Document(DOC_URI, "bad syntax")
    diags = pydocstyle_lint.pyls_lint(doc)
    # We're unable to parse the file, so can't get any pydocstyle diagnostics
    assert not diags
Ejemplo n.º 26
0
def unformatted_pyi_document():
    path = fixtures_dir / "unformatted.pyi"
    uri = f"file:/{path}"
    return Document(uri)
Ejemplo n.º 27
0
def test_document_source_unicode(workspace):
    document_mem = Document(DOC_URI, workspace, u'my source')
    document_disk = Document(DOC_URI, workspace)
    assert isinstance(document_mem.source, type(document_disk.source))
Ejemplo n.º 28
0
def formatted_document():
    path = fixtures_dir / "formatted.txt"
    uri = f"file:/{path}"
    return Document(uri)
Ejemplo n.º 29
0
def test_symbols_all_scopes(config, workspace):
    doc = Document(DOC_URI, workspace, DOC)
    symbols = pyls_document_symbols(config, doc)
    helper_check_symbols_all_scope(symbols)
Ejemplo n.º 30
0
def test_parse_full_line(workspace):
    doc = Document(DOC_URI, workspace)
    diag = plugin.parse_line(TEST_LINE, doc)
    assert diag['message'] == '"Request" has no attribute "id"'
    assert diag['range']['start'] == {'line': 278, 'character': 7}
    assert diag['range']['end'] == {'line': 278, 'character': 8}