diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/treesitter/node_spec.lua | 26 | ||||
-rw-r--r-- | test/functional/treesitter/query_spec.lua | 64 |
2 files changed, 75 insertions, 15 deletions
diff --git a/test/functional/treesitter/node_spec.lua b/test/functional/treesitter/node_spec.lua index 8adec82774..96579f296b 100644 --- a/test/functional/treesitter/node_spec.lua +++ b/test/functional/treesitter/node_spec.lua @@ -143,4 +143,30 @@ describe('treesitter node API', function() eq(28, lua_eval('root:byte_length()')) eq(3, lua_eval('child:byte_length()')) end) + + it('child_containing_descendant() works', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + tree = vim.treesitter.get_parser(0, "c"):parse()[1] + root = tree:root() + main = root:child(0) + body = main:child(2) + statement = body:child(1) + declarator = statement:child(1) + value = declarator:child(1) + ]]) + + eq(lua_eval('main:type()'), lua_eval('root:child_containing_descendant(value):type()')) + eq(lua_eval('body:type()'), lua_eval('main:child_containing_descendant(value):type()')) + eq(lua_eval('statement:type()'), lua_eval('body:child_containing_descendant(value):type()')) + eq( + lua_eval('declarator:type()'), + lua_eval('statement:child_containing_descendant(value):type()') + ) + eq(vim.NIL, lua_eval('declarator:child_containing_descendant(value)')) + end) end) diff --git a/test/functional/treesitter/query_spec.lua b/test/functional/treesitter/query_spec.lua index 96665ee2e7..c3a376cd71 100644 --- a/test/functional/treesitter/query_spec.lua +++ b/test/functional/treesitter/query_spec.lua @@ -10,6 +10,22 @@ local pcall_err = t.pcall_err local api = n.api local fn = n.fn +local get_query_result_code = [[ + function get_query_result(query_text) + cquery = vim.treesitter.query.parse("c", query_text) + parser = vim.treesitter.get_parser(0, "c") + tree = parser:parse()[1] + res = {} + for cid, node in cquery:iter_captures(tree:root(), 0) do + -- can't transmit node over RPC. just check the name, range, and text + local text = vim.treesitter.get_node_text(node, 0) + local range = {node:range()} + table.insert(res, { cquery.captures[cid], node:type(), range, text }) + end + return res + end +]] + describe('treesitter query API', function() before_each(function() clear() @@ -291,21 +307,7 @@ void ui_refresh(void) return 0; } ]]) - exec_lua([[ - function get_query_result(query_text) - cquery = vim.treesitter.query.parse("c", query_text) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for cid, node in cquery:iter_captures(tree:root(), 0) do - -- can't transmit node over RPC. just check the name, range, and text - local text = vim.treesitter.get_node_text(node, 0) - local range = {node:range()} - table.insert(res, { cquery.captures[cid], node:type(), range, text }) - end - return res - end - ]]) + exec_lua(get_query_result_code) local res0 = exec_lua( [[return get_query_result(...)]], @@ -333,6 +335,38 @@ void ui_refresh(void) }, res1) end) + it('supports builtin predicate has-ancestor?', function() + insert([[ + int x = 123; + enum C { y = 124 }; + int main() { int z = 125; }]]) + exec_lua(get_query_result_code) + + local result = exec_lua( + [[return get_query_result(...)]], + [[((number_literal) @literal (#has-ancestor? @literal "function_definition"))]] + ) + eq({ { 'literal', 'number_literal', { 2, 21, 2, 24 }, '125' } }, result) + + result = exec_lua( + [[return get_query_result(...)]], + [[((number_literal) @literal (#has-ancestor? @literal "function_definition" "enum_specifier"))]] + ) + eq({ + { 'literal', 'number_literal', { 1, 13, 1, 16 }, '124' }, + { 'literal', 'number_literal', { 2, 21, 2, 24 }, '125' }, + }, result) + + result = exec_lua( + [[return get_query_result(...)]], + [[((number_literal) @literal (#not-has-ancestor? @literal "enum_specifier"))]] + ) + eq({ + { 'literal', 'number_literal', { 0, 8, 0, 11 }, '123' }, + { 'literal', 'number_literal', { 2, 21, 2, 24 }, '125' }, + }, result) + end) + it('allows loading query with escaped quotes and capture them `#{lua,vim}-match`?', function() insert('char* astring = "Hello World!";') |