menu_spec.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear, command = n.clear, n.command
  4. local expect, feed = n.expect, n.feed
  5. local eq, eval = t.eq, n.eval
  6. local fn = n.fn
  7. describe(':emenu', function()
  8. before_each(function()
  9. clear()
  10. command('nnoremenu Test.Test inormal<ESC>')
  11. command('inoremenu Test.Test insert')
  12. command('vnoremenu Test.Test x')
  13. command('cnoremenu Test.Test cmdmode')
  14. command('nnoremenu Edit.Paste p')
  15. command('cnoremenu Edit.Paste <C-R>"')
  16. end)
  17. it('executes correct bindings in normal mode without using API', function()
  18. command('emenu Test.Test')
  19. expect('normal')
  20. end)
  21. it('executes correct bindings in normal mode', function()
  22. command('emenu Test.Test')
  23. expect('normal')
  24. end)
  25. it('executes correct bindings in insert mode', function()
  26. feed('i')
  27. command('emenu Test.Test')
  28. expect('insert')
  29. end)
  30. it('executes correct bindings in visual mode', function()
  31. feed('iabcde<ESC>0lvll')
  32. command('emenu Test.Test')
  33. expect('ae')
  34. end)
  35. it('executes correct bindings in command mode', function()
  36. feed('ithis is a sentence<esc>^yiwo<esc>')
  37. -- Invoke "Edit.Paste" in normal-mode.
  38. command('emenu Edit.Paste')
  39. -- Invoke "Edit.Paste" and "Test.Test" in command-mode.
  40. feed(':')
  41. command('emenu Edit.Paste')
  42. command('emenu Test.Test')
  43. expect([[
  44. this is a sentence
  45. this]])
  46. -- Assert that Edit.Paste pasted @" into the commandline.
  47. eq('thiscmdmode', eval('getcmdline()'))
  48. end)
  49. end)
  50. describe('menu_get', function()
  51. before_each(function()
  52. clear()
  53. command([=[
  54. aunmenu *
  55. nnoremenu &Test.Test inormal<ESC>
  56. inoremenu Test.Test insert
  57. vnoremenu Test.Test x
  58. cnoremenu Test.Test cmdmode
  59. menu Test.Nested.test level1
  60. menu Test.Nested.Nested2 level2
  61. nnoremenu <script> Export.Script p
  62. tmenu Export.Script This is the tooltip
  63. menu ]Export.hidden thisoneshouldbehidden
  64. nnoremenu Edit.Paste p
  65. cnoremenu Edit.Paste <C-R>"
  66. ]=])
  67. end)
  68. it("path='', modes='a'", function()
  69. local m = fn.menu_get('', 'a')
  70. -- HINT: To print the expected table and regenerate the tests:
  71. -- print(require('vim.inspect')(m))
  72. local expected = {
  73. {
  74. shortcut = 'T',
  75. hidden = 0,
  76. submenus = {
  77. {
  78. mappings = {
  79. i = {
  80. sid = 1,
  81. noremap = 1,
  82. enabled = 1,
  83. rhs = 'insert',
  84. silent = 0,
  85. },
  86. s = {
  87. sid = 1,
  88. noremap = 1,
  89. enabled = 1,
  90. rhs = 'x',
  91. silent = 0,
  92. },
  93. n = {
  94. sid = 1,
  95. noremap = 1,
  96. enabled = 1,
  97. rhs = 'inormal<Esc>',
  98. silent = 0,
  99. },
  100. v = {
  101. sid = 1,
  102. noremap = 1,
  103. enabled = 1,
  104. rhs = 'x',
  105. silent = 0,
  106. },
  107. c = {
  108. sid = 1,
  109. noremap = 1,
  110. enabled = 1,
  111. rhs = 'cmdmode',
  112. silent = 0,
  113. },
  114. },
  115. priority = 500,
  116. name = 'Test',
  117. hidden = 0,
  118. },
  119. {
  120. priority = 500,
  121. name = 'Nested',
  122. submenus = {
  123. {
  124. mappings = {
  125. o = {
  126. sid = 0,
  127. noremap = 0,
  128. enabled = 1,
  129. rhs = 'level1',
  130. silent = 0,
  131. },
  132. v = {
  133. sid = 0,
  134. noremap = 0,
  135. enabled = 1,
  136. rhs = 'level1',
  137. silent = 0,
  138. },
  139. s = {
  140. sid = 0,
  141. noremap = 0,
  142. enabled = 1,
  143. rhs = 'level1',
  144. silent = 0,
  145. },
  146. n = {
  147. sid = 0,
  148. noremap = 0,
  149. enabled = 1,
  150. rhs = 'level1',
  151. silent = 0,
  152. },
  153. },
  154. priority = 500,
  155. name = 'test',
  156. hidden = 0,
  157. },
  158. {
  159. mappings = {
  160. o = {
  161. sid = 0,
  162. noremap = 0,
  163. enabled = 1,
  164. rhs = 'level2',
  165. silent = 0,
  166. },
  167. v = {
  168. sid = 0,
  169. noremap = 0,
  170. enabled = 1,
  171. rhs = 'level2',
  172. silent = 0,
  173. },
  174. s = {
  175. sid = 0,
  176. noremap = 0,
  177. enabled = 1,
  178. rhs = 'level2',
  179. silent = 0,
  180. },
  181. n = {
  182. sid = 0,
  183. noremap = 0,
  184. enabled = 1,
  185. rhs = 'level2',
  186. silent = 0,
  187. },
  188. },
  189. priority = 500,
  190. name = 'Nested2',
  191. hidden = 0,
  192. },
  193. },
  194. hidden = 0,
  195. },
  196. },
  197. priority = 500,
  198. name = 'Test',
  199. },
  200. {
  201. priority = 500,
  202. name = 'Export',
  203. submenus = {
  204. {
  205. tooltip = 'This is the tooltip',
  206. hidden = 0,
  207. name = 'Script',
  208. priority = 500,
  209. mappings = {
  210. n = {
  211. sid = 1,
  212. noremap = 1,
  213. enabled = 1,
  214. rhs = 'p',
  215. silent = 0,
  216. },
  217. },
  218. },
  219. },
  220. hidden = 0,
  221. },
  222. {
  223. priority = 500,
  224. name = 'Edit',
  225. submenus = {
  226. {
  227. mappings = {
  228. c = {
  229. sid = 1,
  230. noremap = 1,
  231. enabled = 1,
  232. rhs = '<C-R>"',
  233. silent = 0,
  234. },
  235. n = {
  236. sid = 1,
  237. noremap = 1,
  238. enabled = 1,
  239. rhs = 'p',
  240. silent = 0,
  241. },
  242. },
  243. priority = 500,
  244. name = 'Paste',
  245. hidden = 0,
  246. },
  247. },
  248. hidden = 0,
  249. },
  250. {
  251. priority = 500,
  252. name = ']Export',
  253. submenus = {
  254. {
  255. mappings = {
  256. o = {
  257. sid = 0,
  258. noremap = 0,
  259. enabled = 1,
  260. rhs = 'thisoneshouldbehidden',
  261. silent = 0,
  262. },
  263. v = {
  264. sid = 0,
  265. noremap = 0,
  266. enabled = 1,
  267. rhs = 'thisoneshouldbehidden',
  268. silent = 0,
  269. },
  270. s = {
  271. sid = 0,
  272. noremap = 0,
  273. enabled = 1,
  274. rhs = 'thisoneshouldbehidden',
  275. silent = 0,
  276. },
  277. n = {
  278. sid = 0,
  279. noremap = 0,
  280. enabled = 1,
  281. rhs = 'thisoneshouldbehidden',
  282. silent = 0,
  283. },
  284. },
  285. priority = 500,
  286. name = 'hidden',
  287. hidden = 0,
  288. },
  289. },
  290. hidden = 1,
  291. },
  292. }
  293. eq(expected, m)
  294. end)
  295. it('matching path, all modes', function()
  296. local m = fn.menu_get('Export', 'a')
  297. local expected = {
  298. {
  299. hidden = 0,
  300. name = 'Export',
  301. priority = 500,
  302. submenus = {
  303. {
  304. tooltip = 'This is the tooltip',
  305. hidden = 0,
  306. name = 'Script',
  307. priority = 500,
  308. mappings = {
  309. n = {
  310. sid = 1,
  311. noremap = 1,
  312. enabled = 1,
  313. rhs = 'p',
  314. silent = 0,
  315. },
  316. },
  317. },
  318. },
  319. },
  320. }
  321. eq(expected, m)
  322. end)
  323. it('no path, matching modes', function()
  324. local m = fn.menu_get('', 'i')
  325. local expected = {
  326. {
  327. shortcut = 'T',
  328. hidden = 0,
  329. submenus = {
  330. {
  331. mappings = {
  332. i = {
  333. sid = 1,
  334. noremap = 1,
  335. enabled = 1,
  336. rhs = 'insert',
  337. silent = 0,
  338. },
  339. },
  340. priority = 500,
  341. name = 'Test',
  342. hidden = 0,
  343. },
  344. },
  345. priority = 500,
  346. name = 'Test',
  347. },
  348. }
  349. eq(expected, m)
  350. end)
  351. it('matching path and modes', function()
  352. local m = fn.menu_get('Test', 'i')
  353. local expected = {
  354. {
  355. shortcut = 'T',
  356. submenus = {
  357. {
  358. mappings = {
  359. i = {
  360. sid = 1,
  361. noremap = 1,
  362. enabled = 1,
  363. rhs = 'insert',
  364. silent = 0,
  365. },
  366. },
  367. priority = 500,
  368. name = 'Test',
  369. hidden = 0,
  370. },
  371. },
  372. priority = 500,
  373. name = 'Test',
  374. hidden = 0,
  375. },
  376. }
  377. eq(expected, m)
  378. end)
  379. end)
  380. describe('menu_get', function()
  381. before_each(function()
  382. clear()
  383. command('aunmenu *')
  384. end)
  385. it('returns <keycode> representation of special keys', function()
  386. command('nnoremenu &Test.Test inormal<ESC>')
  387. command('inoremenu &Test.Test2 <Tab><Esc>')
  388. command('vnoremenu &Test.Test3 yA<C-R>0<Tab>xyz<Esc>')
  389. command('inoremenu &Test.Test4 <c-r>*')
  390. command('inoremenu &Test.Test5 <c-R>+')
  391. command('nnoremenu &Test.Test6 <Nop>')
  392. command('nnoremenu &Test.Test7 <NOP>')
  393. command('nnoremenu &Test.Test8 <NoP>')
  394. command('nnoremenu &Test.Test9 ""')
  395. local m = fn.menu_get('')
  396. local expected = {
  397. {
  398. shortcut = 'T',
  399. hidden = 0,
  400. submenus = {
  401. {
  402. priority = 500,
  403. mappings = {
  404. n = {
  405. sid = 1,
  406. noremap = 1,
  407. enabled = 1,
  408. rhs = 'inormal<Esc>',
  409. silent = 0,
  410. },
  411. },
  412. name = 'Test',
  413. hidden = 0,
  414. },
  415. {
  416. priority = 500,
  417. mappings = {
  418. i = {
  419. sid = 1,
  420. noremap = 1,
  421. enabled = 1,
  422. rhs = '<Tab><Esc>',
  423. silent = 0,
  424. },
  425. },
  426. name = 'Test2',
  427. hidden = 0,
  428. },
  429. {
  430. priority = 500,
  431. mappings = {
  432. s = {
  433. sid = 1,
  434. noremap = 1,
  435. enabled = 1,
  436. rhs = 'yA<C-R>0<Tab>xyz<Esc>',
  437. silent = 0,
  438. },
  439. v = {
  440. sid = 1,
  441. noremap = 1,
  442. enabled = 1,
  443. rhs = 'yA<C-R>0<Tab>xyz<Esc>',
  444. silent = 0,
  445. },
  446. },
  447. name = 'Test3',
  448. hidden = 0,
  449. },
  450. {
  451. priority = 500,
  452. mappings = {
  453. i = {
  454. sid = 1,
  455. noremap = 1,
  456. enabled = 1,
  457. rhs = '<C-R>*',
  458. silent = 0,
  459. },
  460. },
  461. name = 'Test4',
  462. hidden = 0,
  463. },
  464. {
  465. priority = 500,
  466. mappings = {
  467. i = {
  468. sid = 1,
  469. noremap = 1,
  470. enabled = 1,
  471. rhs = '<C-R>+',
  472. silent = 0,
  473. },
  474. },
  475. name = 'Test5',
  476. hidden = 0,
  477. },
  478. {
  479. priority = 500,
  480. mappings = {
  481. n = {
  482. sid = 1,
  483. noremap = 1,
  484. enabled = 1,
  485. rhs = '',
  486. silent = 0,
  487. },
  488. },
  489. name = 'Test6',
  490. hidden = 0,
  491. },
  492. {
  493. priority = 500,
  494. mappings = {
  495. n = {
  496. sid = 1,
  497. noremap = 1,
  498. enabled = 1,
  499. rhs = '',
  500. silent = 0,
  501. },
  502. },
  503. name = 'Test7',
  504. hidden = 0,
  505. },
  506. {
  507. priority = 500,
  508. mappings = {
  509. n = {
  510. sid = 1,
  511. noremap = 1,
  512. enabled = 1,
  513. rhs = '',
  514. silent = 0,
  515. },
  516. },
  517. name = 'Test8',
  518. hidden = 0,
  519. },
  520. {
  521. priority = 500,
  522. mappings = {
  523. n = {
  524. sid = 1,
  525. noremap = 1,
  526. enabled = 1,
  527. rhs = '""',
  528. silent = 0,
  529. },
  530. },
  531. name = 'Test9',
  532. hidden = 0,
  533. },
  534. },
  535. priority = 500,
  536. name = 'Test',
  537. },
  538. }
  539. eq(m, expected)
  540. end)
  541. it('works with right-aligned text and spaces', function()
  542. command('nnoremenu &Test<Tab>Y.Test<Tab>X\\ x inormal<Alt-j>')
  543. command('nnoremenu &Test\\ 1.Test\\ 2 Wargl')
  544. command('nnoremenu &Test4.Test<Tab>3 i space<Esc>')
  545. local m = fn.menu_get('')
  546. local expected = {
  547. {
  548. shortcut = 'T',
  549. hidden = 0,
  550. actext = 'Y',
  551. submenus = {
  552. {
  553. mappings = {
  554. n = {
  555. sid = 1,
  556. noremap = 1,
  557. enabled = 1,
  558. rhs = 'inormal<Alt-j>',
  559. silent = 0,
  560. },
  561. },
  562. hidden = 0,
  563. actext = 'X x',
  564. priority = 500,
  565. name = 'Test',
  566. },
  567. },
  568. priority = 500,
  569. name = 'Test',
  570. },
  571. {
  572. shortcut = 'T',
  573. hidden = 0,
  574. submenus = {
  575. {
  576. priority = 500,
  577. mappings = {
  578. n = {
  579. sid = 1,
  580. noremap = 1,
  581. enabled = 1,
  582. rhs = 'Wargl',
  583. silent = 0,
  584. },
  585. },
  586. name = 'Test 2',
  587. hidden = 0,
  588. },
  589. },
  590. priority = 500,
  591. name = 'Test 1',
  592. },
  593. {
  594. shortcut = 'T',
  595. hidden = 0,
  596. submenus = {
  597. {
  598. mappings = {
  599. n = {
  600. sid = 1,
  601. noremap = 1,
  602. enabled = 1,
  603. rhs = 'i space<Esc>',
  604. silent = 0,
  605. },
  606. },
  607. hidden = 0,
  608. actext = '3',
  609. priority = 500,
  610. name = 'Test',
  611. },
  612. },
  613. priority = 500,
  614. name = 'Test4',
  615. },
  616. }
  617. eq(m, expected)
  618. end)
  619. end)