test_ruby.vim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. " Tests for ruby interface
  2. source check.vim
  3. CheckFeature ruby
  4. func Test_ruby_change_buffer()
  5. call setline(line('$'), ['1 line 1'])
  6. ruby Vim.command("normal /^1\n")
  7. ruby $curbuf.line = "1 changed line 1"
  8. call assert_equal('1 changed line 1', getline('$'))
  9. endfunc
  10. func Test_rubydo()
  11. throw 'skipped: TODO: '
  12. " Check deleting lines does not trigger ml_get error.
  13. new
  14. call setline(1, ['one', 'two', 'three'])
  15. rubydo Vim.command("%d_")
  16. bwipe!
  17. " Check switching to another buffer does not trigger ml_get error.
  18. new
  19. let wincount = winnr('$')
  20. call setline(1, ['one', 'two', 'three'])
  21. rubydo Vim.command("new")
  22. call assert_equal(wincount + 1, winnr('$'))
  23. %bwipe!
  24. endfunc
  25. func Test_rubydo_dollar_underscore()
  26. throw 'skipped: TODO: '
  27. new
  28. call setline(1, ['one', 'two', 'three', 'four'])
  29. 2,3rubydo $_ = '[' + $_ + ']'
  30. call assert_equal(['one', '[two]', '[three]', 'four'], getline(1, '$'))
  31. bwipe!
  32. call assert_fails('rubydo $_ = 0', 'E265:')
  33. call assert_fails('rubydo (')
  34. bwipe!
  35. endfunc
  36. func Test_rubyfile()
  37. " Check :rubyfile does not SEGV with Ruby level exception but just fails
  38. let tempfile = tempname() . '.rb'
  39. call writefile(['raise "vim!"'], tempfile)
  40. call assert_fails('rubyfile ' . tempfile)
  41. call delete(tempfile)
  42. endfunc
  43. func Test_ruby_set_cursor()
  44. " Check that setting the cursor position works.
  45. new
  46. call setline(1, ['first line', 'second line'])
  47. normal gg
  48. rubydo $curwin.cursor = [1, 5]
  49. call assert_equal([1, 6], [line('.'), col('.')])
  50. call assert_equal([1, 5], rubyeval('$curwin.cursor'))
  51. " Check that movement after setting cursor position keeps current column.
  52. normal j
  53. call assert_equal([2, 6], [line('.'), col('.')])
  54. call assert_equal([2, 5], '$curwin.cursor'->rubyeval())
  55. " call assert_fails('ruby $curwin.cursor = [1]',
  56. " \ 'ArgumentError: array length must be 2')
  57. bwipe!
  58. endfunc
  59. " Test buffer.count and buffer.length (number of lines in buffer)
  60. func Test_ruby_buffer_count()
  61. new
  62. call setline(1, ['one', 'two', 'three'])
  63. call assert_equal(3, rubyeval('$curbuf.count'))
  64. call assert_equal(3, rubyeval('$curbuf.length'))
  65. bwipe!
  66. endfunc
  67. " Test buffer.name (buffer name)
  68. func Test_ruby_buffer_name()
  69. new Xfoo
  70. call assert_equal(expand('%:p'), rubyeval('$curbuf.name'))
  71. bwipe
  72. call assert_equal('', rubyeval('$curbuf.name'))
  73. endfunc
  74. " Test buffer.number (number of the buffer).
  75. func Test_ruby_buffer_number()
  76. new
  77. call assert_equal(bufnr('%'), rubyeval('$curbuf.number'))
  78. new
  79. call assert_equal(bufnr('%'), rubyeval('$curbuf.number'))
  80. %bwipe
  81. endfunc
  82. " Test buffer.delete({n}) (delete line {n})
  83. func Test_ruby_buffer_delete()
  84. new
  85. call setline(1, ['one', 'two', 'three'])
  86. ruby $curbuf.delete(2)
  87. call assert_equal(['one', 'three'], getline(1, '$'))
  88. " call assert_fails('ruby $curbuf.delete(0)', 'IndexError: line number 0 out of range')
  89. " call assert_fails('ruby $curbuf.delete(3)', 'IndexError: line number 3 out of range')
  90. call assert_fails('ruby $curbuf.delete(3)', 'RuntimeError: Index out of bounds')
  91. bwipe!
  92. endfunc
  93. " Test buffer.append({str}, str) (append line {str} after line {n})
  94. func Test_ruby_buffer_append()
  95. new
  96. ruby $curbuf.append(0, 'one')
  97. ruby $curbuf.append(1, 'three')
  98. ruby $curbuf.append(1, 'two')
  99. ruby $curbuf.append(4, 'four')
  100. call assert_equal(['one', 'two', 'three', '', 'four'], getline(1, '$'))
  101. " call assert_fails('ruby $curbuf.append(-1, "x")',
  102. " \ 'IndexError: line number -1 out of range')
  103. call assert_fails('ruby $curbuf.append(-1, "x")',
  104. \ 'ArgumentError: Index out of bounds')
  105. call assert_fails('ruby $curbuf.append(6, "x")',
  106. \ 'RuntimeError: Index out of bounds')
  107. bwipe!
  108. endfunc
  109. " Test buffer.line (get or set the current line)
  110. func Test_ruby_buffer_line()
  111. new
  112. call setline(1, ['one', 'two', 'three'])
  113. 2
  114. call assert_equal('two', rubyeval('$curbuf.line'))
  115. ruby $curbuf.line = 'TWO'
  116. call assert_equal(['one', 'TWO', 'three'], getline(1, '$'))
  117. bwipe!
  118. endfunc
  119. " Test buffer.line_number (get current line number)
  120. func Test_ruby_buffer_line_number()
  121. new
  122. call setline(1, ['one', 'two', 'three'])
  123. 2
  124. call assert_equal(2, rubyeval('$curbuf.line_number'))
  125. bwipe!
  126. endfunc
  127. func Test_ruby_buffer_get()
  128. new
  129. call setline(1, ['one', 'two'])
  130. call assert_equal('one', rubyeval('$curbuf[1]'))
  131. call assert_equal('two', rubyeval('$curbuf[2]'))
  132. " call assert_fails('ruby $curbuf[0]',
  133. " \ 'IndexError: line number 0 out of range')
  134. call assert_fails('ruby $curbuf[3]',
  135. \ 'RuntimeError: Index out of bounds')
  136. bwipe!
  137. endfunc
  138. func Test_ruby_buffer_set()
  139. new
  140. call setline(1, ['one', 'two'])
  141. ruby $curbuf[2] = 'TWO'
  142. ruby $curbuf[1] = 'ONE'
  143. " call assert_fails('ruby $curbuf[0] = "ZERO"',
  144. " \ 'IndexError: line number 0 out of range')
  145. " call assert_fails('ruby $curbuf[3] = "THREE"',
  146. " \ 'IndexError: line number 3 out of range')
  147. call assert_fails('ruby $curbuf[3] = "THREE"',
  148. \ 'RuntimeError: Index out of bounds')
  149. bwipe!
  150. endfunc
  151. " Test window.width (get or set window height).
  152. func Test_ruby_window_height()
  153. new
  154. " Test setting window height
  155. ruby $curwin.height = 2
  156. call assert_equal(2, winheight(0))
  157. " Test getting window height
  158. call assert_equal(2, rubyeval('$curwin.height'))
  159. bwipe
  160. endfunc
  161. " Test window.width (get or set window width).
  162. func Test_ruby_window_width()
  163. vnew
  164. " Test setting window width
  165. ruby $curwin.width = 2
  166. call assert_equal(2, winwidth(0))
  167. " Test getting window width
  168. call assert_equal(2, rubyeval('$curwin.width'))
  169. bwipe
  170. endfunc
  171. " Test window.buffer (get buffer object of a window object).
  172. func Test_ruby_window_buffer()
  173. new Xfoo1
  174. new Xfoo2
  175. ruby $b2 = $curwin.buffer
  176. ruby $w2 = $curwin
  177. wincmd j
  178. ruby $b1 = $curwin.buffer
  179. ruby $w1 = $curwin
  180. " call assert_equal(rubyeval('$b1'), rubyeval('$w1.buffer'))
  181. " call assert_equal(rubyeval('$b2'), rubyeval('$w2.buffer'))
  182. call assert_equal(bufnr('Xfoo1'), rubyeval('$w1.buffer.number'))
  183. call assert_equal(bufnr('Xfoo2'), rubyeval('$w2.buffer.number'))
  184. ruby $b1, $w1, $b2, $w2 = nil
  185. %bwipe
  186. endfunc
  187. " Test Vim::Window.current (get current window object)
  188. func Test_ruby_Vim_window_current()
  189. let cw = rubyeval('$curwin.to_s')
  190. " call assert_equal(cw, rubyeval('Vim::Window.current'))
  191. call assert_match('^#<Neovim::Window:0x\x\+>$', cw)
  192. endfunc
  193. " Test Vim::Window.count (number of windows)
  194. func Test_ruby_Vim_window_count()
  195. new Xfoo1
  196. new Xfoo2
  197. split
  198. call assert_equal(4, rubyeval('Vim::Window.count'))
  199. %bwipe
  200. call assert_equal(1, rubyeval('Vim::Window.count'))
  201. endfunc
  202. " Test Vim::Window[n] (get window object of window n)
  203. func Test_ruby_Vim_window_get()
  204. new Xfoo1
  205. new Xfoo2
  206. call assert_match('Xfoo2$', rubyeval('Vim::Window[0].buffer.name'))
  207. wincmd j
  208. call assert_match('Xfoo1$', rubyeval('Vim::Window[1].buffer.name'))
  209. wincmd j
  210. call assert_equal('', rubyeval('Vim::Window[2].buffer.name'))
  211. %bwipe
  212. endfunc
  213. " Test Vim::Buffer.current (return the buffer object of current buffer)
  214. func Test_ruby_Vim_buffer_current()
  215. let cb = rubyeval('$curbuf.to_s')
  216. " call assert_equal(cb, rubyeval('Vim::Buffer.current'))
  217. call assert_match('^#<Neovim::Buffer:0x\x\+>$', cb)
  218. endfunc
  219. " Test Vim::Buffer:.count (return the number of buffers)
  220. func Test_ruby_Vim_buffer_count()
  221. new Xfoo1
  222. new Xfoo2
  223. call assert_equal(3, rubyeval('Vim::Buffer.count'))
  224. %bwipe
  225. call assert_equal(1, rubyeval('Vim::Buffer.count'))
  226. endfunc
  227. " Test Vim::buffer[n] (return the buffer object of buffer number n)
  228. func Test_ruby_Vim_buffer_get()
  229. new Xfoo1
  230. new Xfoo2
  231. " Index of Vim::Buffer[n] goes from 0 to the number of buffers.
  232. call assert_equal('', rubyeval('Vim::Buffer[0].name'))
  233. call assert_match('Xfoo1$', rubyeval('Vim::Buffer[1].name'))
  234. call assert_match('Xfoo2$', rubyeval('Vim::Buffer[2].name'))
  235. call assert_fails('ruby print Vim::Buffer[3].name',
  236. \ "NoMethodError: undefined method `name' for nil")
  237. %bwipe
  238. endfunc
  239. " Test Vim::command({cmd}) (execute a Ex command))
  240. " Test Vim::command({cmd})
  241. func Test_ruby_Vim_command()
  242. new
  243. call setline(1, ['one', 'two', 'three', 'four'])
  244. ruby Vim::command('2,3d')
  245. call assert_equal(['one', 'four'], getline(1, '$'))
  246. bwipe!
  247. endfunc
  248. " Test Vim::set_option (set a vim option)
  249. func Test_ruby_Vim_set_option()
  250. call assert_equal(0, &number)
  251. ruby Vim::set_option('number')
  252. call assert_equal(1, &number)
  253. ruby Vim::set_option('nonumber')
  254. call assert_equal(0, &number)
  255. endfunc
  256. func Test_ruby_Vim_evaluate()
  257. call assert_equal(123, rubyeval('Vim::evaluate("123")'))
  258. " Vim::evaluate("123").class gives Integer or Fixnum depending
  259. " on versions of Ruby.
  260. call assert_match('^Integer\|Fixnum$', rubyeval('Vim::evaluate("123").class'))
  261. if has('float')
  262. call assert_equal(1.23, rubyeval('Vim::evaluate("1.23")'))
  263. call assert_equal('Float', rubyeval('Vim::evaluate("1.23").class'))
  264. endif
  265. call assert_equal('foo', rubyeval('Vim::evaluate("\"foo\"")'))
  266. call assert_equal('String', rubyeval('Vim::evaluate("\"foo\"").class'))
  267. call assert_equal([1, 2], rubyeval('Vim::evaluate("[1, 2]")'))
  268. call assert_equal('Array', rubyeval('Vim::evaluate("[1, 2]").class'))
  269. call assert_equal({'1': 2}, rubyeval('Vim::evaluate("{1:2}")'))
  270. call assert_equal('Hash', rubyeval('Vim::evaluate("{1:2}").class'))
  271. call assert_equal(v:null, rubyeval('Vim::evaluate("v:null")'))
  272. call assert_equal('NilClass', rubyeval('Vim::evaluate("v:null").class'))
  273. " call assert_equal(v:null, rubyeval('Vim::evaluate("v:none")'))
  274. " call assert_equal('NilClass', rubyeval('Vim::evaluate("v:none").class'))
  275. call assert_equal(v:true, rubyeval('Vim::evaluate("v:true")'))
  276. call assert_equal('TrueClass', rubyeval('Vim::evaluate("v:true").class'))
  277. call assert_equal(v:false, rubyeval('Vim::evaluate("v:false")'))
  278. call assert_equal('FalseClass',rubyeval('Vim::evaluate("v:false").class'))
  279. endfunc
  280. func Test_ruby_Vim_blob()
  281. throw 'skipped: TODO: '
  282. call assert_equal('0z', rubyeval('Vim::blob("")'))
  283. call assert_equal('0z31326162', rubyeval('Vim::blob("12ab")'))
  284. call assert_equal('0z00010203', rubyeval('Vim::blob("\x00\x01\x02\x03")'))
  285. call assert_equal('0z8081FEFF', rubyeval('Vim::blob("\x80\x81\xfe\xff")'))
  286. endfunc
  287. func Test_ruby_Vim_evaluate_list()
  288. call setline(line('$'), ['2 line 2'])
  289. ruby Vim.command("normal /^2\n")
  290. let l = ["abc", "def"]
  291. ruby << trim EOF
  292. curline = $curbuf.line_number
  293. l = Vim.evaluate("l");
  294. $curbuf.append(curline, l.join("|"))
  295. EOF
  296. normal j
  297. .rubydo $_ = $_.gsub(/\|/, '/')
  298. call assert_equal('abc/def', getline('$'))
  299. endfunc
  300. func Test_ruby_Vim_evaluate_dict()
  301. let d = {'a': 'foo', 'b': 123}
  302. redir => l:out
  303. ruby d = Vim.evaluate("d"); print d
  304. redir END
  305. call assert_equal(['{"a"=>"foo", "b"=>123}'], split(l:out, "\n"))
  306. endfunc
  307. " Test Vim::message({msg}) (display message {msg})
  308. func Test_ruby_Vim_message()
  309. throw 'skipped: TODO: '
  310. ruby Vim::message('A message')
  311. let messages = split(execute('message'), "\n")
  312. call assert_equal('A message', messages[-1])
  313. endfunc
  314. func Test_ruby_print()
  315. func RubyPrint(expr)
  316. return trim(execute('ruby print ' . a:expr))
  317. endfunc
  318. call assert_equal('123', RubyPrint('123'))
  319. call assert_equal('1.23', RubyPrint('1.23'))
  320. call assert_equal('Hello World!', RubyPrint('"Hello World!"'))
  321. call assert_equal('[1, 2]', RubyPrint('[1, 2]'))
  322. call assert_equal('{"k1"=>"v1", "k2"=>"v2"}', RubyPrint('({"k1" => "v1", "k2" => "v2"})'))
  323. call assert_equal('true', RubyPrint('true'))
  324. call assert_equal('false', RubyPrint('false'))
  325. call assert_equal('', RubyPrint('nil'))
  326. call assert_match('Vim', RubyPrint('Vim'))
  327. call assert_match('Module', RubyPrint('Vim.class'))
  328. delfunc RubyPrint
  329. endfunc
  330. func Test_ruby_p()
  331. ruby p 'Just a test'
  332. let messages = GetMessages()
  333. call assert_equal('"Just a test"', messages[-1])
  334. " Check return values of p method
  335. call assert_equal(123, rubyeval('p(123)'))
  336. call assert_equal([1, 2, 3], rubyeval('p(1, 2, 3)'))
  337. " Avoid the "message maintainer" line.
  338. let $LANG = ''
  339. messages clear
  340. call assert_equal(v:true, rubyeval('p() == nil'))
  341. let messages = GetMessages()
  342. call assert_equal(0, len(messages))
  343. endfunc
  344. func Test_rubyeval_error()
  345. " On Linux or Windows the error matches:
  346. " "syntax error, unexpected end-of-input"
  347. " whereas on macOS in CI, the error message makes less sense:
  348. " "SyntaxError: array length must be 2"
  349. " Unclear why. The test does not check the error message.
  350. call assert_fails('call rubyeval("(")')
  351. endfunc
  352. " Test for various heredoc syntax
  353. func Test_ruby_heredoc()
  354. ruby << END
  355. Vim.command('let s = "A"')
  356. END
  357. ruby <<
  358. Vim.command('let s ..= "B"')
  359. .
  360. ruby << trim END
  361. Vim.command('let s ..= "C"')
  362. END
  363. ruby << trim
  364. Vim.command('let s ..= "D"')
  365. .
  366. ruby << trim eof
  367. Vim.command('let s ..= "E"')
  368. eof
  369. call assert_equal('ABCDE', s)
  370. endfunc
  371. " vim: shiftwidth=2 sts=2 expandtab