ui_spec.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local ok = t.ok
  5. local exec_lua = n.exec_lua
  6. local clear = n.clear
  7. local feed = n.feed
  8. local eval = n.eval
  9. local is_ci = t.is_ci
  10. local is_os = t.is_os
  11. local poke_eventloop = n.poke_eventloop
  12. describe('vim.ui', function()
  13. before_each(function()
  14. clear()
  15. end)
  16. describe('select()', function()
  17. it('can select an item', function()
  18. local result = exec_lua [[
  19. local items = {
  20. { name = 'Item 1' },
  21. { name = 'Item 2' },
  22. }
  23. local opts = {
  24. format_item = function(entry)
  25. return entry.name
  26. end
  27. }
  28. local selected
  29. local cb = function(item)
  30. selected = item
  31. end
  32. -- inputlist would require input and block the test;
  33. local choices
  34. vim.fn.inputlist = function(x)
  35. choices = x
  36. return 1
  37. end
  38. vim.ui.select(items, opts, cb)
  39. vim.wait(100, function() return selected ~= nil end)
  40. return {selected, choices}
  41. ]]
  42. eq({ name = 'Item 1' }, result[1])
  43. eq({
  44. 'Select one of:',
  45. '1: Item 1',
  46. '2: Item 2',
  47. }, result[2])
  48. end)
  49. end)
  50. describe('input()', function()
  51. it('can input text', function()
  52. local result = exec_lua [[
  53. local opts = {
  54. prompt = 'Input: ',
  55. }
  56. local input
  57. local cb = function(item)
  58. input = item
  59. end
  60. -- input would require input and block the test;
  61. local prompt
  62. vim.fn.input = function(opts)
  63. prompt = opts.prompt
  64. return "Inputted text"
  65. end
  66. vim.ui.input(opts, cb)
  67. vim.wait(100, function() return input ~= nil end)
  68. return {input, prompt}
  69. ]]
  70. eq('Inputted text', result[1])
  71. eq('Input: ', result[2])
  72. end)
  73. it('can input text on nil opt', function()
  74. feed(':lua vim.ui.input(nil, function(input) result = input end)<cr>')
  75. eq('', eval('v:errmsg'))
  76. feed('Inputted text<cr>')
  77. eq('Inputted text', exec_lua('return result'))
  78. end)
  79. it('can input text on {} opt', function()
  80. feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
  81. eq('', eval('v:errmsg'))
  82. feed('abcdefg<cr>')
  83. eq('abcdefg', exec_lua('return result'))
  84. end)
  85. it('can input empty text #18144', function()
  86. feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
  87. feed('<cr>')
  88. eq('', exec_lua('return result'))
  89. end)
  90. it('can input empty text with cancelreturn opt #18144', function()
  91. feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
  92. feed('<cr>')
  93. eq('', exec_lua('return result'))
  94. end)
  95. it('can return nil when aborted with ESC #18144', function()
  96. feed(':lua result = "on_confirm not called"<cr>')
  97. feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
  98. feed('Inputted Text<esc>')
  99. -- Note: When `result == nil`, exec_lua('returns result') returns vim.NIL
  100. eq(true, exec_lua('return (nil == result)'))
  101. end)
  102. it('can return opts.cacelreturn when aborted with ESC with cancelreturn opt #18144', function()
  103. feed(':lua result = "on_confirm not called"<cr>')
  104. feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
  105. feed('Inputted Text<esc>')
  106. eq('CANCEL', exec_lua('return result'))
  107. end)
  108. it('can return nil when interrupted with Ctrl-C #18144', function()
  109. feed(':lua result = "on_confirm not called"<cr>')
  110. feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
  111. poke_eventloop() -- This is needed because Ctrl-C flushes input
  112. feed('Inputted Text<c-c>')
  113. eq(true, exec_lua('return (nil == result)'))
  114. end)
  115. it(
  116. 'can return the identical object when an arbitrary opts.cancelreturn object is given',
  117. function()
  118. feed(':lua fn = function() return 42 end<CR>')
  119. eq(42, exec_lua('return fn()'))
  120. feed(':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>')
  121. feed('cancel<esc>')
  122. eq(true, exec_lua('return (result == fn)'))
  123. eq(42, exec_lua('return result()'))
  124. end
  125. )
  126. end)
  127. describe('open()', function()
  128. it('validation', function()
  129. if is_os('win') or not is_ci('github') then
  130. exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]]
  131. end
  132. if not is_os('bsd') then
  133. local rv =
  134. exec_lua [[local cmd = vim.ui.open('non-existent-file'); return cmd:wait(100).code]]
  135. ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv)
  136. end
  137. exec_lua [[
  138. vim.fn.has = function() return 0 end
  139. vim.fn.executable = function() return 0 end
  140. ]]
  141. eq(
  142. 'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)',
  143. exec_lua [[local _, err = vim.ui.open('foo') ; return err]]
  144. )
  145. end)
  146. it('opt.cmd #29490', function()
  147. t.matches(
  148. 'ENOENT: no such file or directory',
  149. t.pcall_err(exec_lua, function()
  150. vim.ui.open('foo', { cmd = { 'non-existent-tool' } })
  151. end)
  152. )
  153. eq(
  154. {
  155. code = 0,
  156. signal = 0,
  157. stderr = '',
  158. stdout = 'arg1=arg1;arg2=https://example.com;',
  159. },
  160. exec_lua(function(cmd_)
  161. local cmd, err = vim.ui.open('https://example.com', { cmd = cmd_ })
  162. assert(cmd and not err)
  163. return cmd:wait()
  164. end, { n.testprg('printargs-test'), 'arg1' })
  165. )
  166. end)
  167. end)
  168. end)