File: test_python.py

package info (click to toggle)
xonsh 0.19.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,532 kB
  • sloc: python: 48,521; makefile: 133; sh: 41; xml: 17
file content (114 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import pytest

from xonsh.completers.imports import complete_import
from xonsh.completers.python import complete_python, python_signature_complete
from xonsh.parsers.completion_context import CompletionContext, PythonContext
from xonsh.pytest.tools import skip_if_pre_3_8


@pytest.fixture(autouse=True)
def xonsh_execer_autouse(xession, xonsh_execer, monkeypatch):
    monkeypatch.setitem(xession.env, "COMPLETIONS_BRACKETS", True)
    return xonsh_execer


def foo(x, y, z):
    pass


def bar(wakka="wow", jawaka="mom"):
    pass


def baz(sonata, artica=True):
    pass


def always_true(x, y):
    return True


BASE_CTX = {"foo": foo, "bar": bar, "baz": baz}
FOO_ARGS = {"x=", "y=", "z="}
BAR_ARGS = {"wakka=", "jawaka="}
BAZ_ARGS = {"sonata=", "artica="}


@pytest.mark.parametrize(
    "line, end, exp",
    [
        ("foo(", 4, FOO_ARGS),  # I have no idea why this one needs to be first
        ("foo()", 3, set()),
        ("foo()", 4, FOO_ARGS),
        ("foo()", 5, set()),
        ("foo(x, ", 6, FOO_ARGS),
        ("foo(x, )", 6, FOO_ARGS),
        ("bar()", 4, BAR_ARGS),
        ("baz()", 4, BAZ_ARGS),
        ("foo(bar(", 8, BAR_ARGS),
        ("foo(bar()", 9, FOO_ARGS),
        ("foo(bar())", 4, FOO_ARGS),
    ],
)
def test_complete_python_signatures(line, end, exp):
    ctx = dict(BASE_CTX)
    obs = python_signature_complete("", line, end, ctx, always_true)
    assert exp == obs


@pytest.mark.parametrize(
    "code, exp",
    (
        ("x = su", "sum"),
        ("imp", "import"),
        ("{}.g", "{}.get("),
        # no signature for native builtins under 3.7:
        pytest.param("''.split(ma", "maxsplit=", marks=skip_if_pre_3_8),
    ),
)
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


def test_complete_python_ctx():
    class A:
        def wow(self):
            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


@pytest.mark.parametrize(
    "command, exp",
    [
        ("import pathli", {"pathlib"}),
        ("from pathli", {"pathlib"}),
        ("import os.pa", {"os.path"}),
        ("import sys,os.pa", {"os.path"}),
        ("from x ", {"import"}),
        ("import os, pathli", {"pathlib"}),
        ("from pathlib import PurePa", {"PurePath"}),
        ("from pathlib import PosixPath,PurePa", {"PurePath"}),
        ("from pathlib import PosixPath PurePa", {"PurePath"}),
    ],
)
@pytest.mark.flaky(reruns=2, reruns_delay=2)
@pytest.mark.skip(reason="flaky")
def test_complete_import(command, exp, completer_obj):
    result = complete_import(completer_obj.parse(command))
    if isinstance(result, tuple):
        result, _ = result
    result = set(result)
    assert result == exp