ruby_spec.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear = helpers.clear
  3. local command = helpers.command
  4. local curbufmeths = helpers.curbufmeths
  5. local eq = helpers.eq
  6. local eval = helpers.eval
  7. local exc_exec = helpers.exc_exec
  8. local expect = helpers.expect
  9. local feed = helpers.feed
  10. local feed_command = helpers.feed_command
  11. local funcs = helpers.funcs
  12. local insert = helpers.insert
  13. local meths = helpers.meths
  14. local missing_provider = helpers.missing_provider
  15. local matches = helpers.matches
  16. local write_file = helpers.write_file
  17. local pcall_err = helpers.pcall_err
  18. do
  19. clear()
  20. local reason = missing_provider('ruby')
  21. if reason then
  22. it(':ruby reports E319 if provider is missing', function()
  23. local expected = [[Vim%(ruby.*%):E319: No "ruby" provider found.*]]
  24. matches(expected, pcall_err(command, 'ruby puts "foo"'))
  25. matches(expected, pcall_err(command, 'rubyfile foo'))
  26. end)
  27. pending(string.format('Missing neovim RubyGem (%s)', reason), function() end)
  28. return
  29. end
  30. end
  31. before_each(function()
  32. clear()
  33. end)
  34. describe('ruby feature test', function()
  35. it('works', function()
  36. eq(1, funcs.has('ruby'))
  37. end)
  38. end)
  39. describe(':ruby command', function()
  40. it('evaluates ruby', function()
  41. command('ruby VIM.command("let g:set_by_ruby = [100, 0]")')
  42. eq({100, 0}, meths.get_var('set_by_ruby'))
  43. end)
  44. it('supports nesting', function()
  45. command([[ruby VIM.command('ruby VIM.command("let set_by_nested_ruby = 555")')]])
  46. eq(555, meths.get_var('set_by_nested_ruby'))
  47. end)
  48. end)
  49. describe(':rubyfile command', function()
  50. it('evaluates a ruby file', function()
  51. local fname = 'rubyfile.rb'
  52. write_file(fname, 'VIM.command("let set_by_rubyfile = 123")')
  53. command('rubyfile rubyfile.rb')
  54. eq(123, meths.get_var('set_by_rubyfile'))
  55. os.remove(fname)
  56. end)
  57. end)
  58. describe(':rubydo command', function()
  59. it('exposes the $_ variable for modifying lines', function()
  60. insert('abc\ndef\nghi\njkl')
  61. expect([[
  62. abc
  63. def
  64. ghi
  65. jkl]])
  66. feed('ggjvj:rubydo $_.upcase!<CR>')
  67. expect([[
  68. abc
  69. DEF
  70. GHI
  71. jkl]])
  72. end)
  73. it('operates on all lines when not given a range', function()
  74. insert('abc\ndef\nghi\njkl')
  75. expect([[
  76. abc
  77. def
  78. ghi
  79. jkl]])
  80. feed(':rubydo $_.upcase!<CR>')
  81. expect([[
  82. ABC
  83. DEF
  84. GHI
  85. JKL]])
  86. end)
  87. it('does not modify the buffer if no changes are made', function()
  88. command('normal :rubydo 42')
  89. eq(false, curbufmeths.get_option('modified'))
  90. end)
  91. end)
  92. describe('ruby provider', function()
  93. it('RPC call to expand("<afile>") during BufDelete #5245 #5617', function()
  94. helpers.add_builddir_to_rtp()
  95. command([=[autocmd BufDelete * ruby VIM::evaluate('expand("<afile>")')]=])
  96. feed_command('help help')
  97. eq(2, eval('1+1')) -- Still alive?
  98. end)
  99. end)
  100. describe('rubyeval()', function()
  101. it('evaluates ruby objects', function()
  102. eq({1, 2, {['key'] = 'val'}}, funcs.rubyeval('[1, 2, {key: "val"}]'))
  103. end)
  104. it('returns nil for empty strings', function()
  105. eq(helpers.NIL, funcs.rubyeval(''))
  106. end)
  107. it('errors out when given non-string', function()
  108. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(10)'))
  109. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(v:_null_dict)'))
  110. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(v:_null_list)'))
  111. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(0.0)'))
  112. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(function("tr"))'))
  113. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(v:true)'))
  114. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(v:false)'))
  115. eq('Vim(call):E474: Invalid argument', exc_exec('call rubyeval(v:null)'))
  116. end)
  117. end)