robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 716835c2d51fa783d7

master
3.6 KiB152 linesraw
1-- Test suite for vim.pos
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local describe, it, before_each = t.describe, t.it, t.before_each
5local eq = t.eq
6local dedent = t.dedent
7
8local clear = n.clear
9local exec_lua = n.exec_lua
10local insert = n.insert
11
12describe('vim.pos', function()
13  before_each(clear)
14
15  it('creates a position', function()
16    local pos, buf = exec_lua(function()
17      local buf = vim.api.nvim_create_buf(false, true)
18      return vim.pos(buf, 3, 5), buf
19    end)
20    eq(3, pos[1])
21    eq(5, pos[2])
22    eq(buf, pos[3])
23  end)
24
25  it('creates a position with buf=0', function()
26    local pos, buf = exec_lua(function()
27      return vim.pos(0, 3, 5), vim.api.nvim_get_current_buf()
28    end)
29    eq(3, pos[1])
30    eq(5, pos[2])
31    eq(buf, pos[3])
32  end)
33
34  it('creates a position from the window cursor', function()
35    local pos, pos_default, buf = exec_lua(function()
36      vim.api.nvim_buf_set_lines(0, 0, -1, true, { 'first', 'second' })
37      vim.api.nvim_win_set_cursor(0, { 2, 3 })
38      return vim.pos.cursor(0), vim.pos.cursor(), vim.api.nvim_get_current_buf()
39    end)
40    eq({ 1, 3, buf }, pos)
41    eq({ 1, 3, buf }, pos_default)
42  end)
43
44  it('is modifiable', function()
45    local pos, buf = exec_lua(function()
46      local pos = vim.pos(-1, 5, 5)
47      pos.buf = 0
48      pos.row = 4
49      pos.col = 2
50      return pos, vim.api.nvim_get_current_buf()
51    end)
52    eq({ 4, 2, buf }, pos)
53  end)
54
55  it('comparisons by overloaded operators', function()
56    local buf = exec_lua(function()
57      return vim.api.nvim_create_buf(false, true)
58    end)
59    eq(
60      true,
61      exec_lua(function()
62        return vim.pos(buf, 3, 5) < vim.pos(buf, 4, 5)
63      end)
64    )
65    eq(
66      true,
67      exec_lua(function()
68        return vim.pos(buf, 3, 5) <= vim.pos(buf, 3, 6)
69      end)
70    )
71    eq(
72      true,
73      exec_lua(function()
74        return vim.pos(buf, 3, 5) > vim.pos(buf, 2, 5)
75      end)
76    )
77    eq(
78      true,
79      exec_lua(function()
80        return vim.pos(buf, 3, 5) >= vim.pos(buf, 3, 5)
81      end)
82    )
83    eq(
84      true,
85      exec_lua(function()
86        return vim.pos(buf, 3, 5) == vim.pos(buf, 3, 5)
87      end)
88    )
89    eq(
90      true,
91      exec_lua(function()
92        return vim.pos(buf, 3, 5) ~= vim.pos(buf, 3, 6)
93      end)
94    )
95  end)
96
97  it('converts between vim.Pos and lsp.Position', function()
98    local buf = exec_lua(function()
99      return vim.api.nvim_get_current_buf()
100    end)
101    insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
102    local lsp_pos = exec_lua(function()
103      local pos = vim.pos(buf, 0, 36)
104      return pos:to_lsp('utf-16')
105    end)
106    eq({ line = 0, character = 20 }, lsp_pos)
107    local pos = exec_lua(function()
108      return vim.pos.lsp(buf, lsp_pos, 'utf-16')
109    end)
110    eq({
111      0,
112      36,
113      buf,
114    }, pos)
115  end)
116
117  it('converts between vim.Pos and buffer offset', function()
118    local buf = exec_lua(function()
119      return vim.api.nvim_get_current_buf()
120    end)
121    insert(dedent [[
122      first
123      second
124      third
125    ]])
126
127    local offsets = exec_lua(function()
128      return {
129        vim.pos(buf, 0, 0):to_offset(),
130        vim.pos(buf, 0, 3):to_offset(),
131        vim.pos(buf, 1, 0):to_offset(),
132        vim.pos(buf, 3, 0):to_offset(),
133      }
134    end)
135    eq({ 0, 3, 6, 19 }, offsets)
136
137    local positions = exec_lua(function()
138      return {
139        vim.pos.offset(buf, 0),
140        vim.pos.offset(buf, 3),
141        vim.pos.offset(buf, 6),
142        vim.pos.offset(buf, 19),
143      }
144    end)
145    eq({
146      { 0, 0, buf },
147      { 0, 3, buf },
148      { 1, 0, buf },
149      { 3, 0, buf },
150    }, positions)
151  end)
152end)