Exemple #1
0
def test_special_tokens(jedi_xontrib):
    assert jedi_xontrib.complete_jedi(CompletionContext(python=PythonContext("", 0))) \
        .issuperset(jedi_xontrib.XONSH_SPECIAL_TOKENS)
    assert jedi_xontrib.complete_jedi(
        CompletionContext(python=PythonContext("@", 1))) == {"@", "@(", "@$("}
    assert jedi_xontrib.complete_jedi(
        CompletionContext(python=PythonContext("$", 1))) == {"$[", "${", "$("}
Exemple #2
0
def test_complete_python(code, exp):
    res = complete_python(
        CompletionContext(python=PythonContext(code, len(code), ctx={}))
    )
    assert res and len(res) == 2
    comps, _ = res
    assert exp in comps
Exemple #3
0
def test_rich_completions(jedi_xontrib, jedi_mock, completion,
                          rich_completion):
    comp_type, comp_name, comp_complete, sig, inf = completion
    comp_mock = MagicMock()
    comp_mock.type = comp_type
    comp_mock.name = comp_name
    comp_mock.complete = comp_complete
    if sig:
        sig_mock = MagicMock()
        sig_mock.to_string.return_value = sig
        comp_mock.get_signatures.return_value = [sig_mock]
    else:
        comp_mock.get_signatures.return_value = []
    if inf:
        inf_type, inf_desc = inf
        inf_mock = MagicMock()
        inf_mock.type = inf_type
        inf_mock.description = inf_desc
        comp_mock.infer.return_value = [inf_mock]
    else:
        comp_mock.infer.return_value = []

    jedi_xontrib.XONSH_SPECIAL_TOKENS = []
    jedi_mock.Interpreter().complete.return_value = [comp_mock]
    completions = jedi_xontrib.complete_jedi(
        CompletionContext(python=PythonContext("", 0)))
    assert len(completions) == 1
    (ret_completion, ) = completions
    assert isinstance(ret_completion, RichCompletion)
    assert ret_completion == rich_completion
    assert ret_completion.display == rich_completion.display
    assert ret_completion.description == rich_completion.description
def test_empty_line():
    completions = complete_base(
        CompletionContext(command=CommandContext((), 0),
                          python=PythonContext("", 0)))
    assert completions
    for exp in ["cool", "abs"]:
        assert exp in completions
Exemple #5
0
def test_complete_import(command, exp):
    result = complete_import(
        CompletionContext(
            command,
            python=PythonContext("", 0)  # `complete_import` needs this
        ))
    if isinstance(result, tuple):
        result, _ = result
    result = set(result)
    assert result == exp
Exemple #6
0
def test_multiline(jedi_xontrib, jedi_mock, monkeypatch):
    complete_document = "xx = 1\n1 + x"
    jedi_xontrib.complete_jedi(
        CompletionContext(
            python=PythonContext(complete_document, len(complete_document))))

    assert jedi_mock.Interpreter.call_args_list[0][0][0] == complete_document
    assert jedi_mock.Interpreter().complete.call_args_list == [
        call(2, 5)  # line (one-indexed), column (zero-indexed)
    ]
Exemple #7
0
def test_complete_python_ctx():
    class A:
        def wow():
            pass
    
    a = A()

    res = complete_python(CompletionContext(python=PythonContext("a.w", 2, ctx=locals())))
    assert res and len(res) == 2
    comps, _ = res
    assert "a.wow(" in comps
def assert_match(
    commandline, command_context=MISSING, python_context=MISSING, is_main_command=False
):
    if X in commandline:
        index = commandline.index(X)
        commandline = commandline.replace(X, "")
    else:
        index = len(commandline)
    context = parse(commandline, index)
    if context is None:
        raise SyntaxError(
            "Failed to parse the commandline - set DEBUG = True in this file to see the error"
        )
    if is_main_command and python_context is MISSING:
        python_context = PythonContext(commandline, index)
    if command_context is not MISSING:
        assert context.command == command_context
    if python_context is not MISSING:
        assert context.python == python_context
Exemple #9
0
    spec = find_xontrib("jedi")
    yield importlib.import_module(spec.name)
    del sys.modules[spec.name]


def test_completer_added(jedi_xontrib, xession):
    assert "xontrib.jedi" in sys.modules
    assert "python" not in xession.completers
    assert "python_mode" not in xession.completers
    assert "jedi_python" in xession.completers


@pytest.mark.parametrize(
    "context",
    [
        CompletionContext(python=PythonContext("10 + x", 6)),
    ],
)
@pytest.mark.parametrize("version", ["new", "old"])
def test_jedi_api(jedi_xontrib, jedi_mock, version, context, xession):
    if version == "old":
        jedi_mock.__version__ = "0.15.0"
        jedi_mock.Interpreter().completions.return_value = []
        jedi_mock.reset_mock()

    jedi_xontrib.complete_jedi(context)

    extra_namespace = {"__xonsh__": xession}
    try:
        extra_namespace["_"] = _
    except NameError:
Exemple #10
0
def test_complete_import(command, exp):
    result = complete_import(CompletionContext(command,
        python=PythonContext("", 0)  # `complete_import` needs this
    ))
    assert result == exp
            MULTIPLE_COMMAND_BORDER_EXAMPLES,
            (
                # ensure these rules work with more than one command
                (f"cat | {commandline}", context)
                for commandline, context in MULTIPLE_COMMAND_BORDER_EXAMPLES
            ),
        )
    ),
)
def test_cursor_in_multiple_keyword_borders(commandline, context):
    assert_match(commandline, context)


PYTHON_EXAMPLES = (
    # commandline, context
    (f"x = {X}", PythonContext("x = ", 4)),
    (f"a {X}= x; b = y", PythonContext("a = x; b = y", 2)),
    (f"a {X}= x\nb = $(ls)", PythonContext("a = x\nb = $(ls)", 2)),
)

PYTHON_NESTING_EXAMPLES = (
    # nesting, prefix
    f"echo @({X})",
    f"echo $(echo @({X}))",
    f"echo @(x + @({X}))",  # invalid syntax, but can still be in a partial command
)


@pytest.mark.parametrize(
    "nesting, commandline, context",
    list(