neovim

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

perl_spec.lua (3417B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local eq, clear = t.eq, n.clear
      5 local missing_provider = n.missing_provider
      6 local command = n.command
      7 local write_file = t.write_file
      8 local eval = n.eval
      9 local retry = t.retry
     10 local api = n.api
     11 local insert = n.insert
     12 local expect = n.expect
     13 local feed = n.feed
     14 
     15 do
     16  clear()
     17  local reason = missing_provider('perl')
     18  if reason then
     19    pending(
     20      string.format('Missing perl host, or perl version is too old (%s)', reason),
     21      function() end
     22    )
     23    return
     24  end
     25 end
     26 
     27 before_each(function()
     28  clear()
     29 end)
     30 
     31 describe('legacy perl provider', function()
     32  it('feature test', function()
     33    eq(1, eval('has("perl")'))
     34  end)
     35 
     36  it(':perl command', function()
     37    command('perl $vim->vars->{set_by_perl} = [100, 0];')
     38    eq({ 100, 0 }, eval('g:set_by_perl'))
     39  end)
     40 
     41  it(':perlfile command', function()
     42    local fname = 'perlfile.pl'
     43    write_file(fname, '$vim->command("let set_by_perlfile = 123")')
     44    command('perlfile perlfile.pl')
     45    eq(123, eval('g:set_by_perlfile'))
     46    os.remove(fname)
     47  end)
     48 
     49  it(':perldo command', function()
     50    -- :perldo 1; doesn't change $_,
     51    -- the buffer should not be changed
     52    command('normal :perldo 1;')
     53    eq(false, api.nvim_get_option_value('modified', {}))
     54    -- insert some text
     55    insert('abc\ndef\nghi')
     56    expect([[
     57      abc
     58      def
     59      ghi]])
     60    -- go to top and select and replace the first two lines
     61    feed('ggvj:perldo $_ = reverse ($_)."$linenr"<CR>')
     62    expect([[
     63      cba1
     64      fed2
     65      ghi]])
     66  end)
     67 
     68  it('perleval()', function()
     69    eq({ 1, 2, { ['key'] = 'val' } }, eval([[perleval('[1, 2, {"key" => "val"}]')]]))
     70  end)
     71 end)
     72 
     73 describe('perl provider', function()
     74  teardown(function()
     75    os.remove('Xtest-perl-hello.pl')
     76    os.remove('Xtest-perl-hello-plugin.pl')
     77  end)
     78 
     79  it('works', function()
     80    local fname = 'Xtest-perl-hello.pl'
     81    write_file(
     82      fname,
     83      [[
     84      package main;
     85      use strict;
     86      use warnings;
     87      use Neovim::Ext;
     88      use Neovim::Ext::MsgPack::RPC;
     89 
     90      my $session = Neovim::Ext::MsgPack::RPC::socket_session($ENV{NVIM});
     91      my $nvim = Neovim::Ext::from_session($session);
     92      $nvim->command('let g:job_out = "hello"');
     93      1;
     94    ]]
     95    )
     96    command('let g:job_id = jobstart(["perl", "' .. fname .. '"])')
     97    retry(nil, 3000, function()
     98      eq('hello', eval('g:job_out'))
     99    end)
    100  end)
    101 
    102  it('plugin works', function()
    103    local fname = 'Xtest-perl-hello-plugin.pl'
    104    write_file(
    105      fname,
    106      [[
    107      package TestPlugin;
    108      use strict;
    109      use warnings;
    110      use parent qw(Neovim::Ext::Plugin);
    111 
    112      __PACKAGE__->register;
    113 
    114      @{TestPlugin::commands} = ();
    115      @{TestPlugin::specs} = ();
    116      sub test_command :nvim_command('TestCommand')
    117      {
    118        my ($this) = @_;
    119        $this->nvim->command('let g:job_out = "hello-plugin"');
    120      }
    121 
    122      package main;
    123      use strict;
    124      use warnings;
    125      use Neovim::Ext;
    126      use Neovim::Ext::MsgPack::RPC;
    127 
    128      my $session = Neovim::Ext::MsgPack::RPC::socket_session($ENV{NVIM});
    129      my $nvim = Neovim::Ext::from_session($session);
    130      my $plugin = TestPlugin->new($nvim);
    131      $plugin->test_command();
    132      1;
    133    ]]
    134    )
    135    command('let g:job_id = jobstart(["perl", "' .. fname .. '"])')
    136    retry(nil, 3000, function()
    137      eq('hello-plugin', eval('g:job_out'))
    138    end)
    139  end)
    140 end)