summaryrefslogtreecommitdiff
path: root/test/test_completion.py
blob: 1a970470280430f3ae50e0bd7456c31ff99fee93 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Test the completions methods on an altered input object.
"""

import string
import pytest
import random
from poezio import config
from poezio.windows import Input


class ConfigShim(object):
    def get(self, *args, **kwargs):
        return ''
    def getbool(self, *args, **kwargs):
        return False


config.config = ConfigShim()


class SubInput(Input):
    def resize(self, *args, **kwargs):
        pass
    def rewrite_text(self, *args, **kwargs):
        pass
    def refresh(self, *args, **kwargs):
        pass


@pytest.fixture(scope="function")
def input_obj():
    from poezio.windows import base_wins
    base_wins.TAB_WIN = True
    obj = SubInput()
    obj.reset_completion()
    return obj

@pytest.fixture(scope="module")
def random_unquoted_words():
    letters = string.ascii_lowercase + ((len(string.ascii_lowercase)//4)*' ')
    acc = [random.choice(letters) for _ in range(200)]
    words = ''.join(acc).split()
    return words

@pytest.fixture(scope="module")
def quoted_words():
    words = []
    letters = string.ascii_lowercase + ((len(string.ascii_lowercase)//4)*' ')
    words_by_letter = {}
    for start_letter in string.ascii_lowercase:
        words_by_letter[start_letter] = []
        for _ in range(5):
            size = random.randint(0, 15)
            word = start_letter + ''.join(random.choice(letters) for i in range(size))
            words.append(word)
            words_by_letter[start_letter].append(word)
    return (words, words_by_letter)


def test_new_completion_1_unquoted(input_obj):

    input_obj.text = '/example '
    input_obj.pos = len(input_obj.text) - 1

    input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
    assert input_obj.text == '/example toto'

    input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
    assert input_obj.text == '/example titi'

    input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
    assert input_obj.text == '/example toto'


def test_new_completion_1_quoted_spaces(input_obj):
    input_obj.text = '/example '
    input_obj.pos = len(input_obj.text) - 1

    input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example "toto toto"'

    input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example "titi titi"'

    input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example "toto toto"'

    input_obj.text = '/example '
    input_obj.pos = len(input_obj.text) - 1
    input_obj.reset_completion()

    input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example "toto toto"'

    input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example tata'

    input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
    assert input_obj.text == '/example "titi titi"'

def test_new_completion_unquoted_random_override(input_obj, random_unquoted_words):
    """
    Complete completely random words and ensure that the input is
    changed adequately.
    """
    words = random_unquoted_words

    # try the completion on the middle element without affecting the others
    input_obj.text = '/example %s %s %s' % (words[0], words[1], words[2])
    base = len(input_obj.text) - len(words[2]) - 1
    input_obj.pos = base
    def f(n):
        return '/example %s %s' % (words[0], words[n])

    for i in range(len(words)):
        pos = input_obj.get_argument_position(False)
        input_obj.new_completion(words[:], pos, quotify=False, override=True)
        assert f(i) + " " + words[2] == input_obj.text
        assert len(f(i)) == input_obj.pos

    assert input_obj.text == '/example %s %s %s' % (words[0], words[-1], words[2])

    pos = input_obj.get_argument_position(False)
    input_obj.new_completion(words[:], pos, quotify=False, override=True)
    assert input_obj.text == '/example %s %s %s' % (words[0], words[0], words[2])

    input_obj.reset_completion()

    # try the completion on the final element without affecting the others
    input_obj.text = '/example %s %s %s' % (words[0], words[1], words[2])
    base = len(input_obj.text)
    input_obj.pos = base
    def f2(n):
        return '/example %s %s %s' % (words[0], words[1], words[n])

    print(words)
    for i in range(len(words)):
        pos = input_obj.get_argument_position(False)
        input_obj.new_completion(words[:], pos, quotify=False, override=True)
        assert f2(i) == input_obj.text
        assert len(f2(i)) == input_obj.pos

    assert input_obj.text == '/example %s %s %s' % (words[0], words[1], words[-1])


def test_new_completion_quoted_random(input_obj, quoted_words):
    """
    Complete (possibly) quoted words starting with a specific letter.
    And make sure that the quotes only appear when necessary.
    """
    words = quoted_words[0]
    words_l = quoted_words[1]

    letters = ('', 'a', 'b', 'c')

    # generate the text which is supposed to be present in the input
    def f(p, i):
        rep = words_l[letters[p]][i] if ' ' not in words_l[letters[p]][i] else '"'+words_l[letters[p]][i]+'"'
        fst = letters[1] if p != 1 else rep
        snd = letters[2] if p != 2 else rep
        trd = letters[3] if p != 3 else rep
        return '/example %s %s %s' % (fst, snd, trd)

    for pos in range(1, 4):
        input_obj.text = '/example a b c'
        input_obj.reset_completion()
        input_obj.pos = len('/example') + pos * 2

        extra = (3 - pos) * 2
        for i in range(5):
            input_obj.new_completion(words[:], pos, quotify=True)
            assert f(pos, i) == input_obj.text
            assert len(f(pos, i)) - extra == input_obj.pos