neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

ruby_spec.lua (3713B)


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