2022-07-01 06:17:46 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from pexpect_helper import SpawnedProc
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
sp = SpawnedProc()
|
|
|
|
send, sendline, sleep, expect_prompt, expect_re, expect_str = (
|
|
|
|
sp.send,
|
|
|
|
sp.sendline,
|
|
|
|
sp.sleep,
|
|
|
|
sp.expect_prompt,
|
|
|
|
sp.expect_re,
|
|
|
|
sp.expect_str,
|
|
|
|
)
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
# ? prints the command line, bracketed by <>, then clears it.
|
|
|
|
sendline(r"""bind '?' 'echo "<$(commandline)>"; commandline ""'; """)
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
# Basic test.
|
2022-04-02 03:05:27 +08:00
|
|
|
# Default abbreviations expand only in command position.
|
2022-07-01 06:17:46 +08:00
|
|
|
sendline(r"abbr alpha beta")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
send(r"alpha ?")
|
|
|
|
expect_str(r"<beta >")
|
|
|
|
|
|
|
|
send(r"echo alpha ?")
|
|
|
|
expect_str(r"<echo alpha >")
|
|
|
|
|
|
|
|
# Abbreviation expansions may have multiple words.
|
2022-12-13 08:18:04 +08:00
|
|
|
sendline(r"abbr --add emacsnw emacs -nw -l")
|
2022-07-01 06:17:46 +08:00
|
|
|
expect_prompt()
|
|
|
|
send(r"emacsnw ?")
|
2022-12-13 08:18:04 +08:00
|
|
|
expect_str(r"<emacs -nw -l >")
|
2022-07-01 06:17:46 +08:00
|
|
|
|
|
|
|
# Regression test that abbreviations still expand in incomplete positions.
|
|
|
|
sendline(r"""abbr --erase (abbr --list)""")
|
|
|
|
expect_prompt()
|
|
|
|
sendline(r"""abbr --add foo echo bar""")
|
|
|
|
expect_prompt()
|
|
|
|
sendline(r"if foo")
|
|
|
|
expect_str(r"if echo bar")
|
|
|
|
sendline(r"end")
|
|
|
|
expect_prompt(r"bar")
|
|
|
|
|
|
|
|
# Regression test that 'echo (' doesn't hang.
|
|
|
|
sendline(r"echo )")
|
|
|
|
expect_str(r"Unexpected ')' for unopened parenthesis")
|
|
|
|
send(r"?")
|
|
|
|
expect_str(r"<echo )>")
|
2022-04-02 03:05:27 +08:00
|
|
|
|
|
|
|
# Support position anywhere.
|
|
|
|
sendline(r"abbr alpha --position anywhere beta2")
|
2022-08-07 07:57:26 +08:00
|
|
|
expect_prompt()
|
2022-04-02 03:05:27 +08:00
|
|
|
|
|
|
|
send(r"alpha ?")
|
|
|
|
expect_str(r"<beta2 >")
|
|
|
|
|
|
|
|
send(r"echo alpha ?")
|
|
|
|
expect_str(r"<echo beta2 >")
|
2022-07-18 04:27:35 +08:00
|
|
|
|
|
|
|
# Support regex.
|
|
|
|
sendline(r"abbr alpha --regex 'A[0-9]+Z' beta3")
|
2022-08-07 07:57:26 +08:00
|
|
|
expect_prompt()
|
2022-07-18 04:27:35 +08:00
|
|
|
send(r"A123Z ?")
|
|
|
|
expect_str(r"<beta3 >")
|
|
|
|
send(r"AZ ?")
|
|
|
|
expect_str(r"<AZ >")
|
2022-08-08 04:31:10 +08:00
|
|
|
send(r"QA123Z ?") # fails as the regex must match the entire string
|
2022-07-18 04:27:35 +08:00
|
|
|
expect_str(r"<QA123Z >")
|
|
|
|
send(r"A0000000000000000000009Z ?")
|
|
|
|
expect_str(r"<beta3 >")
|
2022-08-07 07:57:26 +08:00
|
|
|
|
|
|
|
# Support functions. Here anything in between @@ is uppercased, except 'nope'.
|
|
|
|
sendline(
|
2022-08-08 04:31:10 +08:00
|
|
|
r"""function uppercaser
|
2022-08-07 07:57:26 +08:00
|
|
|
string match --quiet '*nope*' $argv[1] && return 1
|
|
|
|
string trim --chars @ $argv | string upper
|
2022-08-08 04:31:10 +08:00
|
|
|
end
|
|
|
|
""".strip()
|
2022-08-07 07:57:26 +08:00
|
|
|
)
|
|
|
|
expect_prompt()
|
|
|
|
sendline(r"abbr uppercaser --regex '@.+@' --function uppercaser")
|
|
|
|
expect_prompt()
|
|
|
|
send(r"@abc@ ?")
|
|
|
|
expect_str(r"<ABC >")
|
|
|
|
send(r"@nope@ ?")
|
|
|
|
expect_str(r"<@nope@ >")
|
|
|
|
sendline(r"abbr --erase uppercaser")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
# -f works as shorthand for --function.
|
|
|
|
sendline(r"abbr uppercaser2 --regex '@.+@' -f uppercaser")
|
|
|
|
expect_prompt()
|
|
|
|
send(r"@abc@ ?")
|
|
|
|
expect_str(r"<ABC >")
|
|
|
|
send(r"@nope@ ?")
|
|
|
|
expect_str(r"<@nope@ >")
|
|
|
|
sendline(r"abbr --erase uppercaser2")
|
|
|
|
expect_prompt()
|
|
|
|
|
2022-08-08 04:31:10 +08:00
|
|
|
# Abbreviations which cause the command line to become incomplete or invalid
|
2022-11-28 03:29:16 +08:00
|
|
|
# are visibly expanded.
|
2022-12-13 08:18:04 +08:00
|
|
|
sendline(r"abbr openparen --position anywhere '('")
|
2022-08-08 04:31:10 +08:00
|
|
|
expect_prompt()
|
2022-12-13 08:18:04 +08:00
|
|
|
sendline(r"abbr closeparen --position anywhere ')'")
|
2022-08-08 04:31:10 +08:00
|
|
|
expect_prompt()
|
|
|
|
sendline(r"echo openparen")
|
|
|
|
expect_str(r"echo (")
|
|
|
|
send(r"?")
|
|
|
|
expect_str(r"<echo (>")
|
|
|
|
sendline(r"echo closeparen")
|
|
|
|
expect_str(r"echo )")
|
|
|
|
send(r"?")
|
|
|
|
expect_str(r"<echo )>")
|
2022-11-28 03:29:16 +08:00
|
|
|
sendline(r"echo openparen seq 5 closeparen") # expands on enter
|
2022-08-08 04:31:10 +08:00
|
|
|
expect_prompt(r"1 2 3 4 5")
|
2022-11-28 03:29:16 +08:00
|
|
|
sendline(r"echo openparen seq 5 closeparen ") # expands on space
|
|
|
|
expect_prompt(r"1 2 3 4 5")
|
|
|
|
|
2022-08-08 04:31:10 +08:00
|
|
|
|
2022-08-07 07:57:26 +08:00
|
|
|
# Verify that 'commandline' is accurate.
|
|
|
|
# Abbreviation functions cannot usefully change the command line, but they can read it.
|
|
|
|
sendline(
|
|
|
|
r"""function check_cmdline
|
|
|
|
set -g last_cmdline (commandline)
|
|
|
|
set -g last_cursor (commandline --cursor)
|
|
|
|
false
|
2022-08-08 04:31:10 +08:00
|
|
|
end
|
|
|
|
""".strip()
|
2022-08-07 07:57:26 +08:00
|
|
|
)
|
|
|
|
expect_prompt()
|
|
|
|
sendline(r"abbr check_cmdline --regex '@.+@' --function check_cmdline")
|
|
|
|
expect_prompt()
|
|
|
|
send(r"@abc@ ?")
|
|
|
|
expect_str(r"<@abc@ >")
|
2022-08-08 04:31:10 +08:00
|
|
|
sendline(r"echo $last_cursor : $last_cmdline")
|
|
|
|
expect_prompt(r"6 : @abc@ ")
|
|
|
|
|
|
|
|
|
2022-10-10 05:26:44 +08:00
|
|
|
# Test cursor positioning.
|
|
|
|
sendline(r"""abbr --erase (abbr --list) """)
|
|
|
|
expect_prompt()
|
2022-11-28 06:33:35 +08:00
|
|
|
sendline(r"""abbr LLL --position anywhere --set-cursor 'abc%ghi'""")
|
|
|
|
expect_prompt()
|
|
|
|
send(r"""echo LLL def?""")
|
|
|
|
expect_str(r"<echo abcdefghi >")
|
|
|
|
|
|
|
|
sendline(r"""abbr --erase (abbr --list) """)
|
|
|
|
expect_prompt()
|
|
|
|
sendline(r"""abbr LLL --position anywhere --set-cursor=!HERE! '!HERE! | less'""")
|
2022-10-10 05:26:44 +08:00
|
|
|
expect_prompt()
|
|
|
|
send(r"""echo LLL derp?""")
|
2022-11-28 06:33:35 +08:00
|
|
|
expect_str(r"<echo derp | less >")
|