node_spec.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. local exec_lua = n.exec_lua
  6. local insert = n.insert
  7. local assert_alive = n.assert_alive
  8. before_each(clear)
  9. local function lua_eval(lua_expr)
  10. return exec_lua('return ' .. lua_expr)
  11. end
  12. describe('treesitter node API', function()
  13. clear()
  14. it('double free tree', function()
  15. insert('F')
  16. exec_lua(function()
  17. vim.treesitter.start(0, 'lua')
  18. vim.treesitter.get_node():tree()
  19. vim.treesitter.get_node():tree()
  20. collectgarbage()
  21. end)
  22. assert_alive()
  23. end)
  24. it('double free tree 2', function()
  25. exec_lua(function()
  26. local parser = vim.treesitter.get_parser(0, 'c')
  27. local x = parser:parse()[1]:root():tree()
  28. vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { 'y' })
  29. parser:parse()
  30. vim.api.nvim_buf_set_text(0, 0, 0, 0, 1, { 'z' })
  31. parser:parse()
  32. collectgarbage()
  33. x:root()
  34. end)
  35. assert_alive()
  36. end)
  37. it('get_node() with lang given', function()
  38. -- this buffer doesn't have filetype set!
  39. insert('local foo = function() end')
  40. exec_lua(function()
  41. _G.node = vim.treesitter.get_node({
  42. bufnr = 0,
  43. pos = { 0, 6 }, -- on "foo"
  44. lang = 'lua',
  45. })
  46. end)
  47. eq('foo', lua_eval('vim.treesitter.get_node_text(node, 0)'))
  48. eq('identifier', lua_eval('node:type()'))
  49. end)
  50. it('get_node() with anonymous nodes included', function()
  51. insert([[print('test')]])
  52. exec_lua(function()
  53. _G.parser = vim.treesitter.get_parser(0, 'lua')
  54. _G.tree = _G.parser:parse()[1]
  55. _G.node = vim.treesitter.get_node({
  56. bufnr = 0,
  57. pos = { 0, 6 }, -- on the first apostrophe
  58. include_anonymous = true,
  59. })
  60. end)
  61. eq("'", lua_eval('node:type()'))
  62. eq(false, lua_eval('node:named()'))
  63. end)
  64. it('can move between siblings', function()
  65. insert([[
  66. int main(int x, int y, int z) {
  67. return x + y * z
  68. }
  69. ]])
  70. exec_lua(function()
  71. local parser = vim.treesitter.get_parser(0, 'c')
  72. local tree = parser:parse()[1]
  73. _G.root = tree:root()
  74. vim.treesitter.language.inspect('c')
  75. function _G.node_text(node)
  76. return vim.treesitter.get_node_text(node, 0)
  77. end
  78. end)
  79. exec_lua 'node = root:descendant_for_range(0, 11, 0, 16)'
  80. eq('int x', lua_eval('node_text(node)'))
  81. exec_lua 'node = node:next_sibling()'
  82. eq(',', lua_eval('node_text(node)'))
  83. exec_lua 'node = node:next_sibling()'
  84. eq('int y', lua_eval('node_text(node)'))
  85. exec_lua 'node = node:prev_sibling()'
  86. eq(',', lua_eval('node_text(node)'))
  87. exec_lua 'node = node:prev_sibling()'
  88. eq('int x', lua_eval('node_text(node)'))
  89. exec_lua 'node = node:next_named_sibling()'
  90. eq('int y', lua_eval('node_text(node)'))
  91. exec_lua 'node = node:prev_named_sibling()'
  92. eq('int x', lua_eval('node_text(node)'))
  93. end)
  94. it('can retrieve the children of a node', function()
  95. insert([[
  96. int main() {
  97. int x = 3;
  98. }]])
  99. local len = exec_lua(function()
  100. local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
  101. local node = assert(tree:root():child(0))
  102. _G.children = node:named_children()
  103. return #_G.children
  104. end)
  105. eq(3, len)
  106. eq('<node compound_statement>', lua_eval('tostring(children[3])'))
  107. end)
  108. it('can retrieve the tree root given a node', function()
  109. insert([[
  110. int main() {
  111. int x = 3;
  112. }]])
  113. exec_lua(function()
  114. local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
  115. _G.root = tree:root()
  116. _G.node = _G.root:child(0):child(2)
  117. end)
  118. eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())'))
  119. end)
  120. it('can compute the byte length of a node', function()
  121. insert([[
  122. int main() {
  123. int x = 3;
  124. }]])
  125. exec_lua(function()
  126. local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
  127. _G.root = tree:root()
  128. _G.child = _G.root:child(0):child(0)
  129. end)
  130. eq(28, lua_eval('root:byte_length()'))
  131. eq(3, lua_eval('child:byte_length()'))
  132. end)
  133. it('child_containing_descendant() works', function()
  134. insert([[
  135. int main() {
  136. int x = 3;
  137. }]])
  138. exec_lua(function()
  139. local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
  140. _G.root = tree:root()
  141. _G.main = _G.root:child(0)
  142. _G.body = _G.main:child(2)
  143. _G.statement = _G.body:child(1)
  144. _G.declarator = _G.statement:child(1)
  145. _G.value = _G.declarator:child(1)
  146. end)
  147. eq(lua_eval('main:type()'), lua_eval('root:child_containing_descendant(value):type()'))
  148. eq(lua_eval('body:type()'), lua_eval('main:child_containing_descendant(value):type()'))
  149. eq(lua_eval('statement:type()'), lua_eval('body:child_containing_descendant(value):type()'))
  150. eq(
  151. lua_eval('declarator:type()'),
  152. lua_eval('statement:child_containing_descendant(value):type()')
  153. )
  154. eq(vim.NIL, lua_eval('declarator:child_containing_descendant(value)'))
  155. end)
  156. it('child_with_descendant() works', function()
  157. insert([[
  158. int main() {
  159. int x = 3;
  160. }]])
  161. exec_lua(function()
  162. local tree = vim.treesitter.get_parser(0, 'c'):parse()[1]
  163. _G.root = assert(tree:root())
  164. _G.main = assert(_G.root:child(0))
  165. _G.body = assert(_G.main:child(2))
  166. _G.statement = assert(_G.body:child(1))
  167. _G.declarator = assert(_G.statement:child(1))
  168. _G.value = assert(_G.declarator:child(1))
  169. end)
  170. eq(lua_eval('main:type()'), lua_eval('root:child_with_descendant(value):type()'))
  171. eq(lua_eval('body:type()'), lua_eval('main:child_with_descendant(value):type()'))
  172. eq(lua_eval('statement:type()'), lua_eval('body:child_with_descendant(value):type()'))
  173. eq(lua_eval('declarator:type()'), lua_eval('statement:child_with_descendant(value):type()'))
  174. eq(lua_eval('value:type()'), lua_eval('declarator:child_with_descendant(value):type()'))
  175. eq(vim.NIL, lua_eval('value:child_with_descendant(value)'))
  176. end)
  177. end)