robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.5 KiB269 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local eq = t.eq
7local exec_lua = n.exec_lua
8local insert = n.insert
9local assert_alive = n.assert_alive
10
11before_each(clear)
12
13local function lua_eval(lua_expr)
14  return exec_lua('return ' .. lua_expr)
15end
16
17describe('treesitter node API', function()
18  it('double free tree', function()
19    insert('F')
20    exec_lua(function()
21      vim.treesitter.start(0, 'lua')
22      vim.treesitter.get_parser(0):parse()
23      vim.treesitter.get_node():tree()
24      vim.treesitter.get_node():tree()
25      collectgarbage()
26    end)
27    assert_alive()
28  end)
29
30  it('double free tree 2', function()
31    exec_lua(function()
32      local parser = vim.treesitter.get_parser(0, 'c')
33      local x = parser:parse()[1]:root():tree()
34      vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { 'y' })
35      parser:parse()
36      vim.api.nvim_buf_set_text(0, 0, 0, 0, 1, { 'z' })
37      parser:parse()
38      collectgarbage()
39      x:root()
40    end)
41    assert_alive()
42  end)
43
44  it('get_node() with lang given', function()
45    -- this buffer doesn't have filetype set!
46    insert('local foo = function() end')
47    exec_lua(function()
48      vim.treesitter.get_parser(0, 'lua'):parse()
49      _G.node = vim.treesitter.get_node({
50        bufnr = 0,
51        pos = { 0, 6 }, -- on "foo"
52        lang = 'lua',
53      })
54    end)
55    eq('foo', lua_eval('vim.treesitter.get_node_text(node, 0)'))
56    eq('identifier', lua_eval('node:type()'))
57  end)
58
59  it('get_node() with anonymous nodes included', function()
60    insert([[print('test')]])
61
62    exec_lua(function()
63      _G.parser = vim.treesitter.get_parser(0, 'lua')
64      _G.tree = _G.parser:parse()[1]
65      _G.node = vim.treesitter.get_node({
66        bufnr = 0,
67        pos = { 0, 6 }, -- on the first apostrophe
68        include_anonymous = true,
69      })
70    end)
71
72    eq("'", lua_eval('node:type()'))
73    eq(false, lua_eval('node:named()'))
74  end)
75
76  it('can move between siblings', function()
77    insert([[
78      int main(int x, int y, int z) {
79        return x + y * z
80      }
81    ]])
82
83    exec_lua(function()
84      local parser = assert(vim.treesitter.get_parser(0, 'c'))
85      local tree = parser:parse()[1]
86      _G.root = tree:root()
87      vim.treesitter.language.inspect('c')
88
89      function _G.node_text(node)
90        return vim.treesitter.get_node_text(node, 0)
91      end
92    end)
93
94    exec_lua 'node = root:descendant_for_range(0, 9, 0, 14)'
95    eq('int x', lua_eval('node_text(node)'))
96
97    exec_lua 'node = node:next_sibling()'
98    eq(',', lua_eval('node_text(node)'))
99
100    exec_lua 'node = node:next_sibling()'
101    eq('int y', lua_eval('node_text(node)'))
102
103    exec_lua 'node = node:prev_sibling()'
104    eq(',', lua_eval('node_text(node)'))
105
106    exec_lua 'node = node:prev_sibling()'
107    eq('int x', lua_eval('node_text(node)'))
108
109    exec_lua 'node = node:next_named_sibling()'
110    eq('int y', lua_eval('node_text(node)'))
111
112    exec_lua 'node = node:prev_named_sibling()'
113    eq('int x', lua_eval('node_text(node)'))
114  end)
115
116  it('can retrieve the children of a node', function()
117    insert([[
118      int main() {
119        int x = 3;
120      }]])
121
122    local len = exec_lua(function()
123      local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
124      local node = assert(tree:root():child(0))
125      _G.children = node:named_children()
126
127      return #_G.children
128    end)
129
130    eq(3, len)
131    eq('<node compound_statement>', lua_eval('tostring(children[3])'))
132  end)
133
134  it('can retrieve the tree root given a node', function()
135    insert([[
136      int main() {
137        int x = 3;
138      }]])
139
140    exec_lua(function()
141      local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
142      _G.root = tree:root()
143      _G.node = _G.root:child(0):child(2)
144    end)
145
146    eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())'))
147  end)
148
149  it('can compute the byte length of a node', function()
150    insert([[
151      int main() {
152        int x = 3;
153      }]])
154
155    exec_lua(function()
156      local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
157      _G.root = tree:root()
158      _G.child = _G.root:child(0):child(0)
159    end)
160
161    eq(28, lua_eval('root:byte_length()'))
162    eq(3, lua_eval('child:byte_length()'))
163  end)
164
165  it('child_with_descendant() works', function()
166    insert([[
167      int main() {
168        int x = 3;
169      }]])
170
171    exec_lua(function()
172      local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
173      _G.root = assert(tree:root())
174      _G.main = assert(_G.root:child(0))
175      _G.body = assert(_G.main:child(2))
176      _G.statement = assert(_G.body:child(1))
177      _G.declarator = assert(_G.statement:child(1))
178      _G.value = assert(_G.declarator:child(1))
179    end)
180
181    eq(lua_eval('main:type()'), lua_eval('root:child_with_descendant(value):type()'))
182    eq(lua_eval('body:type()'), lua_eval('main:child_with_descendant(value):type()'))
183    eq(lua_eval('statement:type()'), lua_eval('body:child_with_descendant(value):type()'))
184    eq(lua_eval('declarator:type()'), lua_eval('statement:child_with_descendant(value):type()'))
185    eq(lua_eval('value:type()'), lua_eval('declarator:child_with_descendant(value):type()'))
186    eq(vim.NIL, lua_eval('value:child_with_descendant(value)'))
187  end)
188
189  it('gets all children with a given field name', function()
190    insert([[
191      function foo(a,b,c)
192      end
193    ]])
194
195    exec_lua(function()
196      local tree = vim.treesitter.get_parser(0, 'lua'):parse()[1]
197      _G.parameters_node = assert(tree:root():named_descendant_for_range(0, 18, 0, 18))
198      _G.children_by_field = _G.parameters_node:field('name')
199    end)
200
201    eq('parameters', lua_eval('parameters_node:type()'))
202    eq(3, lua_eval('#children_by_field'))
203    eq('a', lua_eval('vim.treesitter.get_node_text(children_by_field[1], 0)'))
204    eq('b', lua_eval('vim.treesitter.get_node_text(children_by_field[2], 0)'))
205    eq('c', lua_eval('vim.treesitter.get_node_text(children_by_field[3], 0)'))
206  end)
207
208  it('node access after tree edit', function()
209    insert([[
210      function x()
211        return true
212      end
213    ]])
214    exec_lua(function()
215      local tree = vim.treesitter.get_parser(0, 'lua'):parse()[1]
216
217      -- ensure treesitter does not try to edit the tree inplace
218      tree:copy()
219
220      local node = tree:root():child(0)
221      vim.api.nvim_buf_set_lines(0, 1, 2, false, {})
222      vim.schedule(function()
223        collectgarbage()
224        local a, b, c, d = node:range()
225        _G.range = { a, b, c, d }
226      end)
227    end)
228    eq({ 0, 0, 2, 3 }, lua_eval('range'))
229  end)
230
231  it(
232    'get_node_text() includes trailing newline for buffer source when node end_col == 0',
233    function()
234      insert('local x = 1')
235
236      exec_lua(function()
237        _G.node = vim.treesitter.get_parser(0, 'lua'):parse()[1]:root()
238      end)
239
240      eq('local x = 1\n', lua_eval('vim.treesitter.get_node_text(_G.node, 0)'))
241    end
242  )
243
244  it(
245    'get_node_text() includes trailing newline for string source when node end_col == 0',
246    function()
247      exec_lua(function()
248        local source = 'local x = 1\n'
249        _G.source = source
250        _G.node = vim.treesitter.get_string_parser(source, 'lua'):parse()[1]:root()
251      end)
252
253      eq('local x = 1\n', lua_eval('vim.treesitter.get_node_text(_G.node, _G.source)'))
254    end
255  )
256
257  it('tree:root() is idempotent', function()
258    insert([[
259      function x()
260        return true
261      end
262    ]])
263    exec_lua(function()
264      local tree = vim.treesitter.get_parser(0, 'lua'):parse()[1]
265      assert(tree:root() == tree:root())
266      assert(tree:root() == tree:root():tree():root())
267    end)
268  end)
269end)