test_cli.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. """
  2. These are almost end-to-end tests. They create a Prompt, feed it with some
  3. input and check the result.
  4. """
  5. from __future__ import annotations
  6. from functools import partial
  7. import pytest
  8. from prompt_toolkit.clipboard import ClipboardData, InMemoryClipboard
  9. from prompt_toolkit.enums import EditingMode
  10. from prompt_toolkit.filters import ViInsertMode
  11. from prompt_toolkit.history import InMemoryHistory
  12. from prompt_toolkit.input.defaults import create_pipe_input
  13. from prompt_toolkit.input.vt100_parser import ANSI_SEQUENCES
  14. from prompt_toolkit.key_binding.bindings.named_commands import prefix_meta
  15. from prompt_toolkit.key_binding.key_bindings import KeyBindings
  16. from prompt_toolkit.output import DummyOutput
  17. from prompt_toolkit.shortcuts import PromptSession
  18. def _history():
  19. h = InMemoryHistory()
  20. h.append_string("line1 first input")
  21. h.append_string("line2 second input")
  22. h.append_string("line3 third input")
  23. return h
  24. def _feed_cli_with_input(
  25. text,
  26. editing_mode=EditingMode.EMACS,
  27. clipboard=None,
  28. history=None,
  29. multiline=False,
  30. check_line_ending=True,
  31. key_bindings=None,
  32. ):
  33. """
  34. Create a Prompt, feed it with the given user input and return the CLI
  35. object.
  36. This returns a (result, Application) tuple.
  37. """
  38. # If the given text doesn't end with a newline, the interface won't finish.
  39. if check_line_ending:
  40. assert text.endswith("\r")
  41. with create_pipe_input() as inp:
  42. inp.send_text(text)
  43. session = PromptSession(
  44. input=inp,
  45. output=DummyOutput(),
  46. editing_mode=editing_mode,
  47. history=history,
  48. multiline=multiline,
  49. clipboard=clipboard,
  50. key_bindings=key_bindings,
  51. )
  52. _ = session.prompt()
  53. return session.default_buffer.document, session.app
  54. def test_simple_text_input():
  55. # Simple text input, followed by enter.
  56. result, cli = _feed_cli_with_input("hello\r")
  57. assert result.text == "hello"
  58. assert cli.current_buffer.text == "hello"
  59. def test_emacs_cursor_movements():
  60. """
  61. Test cursor movements with Emacs key bindings.
  62. """
  63. # ControlA (beginning-of-line)
  64. result, cli = _feed_cli_with_input("hello\x01X\r")
  65. assert result.text == "Xhello"
  66. # ControlE (end-of-line)
  67. result, cli = _feed_cli_with_input("hello\x01X\x05Y\r")
  68. assert result.text == "XhelloY"
  69. # ControlH or \b
  70. result, cli = _feed_cli_with_input("hello\x08X\r")
  71. assert result.text == "hellX"
  72. # Delete. (Left, left, delete)
  73. result, cli = _feed_cli_with_input("hello\x1b[D\x1b[D\x1b[3~\r")
  74. assert result.text == "helo"
  75. # Left.
  76. result, cli = _feed_cli_with_input("hello\x1b[DX\r")
  77. assert result.text == "hellXo"
  78. # ControlA, right
  79. result, cli = _feed_cli_with_input("hello\x01\x1b[CX\r")
  80. assert result.text == "hXello"
  81. # ControlB (backward-char)
  82. result, cli = _feed_cli_with_input("hello\x02X\r")
  83. assert result.text == "hellXo"
  84. # ControlF (forward-char)
  85. result, cli = _feed_cli_with_input("hello\x01\x06X\r")
  86. assert result.text == "hXello"
  87. # ControlD: delete after cursor.
  88. result, cli = _feed_cli_with_input("hello\x01\x04\r")
  89. assert result.text == "ello"
  90. # ControlD at the end of the input ssshould not do anything.
  91. result, cli = _feed_cli_with_input("hello\x04\r")
  92. assert result.text == "hello"
  93. # Left, Left, ControlK (kill-line)
  94. result, cli = _feed_cli_with_input("hello\x1b[D\x1b[D\x0b\r")
  95. assert result.text == "hel"
  96. # Left, Left Esc- ControlK (kill-line, but negative)
  97. result, cli = _feed_cli_with_input("hello\x1b[D\x1b[D\x1b-\x0b\r")
  98. assert result.text == "lo"
  99. # ControlL: should not influence the result.
  100. result, cli = _feed_cli_with_input("hello\x0c\r")
  101. assert result.text == "hello"
  102. # ControlRight (forward-word)
  103. result, cli = _feed_cli_with_input("hello world\x01X\x1b[1;5CY\r")
  104. assert result.text == "XhelloY world"
  105. # ContrlolLeft (backward-word)
  106. result, cli = _feed_cli_with_input("hello world\x1b[1;5DY\r")
  107. assert result.text == "hello Yworld"
  108. # <esc>-f with argument. (forward-word)
  109. result, cli = _feed_cli_with_input("hello world abc def\x01\x1b3\x1bfX\r")
  110. assert result.text == "hello world abcX def"
  111. # <esc>-f with negative argument. (forward-word)
  112. result, cli = _feed_cli_with_input("hello world abc def\x1b-\x1b3\x1bfX\r")
  113. assert result.text == "hello Xworld abc def"
  114. # <esc>-b with argument. (backward-word)
  115. result, cli = _feed_cli_with_input("hello world abc def\x1b3\x1bbX\r")
  116. assert result.text == "hello Xworld abc def"
  117. # <esc>-b with negative argument. (backward-word)
  118. result, cli = _feed_cli_with_input("hello world abc def\x01\x1b-\x1b3\x1bbX\r")
  119. assert result.text == "hello world abc Xdef"
  120. # ControlW (kill-word / unix-word-rubout)
  121. result, cli = _feed_cli_with_input("hello world\x17\r")
  122. assert result.text == "hello "
  123. assert cli.clipboard.get_data().text == "world"
  124. result, cli = _feed_cli_with_input("test hello world\x1b2\x17\r")
  125. assert result.text == "test "
  126. # Escape Backspace (unix-word-rubout)
  127. result, cli = _feed_cli_with_input("hello world\x1b\x7f\r")
  128. assert result.text == "hello "
  129. assert cli.clipboard.get_data().text == "world"
  130. result, cli = _feed_cli_with_input("hello world\x1b\x08\r")
  131. assert result.text == "hello "
  132. assert cli.clipboard.get_data().text == "world"
  133. # Backspace (backward-delete-char)
  134. result, cli = _feed_cli_with_input("hello world\x7f\r")
  135. assert result.text == "hello worl"
  136. assert result.cursor_position == len("hello worl")
  137. result, cli = _feed_cli_with_input("hello world\x08\r")
  138. assert result.text == "hello worl"
  139. assert result.cursor_position == len("hello worl")
  140. # Delete (delete-char)
  141. result, cli = _feed_cli_with_input("hello world\x01\x1b[3~\r")
  142. assert result.text == "ello world"
  143. assert result.cursor_position == 0
  144. # Escape-\\ (delete-horizontal-space)
  145. result, cli = _feed_cli_with_input("hello world\x1b8\x02\x1b\\\r")
  146. assert result.text == "helloworld"
  147. assert result.cursor_position == len("hello")
  148. def test_emacs_kill_multiple_words_and_paste():
  149. # Using control-w twice should place both words on the clipboard.
  150. result, cli = _feed_cli_with_input(
  151. "hello world test\x17\x17--\x19\x19\r" # Twice c-w. Twice c-y.
  152. )
  153. assert result.text == "hello --world testworld test"
  154. assert cli.clipboard.get_data().text == "world test"
  155. # Using alt-d twice should place both words on the clipboard.
  156. result, cli = _feed_cli_with_input(
  157. "hello world test"
  158. "\x1bb\x1bb" # Twice left.
  159. "\x1bd\x1bd" # Twice kill-word.
  160. "abc"
  161. "\x19" # Paste.
  162. "\r"
  163. )
  164. assert result.text == "hello abcworld test"
  165. assert cli.clipboard.get_data().text == "world test"
  166. def test_interrupts():
  167. # ControlC: raise KeyboardInterrupt.
  168. with pytest.raises(KeyboardInterrupt):
  169. result, cli = _feed_cli_with_input("hello\x03\r")
  170. with pytest.raises(KeyboardInterrupt):
  171. result, cli = _feed_cli_with_input("hello\x03\r")
  172. # ControlD without any input: raises EOFError.
  173. with pytest.raises(EOFError):
  174. result, cli = _feed_cli_with_input("\x04\r")
  175. def test_emacs_yank():
  176. # ControlY (yank)
  177. c = InMemoryClipboard(ClipboardData("XYZ"))
  178. result, cli = _feed_cli_with_input("hello\x02\x19\r", clipboard=c)
  179. assert result.text == "hellXYZo"
  180. assert result.cursor_position == len("hellXYZ")
  181. def test_quoted_insert():
  182. # ControlQ - ControlB (quoted-insert)
  183. result, cli = _feed_cli_with_input("hello\x11\x02\r")
  184. assert result.text == "hello\x02"
  185. def test_transformations():
  186. # Meta-c (capitalize-word)
  187. result, cli = _feed_cli_with_input("hello world\01\x1bc\r")
  188. assert result.text == "Hello world"
  189. assert result.cursor_position == len("Hello")
  190. # Meta-u (uppercase-word)
  191. result, cli = _feed_cli_with_input("hello world\01\x1bu\r")
  192. assert result.text == "HELLO world"
  193. assert result.cursor_position == len("Hello")
  194. # Meta-u (downcase-word)
  195. result, cli = _feed_cli_with_input("HELLO WORLD\01\x1bl\r")
  196. assert result.text == "hello WORLD"
  197. assert result.cursor_position == len("Hello")
  198. # ControlT (transpose-chars)
  199. result, cli = _feed_cli_with_input("hello\x14\r")
  200. assert result.text == "helol"
  201. assert result.cursor_position == len("hello")
  202. # Left, Left, Control-T (transpose-chars)
  203. result, cli = _feed_cli_with_input("abcde\x1b[D\x1b[D\x14\r")
  204. assert result.text == "abdce"
  205. assert result.cursor_position == len("abcd")
  206. def test_emacs_other_bindings():
  207. # Transpose characters.
  208. result, cli = _feed_cli_with_input("abcde\x14X\r") # Ctrl-T
  209. assert result.text == "abcedX"
  210. # Left, Left, Transpose. (This is slightly different.)
  211. result, cli = _feed_cli_with_input("abcde\x1b[D\x1b[D\x14X\r")
  212. assert result.text == "abdcXe"
  213. # Clear before cursor.
  214. result, cli = _feed_cli_with_input("hello\x1b[D\x1b[D\x15X\r")
  215. assert result.text == "Xlo"
  216. # unix-word-rubout: delete word before the cursor.
  217. # (ControlW).
  218. result, cli = _feed_cli_with_input("hello world test\x17X\r")
  219. assert result.text == "hello world X"
  220. result, cli = _feed_cli_with_input("hello world /some/very/long/path\x17X\r")
  221. assert result.text == "hello world X"
  222. # (with argument.)
  223. result, cli = _feed_cli_with_input("hello world test\x1b2\x17X\r")
  224. assert result.text == "hello X"
  225. result, cli = _feed_cli_with_input("hello world /some/very/long/path\x1b2\x17X\r")
  226. assert result.text == "hello X"
  227. # backward-kill-word: delete word before the cursor.
  228. # (Esc-ControlH).
  229. result, cli = _feed_cli_with_input("hello world /some/very/long/path\x1b\x08X\r")
  230. assert result.text == "hello world /some/very/long/X"
  231. # (with arguments.)
  232. result, cli = _feed_cli_with_input(
  233. "hello world /some/very/long/path\x1b3\x1b\x08X\r"
  234. )
  235. assert result.text == "hello world /some/very/X"
  236. def test_controlx_controlx():
  237. # At the end: go to the start of the line.
  238. result, cli = _feed_cli_with_input("hello world\x18\x18X\r")
  239. assert result.text == "Xhello world"
  240. assert result.cursor_position == 1
  241. # At the start: go to the end of the line.
  242. result, cli = _feed_cli_with_input("hello world\x01\x18\x18X\r")
  243. assert result.text == "hello worldX"
  244. # Left, Left Control-X Control-X: go to the end of the line.
  245. result, cli = _feed_cli_with_input("hello world\x1b[D\x1b[D\x18\x18X\r")
  246. assert result.text == "hello worldX"
  247. def test_emacs_history_bindings():
  248. # Adding a new item to the history.
  249. history = _history()
  250. result, cli = _feed_cli_with_input("new input\r", history=history)
  251. assert result.text == "new input"
  252. history.get_strings()[-1] == "new input"
  253. # Go up in history, and accept the last item.
  254. result, cli = _feed_cli_with_input("hello\x1b[A\r", history=history)
  255. assert result.text == "new input"
  256. # Esc< (beginning-of-history)
  257. result, cli = _feed_cli_with_input("hello\x1b<\r", history=history)
  258. assert result.text == "line1 first input"
  259. # Esc> (end-of-history)
  260. result, cli = _feed_cli_with_input(
  261. "another item\x1b[A\x1b[a\x1b>\r", history=history
  262. )
  263. assert result.text == "another item"
  264. # ControlUp (previous-history)
  265. result, cli = _feed_cli_with_input("\x1b[1;5A\r", history=history)
  266. assert result.text == "another item"
  267. # Esc< ControlDown (beginning-of-history, next-history)
  268. result, cli = _feed_cli_with_input("\x1b<\x1b[1;5B\r", history=history)
  269. assert result.text == "line2 second input"
  270. def test_emacs_reverse_search():
  271. history = _history()
  272. # ControlR (reverse-search-history)
  273. result, cli = _feed_cli_with_input("\x12input\r\r", history=history)
  274. assert result.text == "line3 third input"
  275. # Hitting ControlR twice.
  276. result, cli = _feed_cli_with_input("\x12input\x12\r\r", history=history)
  277. assert result.text == "line2 second input"
  278. def test_emacs_arguments():
  279. """
  280. Test various combinations of arguments in Emacs mode.
  281. """
  282. # esc 4
  283. result, cli = _feed_cli_with_input("\x1b4x\r")
  284. assert result.text == "xxxx"
  285. # esc 4 4
  286. result, cli = _feed_cli_with_input("\x1b44x\r")
  287. assert result.text == "x" * 44
  288. # esc 4 esc 4
  289. result, cli = _feed_cli_with_input("\x1b4\x1b4x\r")
  290. assert result.text == "x" * 44
  291. # esc - right (-1 position to the right, equals 1 to the left.)
  292. result, cli = _feed_cli_with_input("aaaa\x1b-\x1b[Cbbbb\r")
  293. assert result.text == "aaabbbba"
  294. # esc - 3 right
  295. result, cli = _feed_cli_with_input("aaaa\x1b-3\x1b[Cbbbb\r")
  296. assert result.text == "abbbbaaa"
  297. # esc - - - 3 right
  298. result, cli = _feed_cli_with_input("aaaa\x1b---3\x1b[Cbbbb\r")
  299. assert result.text == "abbbbaaa"
  300. def test_emacs_arguments_for_all_commands():
  301. """
  302. Test all Emacs commands with Meta-[0-9] arguments (both positive and
  303. negative). No one should crash.
  304. """
  305. for key in ANSI_SEQUENCES:
  306. # Ignore BracketedPaste. This would hang forever, because it waits for
  307. # the end sequence.
  308. if key != "\x1b[200~":
  309. try:
  310. # Note: we add an 'X' after the key, because Ctrl-Q (quoted-insert)
  311. # expects something to follow. We add an additional \r, because
  312. # Ctrl-R and Ctrl-S (reverse-search) expect that.
  313. result, cli = _feed_cli_with_input("hello\x1b4" + key + "X\r\r")
  314. result, cli = _feed_cli_with_input("hello\x1b-" + key + "X\r\r")
  315. except KeyboardInterrupt:
  316. # This exception should only be raised for Ctrl-C
  317. assert key == "\x03"
  318. def test_emacs_kill_ring():
  319. operations = (
  320. # abc ControlA ControlK
  321. "abc\x01\x0b"
  322. # def ControlA ControlK
  323. "def\x01\x0b"
  324. # ghi ControlA ControlK
  325. "ghi\x01\x0b"
  326. # ControlY (yank)
  327. "\x19"
  328. )
  329. result, cli = _feed_cli_with_input(operations + "\r")
  330. assert result.text == "ghi"
  331. result, cli = _feed_cli_with_input(operations + "\x1by\r")
  332. assert result.text == "def"
  333. result, cli = _feed_cli_with_input(operations + "\x1by\x1by\r")
  334. assert result.text == "abc"
  335. result, cli = _feed_cli_with_input(operations + "\x1by\x1by\x1by\r")
  336. assert result.text == "ghi"
  337. def test_emacs_selection():
  338. # Copy/paste empty selection should not do anything.
  339. operations = (
  340. "hello"
  341. # Twice left.
  342. "\x1b[D\x1b[D"
  343. # Control-Space
  344. "\x00"
  345. # ControlW (cut)
  346. "\x17"
  347. # ControlY twice. (paste twice)
  348. "\x19\x19\r"
  349. )
  350. result, cli = _feed_cli_with_input(operations)
  351. assert result.text == "hello"
  352. # Copy/paste one character.
  353. operations = (
  354. "hello"
  355. # Twice left.
  356. "\x1b[D\x1b[D"
  357. # Control-Space
  358. "\x00"
  359. # Right.
  360. "\x1b[C"
  361. # ControlW (cut)
  362. "\x17"
  363. # ControlA (Home).
  364. "\x01"
  365. # ControlY (paste)
  366. "\x19\r"
  367. )
  368. result, cli = _feed_cli_with_input(operations)
  369. assert result.text == "lhelo"
  370. def test_emacs_insert_comment():
  371. # Test insert-comment (M-#) binding.
  372. result, cli = _feed_cli_with_input("hello\x1b#", check_line_ending=False)
  373. assert result.text == "#hello"
  374. result, cli = _feed_cli_with_input(
  375. "hello\rworld\x1b#", check_line_ending=False, multiline=True
  376. )
  377. assert result.text == "#hello\n#world"
  378. def test_emacs_record_macro():
  379. operations = (
  380. " "
  381. "\x18(" # Start recording macro. C-X(
  382. "hello"
  383. "\x18)" # Stop recording macro.
  384. " "
  385. "\x18e" # Execute macro.
  386. "\x18e" # Execute macro.
  387. "\r"
  388. )
  389. result, cli = _feed_cli_with_input(operations)
  390. assert result.text == " hello hellohello"
  391. def test_emacs_nested_macro():
  392. "Test calling the macro within a macro."
  393. # Calling a macro within a macro should take the previous recording (if one
  394. # exists), not the one that is in progress.
  395. operations = (
  396. "\x18(" # Start recording macro. C-X(
  397. "hello"
  398. "\x18e" # Execute macro.
  399. "\x18)" # Stop recording macro.
  400. "\x18e" # Execute macro.
  401. "\r"
  402. )
  403. result, cli = _feed_cli_with_input(operations)
  404. assert result.text == "hellohello"
  405. operations = (
  406. "\x18(" # Start recording macro. C-X(
  407. "hello"
  408. "\x18)" # Stop recording macro.
  409. "\x18(" # Start recording macro. C-X(
  410. "\x18e" # Execute macro.
  411. "world"
  412. "\x18)" # Stop recording macro.
  413. "\x01\x0b" # Delete all (c-a c-k).
  414. "\x18e" # Execute macro.
  415. "\r"
  416. )
  417. result, cli = _feed_cli_with_input(operations)
  418. assert result.text == "helloworld"
  419. def test_prefix_meta():
  420. # Test the prefix-meta command.
  421. b = KeyBindings()
  422. b.add("j", "j", filter=ViInsertMode())(prefix_meta)
  423. result, cli = _feed_cli_with_input(
  424. "hellojjIX\r", key_bindings=b, editing_mode=EditingMode.VI
  425. )
  426. assert result.text == "Xhello"
  427. def test_bracketed_paste():
  428. result, cli = _feed_cli_with_input("\x1b[200~hello world\x1b[201~\r")
  429. assert result.text == "hello world"
  430. result, cli = _feed_cli_with_input("\x1b[200~hello\rworld\x1b[201~\x1b\r")
  431. assert result.text == "hello\nworld"
  432. # With \r\n endings.
  433. result, cli = _feed_cli_with_input("\x1b[200~hello\r\nworld\x1b[201~\x1b\r")
  434. assert result.text == "hello\nworld"
  435. # With \n endings.
  436. result, cli = _feed_cli_with_input("\x1b[200~hello\nworld\x1b[201~\x1b\r")
  437. assert result.text == "hello\nworld"
  438. def test_vi_cursor_movements():
  439. """
  440. Test cursor movements with Vi key bindings.
  441. """
  442. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  443. result, cli = feed("\x1b\r")
  444. assert result.text == ""
  445. assert cli.editing_mode == EditingMode.VI
  446. # Esc h a X
  447. result, cli = feed("hello\x1bhaX\r")
  448. assert result.text == "hellXo"
  449. # Esc I X
  450. result, cli = feed("hello\x1bIX\r")
  451. assert result.text == "Xhello"
  452. # Esc I X
  453. result, cli = feed("hello\x1bIX\r")
  454. assert result.text == "Xhello"
  455. # Esc 2hiX
  456. result, cli = feed("hello\x1b2hiX\r")
  457. assert result.text == "heXllo"
  458. # Esc 2h2liX
  459. result, cli = feed("hello\x1b2h2liX\r")
  460. assert result.text == "hellXo"
  461. # Esc \b\b
  462. result, cli = feed("hello\b\b\r")
  463. assert result.text == "hel"
  464. # Esc \b\b
  465. result, cli = feed("hello\b\b\r")
  466. assert result.text == "hel"
  467. # Esc 2h D
  468. result, cli = feed("hello\x1b2hD\r")
  469. assert result.text == "he"
  470. # Esc 2h rX \r
  471. result, cli = feed("hello\x1b2hrX\r")
  472. assert result.text == "heXlo"
  473. def test_vi_operators():
  474. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  475. # Esc g~0
  476. result, cli = feed("hello\x1bg~0\r")
  477. assert result.text == "HELLo"
  478. # Esc gU0
  479. result, cli = feed("hello\x1bgU0\r")
  480. assert result.text == "HELLo"
  481. # Esc d0
  482. result, cli = feed("hello\x1bd0\r")
  483. assert result.text == "o"
  484. def test_vi_text_objects():
  485. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  486. # Esc gUgg
  487. result, cli = feed("hello\x1bgUgg\r")
  488. assert result.text == "HELLO"
  489. # Esc gUU
  490. result, cli = feed("hello\x1bgUU\r")
  491. assert result.text == "HELLO"
  492. # Esc di(
  493. result, cli = feed("before(inside)after\x1b8hdi(\r")
  494. assert result.text == "before()after"
  495. # Esc di[
  496. result, cli = feed("before[inside]after\x1b8hdi[\r")
  497. assert result.text == "before[]after"
  498. # Esc da(
  499. result, cli = feed("before(inside)after\x1b8hda(\r")
  500. assert result.text == "beforeafter"
  501. def test_vi_digraphs():
  502. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  503. # C-K o/
  504. result, cli = feed("hello\x0bo/\r")
  505. assert result.text == "helloø"
  506. # C-K /o (reversed input.)
  507. result, cli = feed("hello\x0b/o\r")
  508. assert result.text == "helloø"
  509. # C-K e:
  510. result, cli = feed("hello\x0be:\r")
  511. assert result.text == "helloë"
  512. # C-K xxy (Unknown digraph.)
  513. result, cli = feed("hello\x0bxxy\r")
  514. assert result.text == "helloy"
  515. def test_vi_block_editing():
  516. "Test Vi Control-V style block insertion."
  517. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  518. operations = (
  519. # Six lines of text.
  520. "-line1\r-line2\r-line3\r-line4\r-line5\r-line6"
  521. # Go to the second character of the second line.
  522. "\x1bkkkkkkkj0l"
  523. # Enter Visual block mode.
  524. "\x16"
  525. # Go down two more lines.
  526. "jj"
  527. # Go 3 characters to the right.
  528. "lll"
  529. # Go to insert mode.
  530. "insert" # (Will be replaced.)
  531. # Insert stars.
  532. "***"
  533. # Escape again.
  534. "\x1b\r"
  535. )
  536. # Control-I
  537. result, cli = feed(operations.replace("insert", "I"))
  538. assert result.text == "-line1\n-***line2\n-***line3\n-***line4\n-line5\n-line6"
  539. # Control-A
  540. result, cli = feed(operations.replace("insert", "A"))
  541. assert result.text == "-line1\n-line***2\n-line***3\n-line***4\n-line5\n-line6"
  542. def test_vi_block_editing_empty_lines():
  543. "Test block editing on empty lines."
  544. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  545. operations = (
  546. # Six empty lines.
  547. "\r\r\r\r\r"
  548. # Go to beginning of the document.
  549. "\x1bgg"
  550. # Enter Visual block mode.
  551. "\x16"
  552. # Go down two more lines.
  553. "jj"
  554. # Go 3 characters to the right.
  555. "lll"
  556. # Go to insert mode.
  557. "insert" # (Will be replaced.)
  558. # Insert stars.
  559. "***"
  560. # Escape again.
  561. "\x1b\r"
  562. )
  563. # Control-I
  564. result, cli = feed(operations.replace("insert", "I"))
  565. assert result.text == "***\n***\n***\n\n\n"
  566. # Control-A
  567. result, cli = feed(operations.replace("insert", "A"))
  568. assert result.text == "***\n***\n***\n\n\n"
  569. def test_vi_visual_line_copy():
  570. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  571. operations = (
  572. # Three lines of text.
  573. "-line1\r-line2\r-line3\r-line4\r-line5\r-line6"
  574. # Go to the second character of the second line.
  575. "\x1bkkkkkkkj0l"
  576. # Enter Visual linemode.
  577. "V"
  578. # Go down one line.
  579. "j"
  580. # Go 3 characters to the right (should not do much).
  581. "lll"
  582. # Copy this block.
  583. "y"
  584. # Go down one line.
  585. "j"
  586. # Insert block twice.
  587. "2p"
  588. # Escape again.
  589. "\x1b\r"
  590. )
  591. result, cli = feed(operations)
  592. assert (
  593. result.text
  594. == "-line1\n-line2\n-line3\n-line4\n-line2\n-line3\n-line2\n-line3\n-line5\n-line6"
  595. )
  596. def test_vi_visual_empty_line():
  597. """
  598. Test edge case with an empty line in Visual-line mode.
  599. """
  600. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  601. # 1. Delete first two lines.
  602. operations = (
  603. # Three lines of text. The middle one is empty.
  604. "hello\r\rworld"
  605. # Go to the start.
  606. "\x1bgg"
  607. # Visual line and move down.
  608. "Vj"
  609. # Delete.
  610. "d\r"
  611. )
  612. result, cli = feed(operations)
  613. assert result.text == "world"
  614. # 1. Delete middle line.
  615. operations = (
  616. # Three lines of text. The middle one is empty.
  617. "hello\r\rworld"
  618. # Go to middle line.
  619. "\x1bggj"
  620. # Delete line
  621. "Vd\r"
  622. )
  623. result, cli = feed(operations)
  624. assert result.text == "hello\nworld"
  625. def test_vi_character_delete_after_cursor():
  626. "Test 'x' keypress."
  627. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  628. # Delete one character.
  629. result, cli = feed("abcd\x1bHx\r")
  630. assert result.text == "bcd"
  631. # Delete multiple character.s
  632. result, cli = feed("abcd\x1bH3x\r")
  633. assert result.text == "d"
  634. # Delete on empty line.
  635. result, cli = feed("\x1bo\x1bo\x1bggx\r")
  636. assert result.text == "\n\n"
  637. # Delete multiple on empty line.
  638. result, cli = feed("\x1bo\x1bo\x1bgg10x\r")
  639. assert result.text == "\n\n"
  640. # Delete multiple on empty line.
  641. result, cli = feed("hello\x1bo\x1bo\x1bgg3x\r")
  642. assert result.text == "lo\n\n"
  643. def test_vi_character_delete_before_cursor():
  644. "Test 'X' keypress."
  645. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)
  646. # Delete one character.
  647. result, cli = feed("abcd\x1bX\r")
  648. assert result.text == "abd"
  649. # Delete multiple character.
  650. result, cli = feed("hello world\x1b3X\r")
  651. assert result.text == "hello wd"
  652. # Delete multiple character on multiple lines.
  653. result, cli = feed("hello\x1boworld\x1bgg$3X\r")
  654. assert result.text == "ho\nworld"
  655. result, cli = feed("hello\x1boworld\x1b100X\r")
  656. assert result.text == "hello\nd"
  657. # Delete on empty line.
  658. result, cli = feed("\x1bo\x1bo\x1b10X\r")
  659. assert result.text == "\n\n"
  660. def test_vi_character_paste():
  661. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  662. # Test 'p' character paste.
  663. result, cli = feed("abcde\x1bhhxp\r")
  664. assert result.text == "abdce"
  665. assert result.cursor_position == 3
  666. # Test 'P' character paste.
  667. result, cli = feed("abcde\x1bhhxP\r")
  668. assert result.text == "abcde"
  669. assert result.cursor_position == 2
  670. def test_vi_temp_navigation_mode():
  671. """
  672. Test c-o binding: go for one action into navigation mode.
  673. """
  674. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  675. result, cli = feed("abcde\x0f3hx\r") # c-o # 3 times to the left.
  676. assert result.text == "axbcde"
  677. assert result.cursor_position == 2
  678. result, cli = feed("abcde\x0fbx\r") # c-o # One word backwards.
  679. assert result.text == "xabcde"
  680. assert result.cursor_position == 1
  681. # In replace mode
  682. result, cli = feed(
  683. "abcdef"
  684. "\x1b" # Navigation mode.
  685. "0l" # Start of line, one character to the right.
  686. "R" # Replace mode
  687. "78"
  688. "\x0f" # c-o
  689. "l" # One character forwards.
  690. "9\r"
  691. )
  692. assert result.text == "a78d9f"
  693. assert result.cursor_position == 5
  694. def test_vi_macros():
  695. feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)
  696. # Record and execute macro.
  697. result, cli = feed("\x1bqcahello\x1bq@c\r")
  698. assert result.text == "hellohello"
  699. assert result.cursor_position == 9
  700. # Running unknown macro.
  701. result, cli = feed("\x1b@d\r")
  702. assert result.text == ""
  703. assert result.cursor_position == 0
  704. # When a macro is called within a macro.
  705. # It shouldn't result in eternal recursion.
  706. result, cli = feed("\x1bqxahello\x1b@xq@x\r")
  707. assert result.text == "hellohello"
  708. assert result.cursor_position == 9
  709. # Nested macros.
  710. result, cli = feed(
  711. # Define macro 'x'.
  712. "\x1bqxahello\x1bq"
  713. # Define macro 'y' which calls 'x'.
  714. "qya\x1b@xaworld\x1bq"
  715. # Delete line.
  716. "2dd"
  717. # Execute 'y'
  718. "@y\r"
  719. )
  720. assert result.text == "helloworld"
  721. def test_accept_default():
  722. """
  723. Test `prompt(accept_default=True)`.
  724. """
  725. with create_pipe_input() as inp:
  726. session = PromptSession(input=inp, output=DummyOutput())
  727. result = session.prompt(default="hello", accept_default=True)
  728. assert result == "hello"
  729. # Test calling prompt() for a second time. (We had an issue where the
  730. # prompt reset between calls happened at the wrong time, breaking this.)
  731. result = session.prompt(default="world", accept_default=True)
  732. assert result == "world"