neovim

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

pos_spec.lua (1991B)


      1 -- Test suite for vim.pos
      2 local t = require('test.testutil')
      3 local n = require('test.functional.testnvim')()
      4 local eq = t.eq
      5 
      6 local clear = n.clear
      7 local exec_lua = n.exec_lua
      8 local insert = n.insert
      9 
     10 describe('vim.pos', function()
     11  before_each(clear)
     12 
     13  it('creates a position with or without optional fields', function()
     14    local pos = exec_lua(function()
     15      return vim.pos(3, 5)
     16    end)
     17    eq(3, pos.row)
     18    eq(5, pos.col)
     19    eq(nil, pos.buf)
     20 
     21    local buf = exec_lua(function()
     22      return vim.api.nvim_create_buf(false, true)
     23    end)
     24    pos = exec_lua(function()
     25      return vim.pos(3, 5, { buf = buf })
     26    end)
     27    eq(3, pos.row)
     28    eq(5, pos.col)
     29    eq(buf, pos.buf)
     30  end)
     31 
     32  it('comparisons by overloaded operators', function()
     33    eq(
     34      true,
     35      exec_lua(function()
     36        return vim.pos(3, 5) < vim.pos(4, 5)
     37      end)
     38    )
     39    eq(
     40      true,
     41      exec_lua(function()
     42        return vim.pos(3, 5) <= vim.pos(3, 6)
     43      end)
     44    )
     45    eq(
     46      true,
     47      exec_lua(function()
     48        return vim.pos(3, 5) > vim.pos(2, 5)
     49      end)
     50    )
     51    eq(
     52      true,
     53      exec_lua(function()
     54        return vim.pos(3, 5) >= vim.pos(3, 5)
     55      end)
     56    )
     57    eq(
     58      true,
     59      exec_lua(function()
     60        return vim.pos(3, 5) == vim.pos(3, 5)
     61      end)
     62    )
     63    eq(
     64      true,
     65      exec_lua(function()
     66        return vim.pos(3, 5) ~= vim.pos(3, 6)
     67      end)
     68    )
     69  end)
     70 
     71  it('converts between vim.Pos and lsp.Position', function()
     72    local buf = exec_lua(function()
     73      return vim.api.nvim_get_current_buf()
     74    end)
     75    insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
     76    local lsp_pos = exec_lua(function()
     77      local pos = vim.pos(0, 36, { buf = buf })
     78      return pos:to_lsp('utf-16')
     79    end)
     80    eq({ line = 0, character = 20 }, lsp_pos)
     81    local pos = exec_lua(function()
     82      return vim.pos.lsp(buf, lsp_pos, 'utf-16')
     83    end)
     84    eq({
     85      buf = buf,
     86      row = 0,
     87      col = 36,
     88    }, pos)
     89  end)
     90 end)