summaryrefslogtreecommitdiff
path: root/test/t/unit/test_unit_get_comp_words_by_ref.py
blob: b6498fa74c214b3c9c5971a0a3092114518a898b (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pytest

from conftest import TestUnitBase, assert_bash_exec


@pytest.mark.bashcomp(
    cmd=None,
    ignore_env=r"^(\+(words|cword|cur|prev)|[+-]COMP_(WORDS|CWORD|LINE|POINT))=",
)
class TestUnitGetCompWordsByRef(TestUnitBase):
    def _test(self, bash, *args, **kwargs):
        assert_bash_exec(bash, "unset cur prev")
        output = self._test_unit(
            "_get_comp_words_by_ref %s cur prev; echo $cur,${prev-}",
            bash,
            *args,
            **kwargs
        )
        return output.strip()

    def test_1(self, bash):
        assert_bash_exec(
            bash,
            "COMP_WORDS=() COMP_CWORD= COMP_POINT= COMP_LINE= "
            "_get_comp_words_by_ref cur >/dev/null",
        )

    def test_2(self, bash):
        """a b|"""
        output = self._test(bash, "(a b)", 1, "a b", 3)
        assert output == "b,a"

    def test_3(self, bash):
        """a |"""
        output = self._test(bash, "(a)", 1, "a ", 2)
        assert output == ",a"

    def test_4(self, bash):
        """|a"""
        output = self._test(bash, "(a)", 0, "a", 0)
        assert output == ","

    def test_5(self, bash):
        """|a """
        output = self._test(bash, "(a)", 0, "a ", 0)
        assert output == ","

    def test_6(self, bash):
        """ | a """
        output = self._test(bash, "(a)", 0, "  a ", 1)
        assert output.strip() == ","

    def test_7(self, bash):
        """a b |"""
        output = self._test(bash, "(a b '')", 2, "a b ", 4)
        assert output == ",b"

    def test_8(self, bash):
        """a b | with WORDBREAKS -= :"""
        output = self._test(bash, "(a b '')", 2, "a b ", 4, arg="-n :")
        assert output == ",b"

    def test_9(self, bash):
        """a b|c"""
        output = self._test(bash, "(a bc)", 1, "a bc", 3)
        assert output == "b,a"

    def test_10(self, bash):
        """a | b"""
        output = self._test(bash, "(a b)", 1, "a  b", 2)
        assert output == ",a"

    def test_11(self, bash):
        r"""a b\ c|"""
        output = self._test(bash, r"(a 'b\ c')", 1, r"a b\ c", 6)
        assert output == r"b\ c,a"

    def test_12(self, bash):
        r"""a\ b a\ b|"""
        output = self._test(bash, r"('a\ b' 'a\ b')", 1, r"a\ b a\ b", 9)
        assert output == r"a\ b,a\ b"

    def test_13(self, bash):
        r"""a b\| c"""
        output = self._test(bash, r"(a 'b\ c')", 1, r"a b\ c", 4)
        assert output == r"b\,a"

    def test_14(self, bash):
        r"""a "b\|"""
        output = self._test(bash, "(a '\"b')", 1, 'a "b\\', 5)
        assert output == r'"b\,a'

    def test_15(self, bash):
        """a 'b c|"""
        output = self._test(bash, '(a "\'b c")', 1, "a 'b c", 6)
        assert output == "'b c,a"

    def test_16(self, bash):
        """a "b c|"""
        output = self._test(bash, r'(a "\"b c")', 1, 'a "b c', 6)
        assert output == '"b c,a'

    def test_17(self, bash):
        """a b:c| with WORDBREAKS += :"""
        assert_bash_exec(bash, "add_comp_wordbreak_char :")
        output = self._test(bash, "(a b : c)", 3, "a b:c", 5)
        assert output == "c,:"

    def test_18(self, bash):
        """a b:c| with WORDBREAKS -= :"""
        output = self._test(bash, "(a b : c)", 3, "a b:c", 5, arg="-n :")
        assert output == "b:c,a"

    def test_19(self, bash):
        """a b c:| with WORDBREAKS -= :"""
        output = self._test(bash, "(a b c :)", 3, "a b c:", 6, arg="-n :")
        assert output == "c:,b"

    def test_20(self, bash):
        r"""a b:c | with WORDBREAKS -= :"""
        output = self._test(bash, "(a b : c '')", 4, "a b:c ", 6, arg="-n :")
        assert output == ",b:c"

    def test_21(self, bash):
        """a :| with WORDBREAKS -= :"""
        output = self._test(bash, "(a :)", 1, "a :", 3, arg="-n :")
        assert output == ":,a"

    def test_22(self, bash):
        """a b::| with WORDBREAKS -= :"""
        output = self._test(bash, "(a b ::)", 2, "a b::", 5, arg="-n :")
        assert output == "b::,a"

    def test_23(self, bash):
        """a -n|

        This test makes sure `_get_cword' doesn't use `echo' to return its
        value, because -n might be interpreted by `echo' and thus woud not
        be returned.
        """
        output = self._test(bash, "(a -n)", 1, "a -n", 4)
        assert output == "-n,a"

    def test_24(self, bash):
        """a b>c|"""
        output = self._test(bash, r"(a b \> c)", 3, "a b>c", 5)
        assert output.startswith("c,")

    def test_25(self, bash):
        """a b=c|"""
        output = self._test(bash, "(a b = c)", 3, "a b=c", 5)
        assert output.startswith("c,")

    def test_26(self, bash):
        """a *|"""
        output = self._test(bash, r"(a \*)", 1, "a *", 4)
        assert output == "*,a"

    def test_27(self, bash):
        """a $(b c|"""
        output = self._test(bash, "(a '$(b c')", 1, "a $(b c", 7)
        assert output == "$(b c,a"

    def test_28(self, bash):
        r"""a $(b c\ d|"""
        output = self._test(bash, r"(a '$(b c\ d')", 1, r"a $(b c\ d", 10)
        assert output == r"$(b c\ d,a"

    def test_29(self, bash):
        """a 'b&c|"""
        output = self._test(bash, '(a "\'b&c")', 1, "a 'b&c", 6)
        assert output == "'b&c,a"

    def test_30(self, bash):
        """a b| to all vars"""
        assert_bash_exec(bash, "unset words cword cur prev")
        output = self._test_unit(
            "_get_comp_words_by_ref words cword cur prev%s; "
            'echo "${words[@]}",$cword,$cur,$prev',
            bash,
            "(a b)",
            1,
            "a b",
            3,
        )
        assert output == "a b,1,b,a"

    def test_31(self, bash):
        """a b| to alternate vars"""
        assert_bash_exec(bash, "unset words2 cword2 cur2 prev2")
        output = self._test_unit(
            "_get_comp_words_by_ref -w words2 -i cword2 -c cur2 -p prev2%s; "
            'echo $cur2,$prev2,"${words2[@]}",$cword2',
            bash,
            "(a b)",
            1,
            "a b",
            3,
        )
        assert output == "b,a,a b,1"
        assert_bash_exec(bash, "unset words2 cword2 cur2 prev2")

    def test_32(self, bash):
        """a b : c| with wordbreaks -= :"""
        assert_bash_exec(bash, "unset words")
        output = self._test_unit(
            '_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
            bash,
            "(a b : c)",
            3,
            "a b : c",
            7,
        )
        assert output == "a b : c"

    def test_33(self, bash):
        """a b: c| with wordbreaks -= :"""
        assert_bash_exec(bash, "unset words")
        output = self._test_unit(
            '_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
            bash,
            "(a b : c)",
            3,
            "a b: c",
            6,
        )
        assert output == "a b: c"

    def test_34(self, bash):
        """a b :c| with wordbreaks -= :"""
        assert_bash_exec(bash, "unset words")
        output = self._test_unit(
            '_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
            bash,
            "(a b : c)",
            3,
            "a b :c",
            6,
        )
        assert output == "a b :c"

    def test_35(self, bash):
        r"""a b\ :c| with wordbreaks -= :"""
        assert_bash_exec(bash, "unset words")
        output = self._test_unit(
            '_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
            bash,
            "(a 'b ' : c)",
            3,
            r"a b\ :c",
            7,
        )
        assert output == "a b  :c"

    def test_unknown_arg_error(self, bash):
        with pytest.raises(AssertionError) as ex:
            _ = assert_bash_exec(
                bash, "_get_comp_words_by_ref dummy", want_output=True
            )
        ex.match("dummy.* unknown argument")