test_cli.py 27 KB

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