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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
|
import itertools
import typing as tp
from unittest import mock
import pytest
import xonsh.parsers.completion_context as ctx
from xonsh.parsers.completion_context import (
CommandArg,
CommandContext,
CompletionContextParser,
PythonContext,
)
from xonsh.pytest.tools import ON_WINDOWS
DEBUG = False
MISSING = object()
X = "\x00" # cursor marker
PARSER: tp.Optional[CompletionContextParser] = None
@pytest.fixture(scope="module", autouse=True)
def parser():
global PARSER
PARSER = CompletionContextParser(debug=DEBUG)
patcher = None
if ON_WINDOWS:
# on-windows has an option for interactive sessions. Overriding the lazyObject
patcher = mock.patch.object(
ctx,
"LINE_CONT_REPLACEMENT_DIFF",
("\\\n", "", -2),
)
patcher.start()
yield
PARSER = None
if ON_WINDOWS and patcher:
patcher.stop()
def parse(command, inner_index):
return PARSER.parse(command, inner_index)
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
COMMAND_EXAMPLES = (
(f"comm{X}", CommandContext(args=(), arg_index=0, prefix="comm")),
(f" comm{X}", CommandContext(args=(), arg_index=0, prefix="comm")),
(f"comm{X}and", CommandContext(args=(), arg_index=0, prefix="comm", suffix="and")),
(f"command {X}", CommandContext(args=(CommandArg("command"),), arg_index=1)),
(f"{X} command", CommandContext(args=(CommandArg("command"),), arg_index=0)),
(f" command {X}", CommandContext(args=(CommandArg("command"),), arg_index=1)),
(
f"command --{X}",
CommandContext(args=(CommandArg("command"),), arg_index=1, prefix="--"),
),
(
f"command a {X}",
CommandContext(args=(CommandArg("command"), CommandArg("a")), arg_index=2),
),
(
f"command a b{X}",
CommandContext(
args=(CommandArg("command"), CommandArg("a")), arg_index=2, prefix="b"
),
),
(
f"command a b{X}",
CommandContext(
args=(CommandArg("command"), CommandArg("a")), arg_index=2, prefix="b"
),
),
(
f"command {X} a",
CommandContext(args=(CommandArg("command"), CommandArg("a")), arg_index=1),
),
(
f"command a {X} b",
CommandContext(
args=(CommandArg("command"), CommandArg("a"), CommandArg("b")), arg_index=2
),
),
(
f"command -{X} b",
CommandContext(
args=(CommandArg("command"), CommandArg("b")), arg_index=1, prefix="-"
),
),
(
f"command a {X}b",
CommandContext(
args=(CommandArg("command"), CommandArg("a")), arg_index=2, suffix="b"
),
),
(
f"command a{X}b",
CommandContext(
args=(CommandArg("command"),), arg_index=1, prefix="a", suffix="b"
),
),
(
f"'comm and' a{X}b",
CommandContext(
args=(CommandArg("comm and", opening_quote="'", closing_quote="'"),),
arg_index=1,
prefix="a",
suffix="b",
),
),
(
f"command >/dev/nul{X}",
CommandContext(
args=(CommandArg("command"), CommandArg(">", is_io_redir=True)),
arg_index=2,
prefix="/dev/nul",
),
),
(
f"command 2>/dev/nul{X}",
CommandContext(
args=(CommandArg("command"), CommandArg("2>", is_io_redir=True)),
arg_index=2,
prefix="/dev/nul",
),
),
(
f"command >>/dev/nul{X}",
CommandContext(
args=(CommandArg("command"), CommandArg(">>", is_io_redir=True)),
arg_index=2,
prefix="/dev/nul",
),
),
(
f"command </dev/nul{X}",
CommandContext(
args=(CommandArg("command"), CommandArg("<", is_io_redir=True)),
arg_index=2,
prefix="/dev/nul",
),
),
)
EMPTY_COMMAND_EXAMPLES = (
(f"{X}", CommandContext((), 0)),
(f" {X}", CommandContext((), 0)),
(f"{X} ", CommandContext((), 0)),
(f" {X} ", CommandContext((), 0)),
)
STRING_ARGS_EXAMPLES = (
(
f"'comm an{X}d'",
CommandContext(
args=(),
arg_index=0,
prefix="comm an",
suffix="d",
opening_quote="'",
closing_quote="'",
),
),
(
f"'comm and{X}'",
CommandContext(
args=(),
arg_index=0,
prefix="comm and",
suffix="",
opening_quote="'",
closing_quote="'",
),
),
(
f"'comm {X}'",
CommandContext(
args=(),
arg_index=0,
prefix="comm ",
suffix="",
opening_quote="'",
closing_quote="'",
),
),
(
f'"comm an{X}d"',
CommandContext(
args=(),
arg_index=0,
prefix="comm an",
suffix="d",
opening_quote='"',
closing_quote='"',
),
),
(
f"'''comm an{X}d'''",
CommandContext(
args=(),
arg_index=0,
prefix="comm an",
suffix="d",
opening_quote="'''",
closing_quote="'''",
),
),
(
f"fr'comm an{X}d'",
CommandContext(
args=(),
arg_index=0,
prefix="comm an",
suffix="d",
opening_quote="fr'",
closing_quote="'",
),
),
(
f"'()+{X}'",
CommandContext(
args=(), arg_index=0, prefix="()+", opening_quote="'", closing_quote="'"
),
),
(
f"'comm and'{X}",
CommandContext(
args=(),
arg_index=0,
prefix="comm and",
opening_quote="'",
closing_quote="'",
is_after_closing_quote=True,
),
),
(
f"'''comm and'''{X}",
CommandContext(
args=(),
arg_index=0,
prefix="comm and",
opening_quote="'''",
closing_quote="'''",
is_after_closing_quote=True,
),
),
)
COMMAND_EXAMPLES += STRING_ARGS_EXAMPLES
COMMAND_EXAMPLES += EMPTY_COMMAND_EXAMPLES
@pytest.mark.parametrize("commandline, context", COMMAND_EXAMPLES)
def test_command(commandline, context):
assert_match(commandline, context, is_main_command=True)
@pytest.mark.parametrize(
"commandline, context",
tuple(
(commandline, context)
for commandline, context in STRING_ARGS_EXAMPLES
if commandline.endswith("'") or commandline.endswith('"')
),
)
def test_partial_string_arg(commandline, context):
partial_commandline = commandline.rstrip("\"'")
partial_context = context._replace(closing_quote="")
assert_match(partial_commandline, partial_context, is_main_command=True)
CONT = "\\" "\n"
@pytest.mark.parametrize(
"commandline, context",
(
# line continuations:
(
f"echo {CONT}a {X}",
CommandContext(args=(CommandArg("echo"), CommandArg("a")), arg_index=2),
),
(
f"echo {CONT}{X}a {CONT} b",
CommandContext(
args=(CommandArg("echo"), CommandArg("b")), arg_index=1, suffix="a"
),
),
(
f"echo a{CONT}{X}b",
CommandContext(
args=(CommandArg("echo"),), arg_index=1, prefix="a", suffix="b"
),
),
(
f"echo a{X}{CONT}b",
CommandContext(
args=(CommandArg("echo"),), arg_index=1, prefix="a", suffix="b"
),
),
(
f"echo ${CONT}(a) {CONT} {X}b",
CommandContext(
args=(CommandArg("echo"), CommandArg("$(a)")), arg_index=2, suffix="b"
),
),
# line continuations in strings:
(
f"echo 'a{CONT}{X}b'",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix="b",
opening_quote="'",
closing_quote="'",
),
),
(
f"echo '''a{CONT}{X}b'''",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix="b",
opening_quote="'''",
closing_quote="'''",
),
),
(
f"echo 'a{CONT}{X}b",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix="b",
opening_quote="'",
),
),
(
f"echo '''a{CONT}{X}b",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix="b",
opening_quote="'''",
),
),
(
f"echo ''{CONT}'a{X}b",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix="b",
opening_quote="'''",
),
),
(
f"echo '''a{CONT}{X} b",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a",
suffix=" b",
opening_quote="'''",
),
),
# triple-quoted strings:
(
f"echo '''a\nb{X}\nc'''",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a\nb",
suffix="\nc",
opening_quote="'''",
closing_quote="'''",
),
),
(
f"echo '''a\n b{X} \n c'''",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a\n b",
suffix=" \n c",
opening_quote="'''",
closing_quote="'''",
),
),
# partial triple-quoted strings:
(
f"echo '''a\nb{X}\nc",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a\nb",
suffix="\nc",
opening_quote="'''",
),
),
(
f"echo '''a\n b{X} \n c",
CommandContext(
args=(CommandArg("echo"),),
arg_index=1,
prefix="a\n b",
suffix=" \n c",
opening_quote="'''",
),
),
),
)
def test_multiline_command(commandline, context):
assert_match(commandline, context, is_main_command=True)
NESTING_EXAMPLES = (
# nesting, prefix
(f"echo $({X})", "$("),
(f"echo @$({X})", "@$("),
(f"echo $(echo $({X}))", "$("),
(f"echo @(x + $({X}))", "$("),
(f"!({X})", "!("),
(f"$[{X}]", "$["),
(f"![{X}]", "!["),
)
NESTED_SIMPLE_CMD_EXAMPLES = [
(
nesting,
f"simple {X}",
CommandContext(
args=(CommandArg("simple"),), arg_index=1, subcmd_opening=prefix
),
)
for nesting, prefix in NESTING_EXAMPLES[1:]
]
@pytest.mark.parametrize(
"nesting, commandline, context",
list(
itertools.chain(
(
# complex subcommand in a simple nested expression
(
NESTING_EXAMPLES[0][0],
commandline,
context._replace(subcmd_opening=NESTING_EXAMPLES[0][1]),
)
for commandline, context in COMMAND_EXAMPLES
),
NESTED_SIMPLE_CMD_EXAMPLES,
)
),
)
def test_nested_command(commandline, context, nesting):
nested_commandline = nesting.replace(X, commandline)
assert_match(nested_commandline, command_context=context, python_context=None)
NESTING_MALFORMATIONS = (
lambda s: s[:-1], # remove the last closing brace ')' / ']'
lambda s: s + s[-1], # add an extra closing brace ')' / ']'
lambda s: s[-1] + s,
lambda s: s + "$(",
lambda s: "$(" + s,
)
@pytest.mark.parametrize("nesting, commandline, context", NESTED_SIMPLE_CMD_EXAMPLES)
@pytest.mark.parametrize("malformation", NESTING_MALFORMATIONS)
def test_malformed_subcmd(nesting, commandline, context, malformation):
nested_commandline = nesting.replace(X, commandline)
nested_commandline = malformation(nested_commandline)
assert_match(nested_commandline, command_context=context, python_context=None)
MALFORMED_SUBCOMMANDS_NESTINGS = (
# nesting, subcmd_opening
(f"echo $(a $({X}", "$("),
(f"echo $(a $(b; {X}", ""),
(f"$(echo $(a $({X}", "$("),
(f"echo $[a $({X}]", "$("),
(f"echo $(a $[{X})", "$["),
(f"echo @(x = $({X}", "$("),
(f"echo @(a; x = $({X}", "$("),
(f"echo @(x = $(a; {X}", ""),
)
@pytest.mark.parametrize("nesting, subcmd_opening", MALFORMED_SUBCOMMANDS_NESTINGS)
@pytest.mark.parametrize("commandline, context", COMMAND_EXAMPLES[:5])
def test_multiple_malformed_subcmds(nesting, subcmd_opening, commandline, context):
nested_commandline = nesting.replace(X, commandline)
nested_context = context._replace(subcmd_opening=subcmd_opening)
assert_match(nested_commandline, nested_context, python_context=None)
def test_other_subcommand_arg():
command = "echo $(pwd) "
assert_match(
command,
CommandContext((CommandArg("echo"), CommandArg("$(pwd)")), arg_index=2),
is_main_command=True,
)
def test_combined_subcommand_arg():
command = f"echo file=$(pwd{X})/x"
# index inside the subproc
assert_match(
command,
CommandContext((), arg_index=0, prefix="pwd", subcmd_opening="$("),
python_context=None,
)
# index at the end of the command
assert_match(
command.replace(X, ""),
CommandContext((CommandArg("echo"),), arg_index=1, prefix="file=$(pwd)/x"),
is_main_command=True,
)
SUBCMD_BORDER_EXAMPLES = (
(f"{X}$(echo)", CommandContext((), 0, suffix="$(echo)")),
(f"${X}(echo)", CommandContext((), 0, prefix="$", suffix="(echo)")),
(f"$(echo){X}", CommandContext((), 0, prefix="$(echo)")),
(f"${X}( echo)", CommandContext((), 0, prefix="$", suffix="( echo)")),
(f"$(echo ){X}", CommandContext((), 0, prefix="$(echo )")),
)
@pytest.mark.parametrize("commandline, context", SUBCMD_BORDER_EXAMPLES)
def test_cursor_in_subcmd_borders(commandline, context):
assert_match(commandline, context, is_main_command=True)
MULTIPLE_COMMAND_KEYWORDS = (
"; ",
"\n",
" and ",
"&& ",
" or ",
"|| ",
"| ",
)
MULTIPLE_CMD_SIMPLE_EXAMPLES = [
(
keyword,
("echo hi", f"simple {X}"),
CommandContext(args=(CommandArg("simple"),), arg_index=1),
)
for keyword in MULTIPLE_COMMAND_KEYWORDS
]
EXTENSIVE_COMMAND_PAIRS = tuple(
itertools.chain(
zip(COMMAND_EXAMPLES, COMMAND_EXAMPLES[::-1]),
zip(COMMAND_EXAMPLES, EMPTY_COMMAND_EXAMPLES),
zip(EMPTY_COMMAND_EXAMPLES, COMMAND_EXAMPLES),
zip(EMPTY_COMMAND_EXAMPLES, EMPTY_COMMAND_EXAMPLES),
)
)
MULTIPLE_COMMAND_EXTENSIVE_EXAMPLES = tuple(
itertools.chain(
(
# cursor in first command
((first, second.replace(X, "")), first_context)
for (first, first_context), (
second,
second_context,
) in EXTENSIVE_COMMAND_PAIRS
),
(
# cursor in second command
((first.replace(X, ""), second), second_context)
for (first, first_context), (
second,
second_context,
) in EXTENSIVE_COMMAND_PAIRS
),
(
# cursor in middle command
((first.replace(X, ""), second, third.replace(X, "")), second_context)
for (first, _1), (second, second_context), (third, _3) in zip(
COMMAND_EXAMPLES[:3], COMMAND_EXAMPLES[3:6], COMMAND_EXAMPLES[6:9]
)
),
(
# cursor in third command
((first.replace(X, ""), second.replace(X, ""), third), third_context)
for (first, _1), (second, _2), (third, third_context) in zip(
COMMAND_EXAMPLES[:3], COMMAND_EXAMPLES[3:6], COMMAND_EXAMPLES[6:9]
)
),
)
)
@pytest.mark.parametrize(
"keyword, commands, context",
tuple(
itertools.chain(
(
(MULTIPLE_COMMAND_KEYWORDS[0], commands, context)
for commands, context in MULTIPLE_COMMAND_EXTENSIVE_EXAMPLES
),
MULTIPLE_CMD_SIMPLE_EXAMPLES,
)
),
)
def test_multiple_commands(keyword, commands, context):
joined_command = keyword.join(commands)
cursor_command = next(command for command in commands if X in command)
if cursor_command is commands[0]:
relative_index = cursor_command.index(X)
else:
absolute_index = joined_command.index(X)
relative_index = (
absolute_index
- joined_command.rindex(keyword, 0, absolute_index)
- len(keyword)
)
if keyword.endswith(" "):
# the last space is part of the command
relative_index += 1
cursor_command = " " + cursor_command
assert_match(joined_command, context, is_main_command=True)
@pytest.mark.parametrize(
"commandline",
(
f"{X};",
f"; {X}",
f"{X};;",
f"; {X};",
f";; {X}",
f";;; {X}",
),
)
def test_multiple_empty_commands(commandline):
assert_match(commandline, CommandContext((), 0), is_main_command=True)
@pytest.mark.parametrize(
"nesting, keyword, commands, context",
tuple(
(
nesting,
keyword,
commands,
context,
) # no subcmd_opening in nested multi-commands
for nesting, prefix in NESTING_EXAMPLES
for keyword, commands, context in MULTIPLE_CMD_SIMPLE_EXAMPLES
if keyword != "\n" # the lexer ignores newlines inside subcommands
),
)
def test_nested_multiple_commands(nesting, keyword, commands, context):
joined_command = keyword.join(commands)
nested_joined = nesting.replace(X, joined_command)
assert_match(nested_joined, context, python_context=None)
def test_multiple_nested_commands():
assert_match(
f"echo hi; echo $(ls{X})",
CommandContext((), 0, prefix="ls", subcmd_opening="$("),
python_context=None,
)
@pytest.mark.parametrize(
"commandline, context",
tuple(
(commandline, context)
for commandline, context in STRING_ARGS_EXAMPLES
if commandline.endswith("'") or commandline.endswith('"')
),
)
def test_multiple_partial_string_arg(commandline, context):
partial_commandline = commandline.rstrip("\"'")
partial_context = context._replace(closing_quote="")
assert_match("echo;" + partial_commandline, partial_context)
assert_match("echo $[a ;" + partial_commandline, partial_context)
@pytest.mark.parametrize(
"nesting, keyword, commands, context",
tuple(
(nesting, keyword, commands, context)
for nesting, prefix in NESTING_EXAMPLES[:1]
for keyword, commands, context in MULTIPLE_CMD_SIMPLE_EXAMPLES[:1]
),
)
@pytest.mark.parametrize("malformation", NESTING_MALFORMATIONS)
def test_malformed_subcmd_1(malformation, nesting, keyword, commands, context):
joined_command = keyword.join(commands)
nested_joined = nesting.replace(X, joined_command)
malformed_commandline = malformation(nested_joined)
assert_match(malformed_commandline, context, python_context=None)
MULTIPLE_COMMAND_BORDER_EXAMPLES = tuple(
itertools.chain(
itertools.chain(
*(
(
(
f"ls{ws1}{X}{kwd}{ws2}echo",
CommandContext((CommandArg("ls"),), 1)
if ws1
else CommandContext((), 0, prefix="ls"),
),
)
for ws1, ws2, kwd in itertools.product(
("", " "), ("", " "), ("&&", ";")
)
)
),
# all keywords are treated as a normal arg if the cursor is at the edge
(
(
f"ls {X}and echo",
CommandContext((CommandArg("ls"), CommandArg("echo")), 1, suffix="and"),
),
(
f"ls and{X} echo",
CommandContext((CommandArg("ls"), CommandArg("echo")), 1, prefix="and"),
),
(
f"ls ||{X} echo",
CommandContext((CommandArg("ls"), CommandArg("echo")), 1, prefix="||"),
),
),
# if the cursor is inside the keyword, it's treated as a normal arg
(
(
f"ls a{X}nd echo",
CommandContext(
(CommandArg("ls"), CommandArg("echo")), 1, prefix="a", suffix="nd"
),
),
(
f"ls &{X}& echo",
CommandContext(
(CommandArg("ls"), CommandArg("echo")), 1, prefix="&", suffix="&"
),
),
),
)
)
@pytest.mark.parametrize(
"commandline, context",
tuple(
itertools.chain(
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(
itertools.chain(
(
# complex subcommand in a simple nested expression
(nesting, commandline, context._replace(is_sub_expression=True))
for nesting in PYTHON_NESTING_EXAMPLES[:1]
for commandline, context in PYTHON_EXAMPLES
),
(
# simple subcommand in a complex nested expression
(nesting, commandline, context._replace(is_sub_expression=True))
for nesting in PYTHON_NESTING_EXAMPLES
for commandline, context in PYTHON_EXAMPLES[:1]
),
)
),
)
def test_nested_python(commandline, context, nesting):
nested_commandline = nesting.replace(X, commandline)
assert_match(nested_commandline, command_context=None, python_context=context)
@pytest.mark.parametrize(
"commandline, context",
[
(
commandline.replace("$", "@"),
context._replace(
prefix=context.prefix.replace("$", "@"),
suffix=context.suffix.replace("$", "@"),
),
)
for commandline, context in SUBCMD_BORDER_EXAMPLES
],
)
def test_cursor_in_sub_python_borders(commandline, context):
assert_match(commandline, context, is_main_command=True)
@pytest.mark.parametrize(
"code",
(
f"""
x = 3
x.{X}""",
f"""
x = 3;
y = 4;
x.{X}""",
f"""
def func({X}):
return 100
""",
f"""
class A:
def a():
return "a{X}"
pass
exit()
""",
),
)
def test_multiline_python(code):
assert_match(code, is_main_command=True)
|