|
7 | 7 | """ |
8 | 8 | import os |
9 | 9 | import pytest |
| 10 | +import shlex |
10 | 11 | import sys |
11 | 12 | from typing import List |
12 | 13 |
|
13 | 14 | from cmd2.argparse_completer import ACArgumentParser, AutoCompleter |
14 | 15 |
|
15 | 16 |
|
16 | 17 | try: |
17 | | - from cmd2.argcomplete_bridge import CompletionFinder |
| 18 | + from cmd2.argcomplete_bridge import CompletionFinder, tokens_for_completion |
18 | 19 | skip_reason1 = False |
19 | 20 | skip_reason = '' |
20 | 21 | except ImportError: |
@@ -230,3 +231,26 @@ def test_fail_alt_stderr(parser1, capfd, mock): |
230 | 231 | out, err = capfd.readouterr() |
231 | 232 | assert out == exp_out |
232 | 233 | assert err == exp_err |
| 234 | + |
| 235 | +@pytest.mark.skipif(skip_reason1, reason=skip_reason) |
| 236 | +def test_argcomplete_tokens_for_completion_simple(): |
| 237 | + line = 'this is "a test"' |
| 238 | + endidx = len(line) |
| 239 | + |
| 240 | + tokens, raw_tokens, begin_idx, end_idx = tokens_for_completion(line, endidx) |
| 241 | + assert tokens == shlex.split(line) |
| 242 | + assert raw_tokens == ['this', 'is', '"a test"'] |
| 243 | + assert begin_idx == line.rfind("is ") + len("is ") |
| 244 | + assert end_idx == end_idx |
| 245 | + |
| 246 | +@pytest.mark.skipif(skip_reason1, reason=skip_reason) |
| 247 | +def test_argcomplete_tokens_for_completion_unclosed_quotee_exception(): |
| 248 | + line = 'this is "a test' |
| 249 | + endidx = len(line) |
| 250 | + |
| 251 | + tokens, raw_tokens, begin_idx, end_idx = tokens_for_completion(line, endidx) |
| 252 | + |
| 253 | + assert tokens == ['this', 'is', 'a test'] |
| 254 | + assert raw_tokens == ['this', 'is', '"a test'] |
| 255 | + assert begin_idx == line.rfind("is ") + len("is ") + 1 |
| 256 | + assert end_idx == end_idx |
0 commit comments