From 4ea5e63aa8c866b4fcc9d10f1a26078d2517f96a Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 9 Jun 2019 13:26:48 +0200 Subject: tree-sitter: add basic testing on ci build tree-sitter c parser on ci for testing purposes --- test/functional/lua/tree_sitter_spec.lua | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/functional/lua/tree_sitter_spec.lua (limited to 'test/functional/lua') diff --git a/test/functional/lua/tree_sitter_spec.lua b/test/functional/lua/tree_sitter_spec.lua new file mode 100644 index 0000000000..e25eb47a60 --- /dev/null +++ b/test/functional/lua/tree_sitter_spec.lua @@ -0,0 +1,67 @@ +-- Test suite for testing interactions with API bindings +local helpers = require('test.functional.helpers')(after_each) + +local meths = helpers.meths +local clear = helpers.clear +local eq = helpers.eq +local insert = helpers.insert +local meth_pcall = helpers.meth_pcall +local exec_lua = helpers.exec_lua +local iswin = helpers.iswin + +before_each(clear) + +describe('tree-sitter API', function() + -- error tests not requiring a parser library + it('handles basic errors', function() + --eq({false, 'Error executing lua: vim.schedule: expected function'}, + -- meth_pcall(meths.execute_lua, "parser = vim.tree_sitter.create_parser(0, 'nosuchlang')", {})) + + + + end) + + local ts_path = os.getenv("TREE_SITTER_DIR") + + describe('with C parser', function() + if ts_path == nil then + it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) + return + end + + before_each(function() + -- TODO the .so/.dylib/.dll thingie + local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') + exec_lua([[ + local path = ... + vim.tree_sitter.add_language(path,'c') + + ]], path) + end) + + it('parses buffer', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + parser = vim.tree_sitter.create_parser(0, "c") + tree = parser:parse_tree() + root = tree:root() + ]]) + + --eq("", exec_lua("return tostring(parser)")) + eq("", exec_lua("return tostring(tree)")) + eq("", exec_lua("return tostring(root)")) + eq({0,0,3,0}, exec_lua("return {root:range()}")) + + eq(1, exec_lua("return root:child_count()")) + exec_lua("child = root:child(0)") + eq("", exec_lua("return tostring(child)")) + eq({0,0,2,1}, exec_lua("return {child:range()}")) + end) + + end) +end) + -- cgit From c8f861b739b4703b1198dc1f88b09edbeb0d9f2e Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sat, 15 Jun 2019 12:10:12 +0200 Subject: tree-sitter: rename tree_sitter => treesitter for consistency --- test/functional/lua/tree_sitter_spec.lua | 67 -------------------------------- test/functional/lua/treesitter_spec.lua | 67 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 67 deletions(-) delete mode 100644 test/functional/lua/tree_sitter_spec.lua create mode 100644 test/functional/lua/treesitter_spec.lua (limited to 'test/functional/lua') diff --git a/test/functional/lua/tree_sitter_spec.lua b/test/functional/lua/tree_sitter_spec.lua deleted file mode 100644 index e25eb47a60..0000000000 --- a/test/functional/lua/tree_sitter_spec.lua +++ /dev/null @@ -1,67 +0,0 @@ --- Test suite for testing interactions with API bindings -local helpers = require('test.functional.helpers')(after_each) - -local meths = helpers.meths -local clear = helpers.clear -local eq = helpers.eq -local insert = helpers.insert -local meth_pcall = helpers.meth_pcall -local exec_lua = helpers.exec_lua -local iswin = helpers.iswin - -before_each(clear) - -describe('tree-sitter API', function() - -- error tests not requiring a parser library - it('handles basic errors', function() - --eq({false, 'Error executing lua: vim.schedule: expected function'}, - -- meth_pcall(meths.execute_lua, "parser = vim.tree_sitter.create_parser(0, 'nosuchlang')", {})) - - - - end) - - local ts_path = os.getenv("TREE_SITTER_DIR") - - describe('with C parser', function() - if ts_path == nil then - it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) - return - end - - before_each(function() - -- TODO the .so/.dylib/.dll thingie - local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') - exec_lua([[ - local path = ... - vim.tree_sitter.add_language(path,'c') - - ]], path) - end) - - it('parses buffer', function() - insert([[ - int main() { - int x = 3; - }]]) - - exec_lua([[ - parser = vim.tree_sitter.create_parser(0, "c") - tree = parser:parse_tree() - root = tree:root() - ]]) - - --eq("", exec_lua("return tostring(parser)")) - eq("", exec_lua("return tostring(tree)")) - eq("", exec_lua("return tostring(root)")) - eq({0,0,3,0}, exec_lua("return {root:range()}")) - - eq(1, exec_lua("return root:child_count()")) - exec_lua("child = root:child(0)") - eq("", exec_lua("return tostring(child)")) - eq({0,0,2,1}, exec_lua("return {child:range()}")) - end) - - end) -end) - diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua new file mode 100644 index 0000000000..8916e59563 --- /dev/null +++ b/test/functional/lua/treesitter_spec.lua @@ -0,0 +1,67 @@ +-- Test suite for testing interactions with API bindings +local helpers = require('test.functional.helpers')(after_each) + +local meths = helpers.meths +local clear = helpers.clear +local eq = helpers.eq +local insert = helpers.insert +local meth_pcall = helpers.meth_pcall +local exec_lua = helpers.exec_lua +local iswin = helpers.iswin + +before_each(clear) + +describe('tree-sitter API', function() + -- error tests not requiring a parser library + it('handles basic errors', function() + --eq({false, 'Error executing lua: vim.schedule: expected function'}, + -- meth_pcall(meths.execute_lua, "parser = vim.treesitter.create_parser(0, 'nosuchlang')", {})) + + + + end) + + local ts_path = os.getenv("TREE_SITTER_DIR") + + describe('with C parser', function() + if ts_path == nil then + it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) + return + end + + before_each(function() + -- TODO the .so/.dylib/.dll thingie + local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') + exec_lua([[ + local path = ... + vim.treesitter.add_language(path,'c') + + ]], path) + end) + + it('parses buffer', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + parser = vim.treesitter.create_parser(0, "c") + tree = parser:parse_tree() + root = tree:root() + ]]) + + --eq("", exec_lua("return tostring(parser)")) + eq("", exec_lua("return tostring(tree)")) + eq("", exec_lua("return tostring(root)")) + eq({0,0,3,0}, exec_lua("return {root:range()}")) + + eq(1, exec_lua("return root:child_count()")) + exec_lua("child = root:child(0)") + eq("", exec_lua("return tostring(child)")) + eq({0,0,2,1}, exec_lua("return {child:range()}")) + end) + + end) +end) + -- cgit From 167a1cfdef0c4b3526830ad0356f06bf480df6af Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Mon, 17 Jun 2019 21:46:31 +0200 Subject: tree-sitter: improve parser API (shared parser between plugins) --- test/functional/lua/treesitter_spec.lua | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 8916e59563..f3f7f4fd0a 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -8,6 +8,7 @@ local insert = helpers.insert local meth_pcall = helpers.meth_pcall local exec_lua = helpers.exec_lua local iswin = helpers.iswin +local feed = helpers.feed before_each(clear) @@ -46,12 +47,11 @@ describe('tree-sitter API', function() }]]) exec_lua([[ - parser = vim.treesitter.create_parser(0, "c") - tree = parser:parse_tree() + parser = vim.treesitter.get_parser(0, "c") + tree = parser:parse() root = tree:root() ]]) - --eq("", exec_lua("return tostring(parser)")) eq("", exec_lua("return tostring(tree)")) eq("", exec_lua("return tostring(root)")) eq({0,0,3,0}, exec_lua("return {root:range()}")) @@ -60,6 +60,27 @@ describe('tree-sitter API', function() exec_lua("child = root:child(0)") eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) + + exec_lua("descendant = root:descendant_for_point_range(1,2,1,12)") + eq("", exec_lua("return tostring(descendant)")) + eq({1,2,1,12}, exec_lua("return {descendant:range()}")) + eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + + feed("2G7|ay") + exec_lua([[ + tree2 = parser:parse() + root2 = tree2:root() + descendant2 = root2:descendant_for_point_range(1,2,1,13) + ]]) + eq(false, exec_lua("return tree2 == tree1")) + eq("", exec_lua("return tostring(descendant2)")) + eq({1,2,1,13}, exec_lua("return {descendant2:range()}")) + + -- orginal tree did not change + eq({1,2,1,12}, exec_lua("return {descendant:range()}")) + + -- unchanged buffer: return the same tree + eq(true, exec_lua("return parser:parse() == tree2")) end) end) -- cgit From 06ee45b9b1c14c7ce6cb23403cdbe2852d495cad Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Fri, 21 Jun 2019 14:14:51 +0200 Subject: tree-sitter: fix lint, delete "demo" plugin (replaced by functional tests) --- test/functional/lua/treesitter_spec.lua | 2 -- 1 file changed, 2 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index f3f7f4fd0a..5aaaa80868 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -1,11 +1,9 @@ -- Test suite for testing interactions with API bindings local helpers = require('test.functional.helpers')(after_each) -local meths = helpers.meths local clear = helpers.clear local eq = helpers.eq local insert = helpers.insert -local meth_pcall = helpers.meth_pcall local exec_lua = helpers.exec_lua local iswin = helpers.iswin local feed = helpers.feed -- cgit From e0d6228978dd1389f75a3e0351f6e6d5625643ae Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sat, 21 Sep 2019 10:10:47 +0200 Subject: tree-sitter: use "range" instead of "point_range" consistently in lua API --- test/functional/lua/treesitter_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 5aaaa80868..d566f15649 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -59,7 +59,7 @@ describe('tree-sitter API', function() eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) - exec_lua("descendant = root:descendant_for_point_range(1,2,1,12)") + exec_lua("descendant = root:descendant_for_range(1,2,1,12)") eq("", exec_lua("return tostring(descendant)")) eq({1,2,1,12}, exec_lua("return {descendant:range()}")) eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) @@ -68,7 +68,7 @@ describe('tree-sitter API', function() exec_lua([[ tree2 = parser:parse() root2 = tree2:root() - descendant2 = root2:descendant_for_point_range(1,2,1,13) + descendant2 = root2:descendant_for_range(1,2,1,13) ]]) eq(false, exec_lua("return tree2 == tree1")) eq("", exec_lua("return tostring(descendant2)")) -- cgit From d5a69eb07648a515d03aa5c9e268aef852015ea9 Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 22 Sep 2019 11:33:55 +0200 Subject: tree-sitter: handle node equality --- test/functional/lua/treesitter_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/functional/lua') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index d566f15649..8e21faca12 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -64,6 +64,13 @@ describe('tree-sitter API', function() eq({1,2,1,12}, exec_lua("return {descendant:range()}")) eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + eq(true, exec_lua("return child == child")) + -- separate lua object, but represents same node + eq(true, exec_lua("return child == root:child(0)")) + eq(false, exec_lua("return child == descendant2")) + eq(false, exec_lua("return child == nil")) + eq(false, exec_lua("return child == tree")) + feed("2G7|ay") exec_lua([[ tree2 = parser:parse() @@ -71,6 +78,7 @@ describe('tree-sitter API', function() descendant2 = root2:descendant_for_range(1,2,1,13) ]]) eq(false, exec_lua("return tree2 == tree1")) + eq(false, exec_lua("return root2 == root")) eq("", exec_lua("return tostring(descendant2)")) eq({1,2,1,13}, exec_lua("return {descendant2:range()}")) -- cgit From 9fa850991dbe8984996afdc149b5b32dc248197e Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sat, 28 Sep 2019 09:32:55 +0200 Subject: tree-sitter: improve and cleanup tests --- test/functional/lua/treesitter_spec.lua | 73 +++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 9 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 8e21faca12..700e4599f2 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -7,34 +7,40 @@ local insert = helpers.insert local exec_lua = helpers.exec_lua local iswin = helpers.iswin local feed = helpers.feed +local pcall_err = helpers.pcall_err +local matches = helpers.matches before_each(clear) -describe('tree-sitter API', function() +describe('treesitter API', function() -- error tests not requiring a parser library - it('handles basic errors', function() - --eq({false, 'Error executing lua: vim.schedule: expected function'}, - -- meth_pcall(meths.execute_lua, "parser = vim.treesitter.create_parser(0, 'nosuchlang')", {})) + it('handles missing language', function() + local path_pat = 'Error executing lua: '..(iswin() and '.+\\vim\\' or '.+/vim/') + matches(path_pat..'treesitter.lua:39: no such language: borklang', + pcall_err(exec_lua, "parser = vim.treesitter.create_parser(0, 'borklang')")) + -- actual message depends on platform + matches('Error executing lua: Failed to load parser: uv_dlopen: .+', + pcall_err(exec_lua, "parser = vim.treesitter.add_language('borkbork.so', 'borklang')")) + eq('Error executing lua: [string ""]:1: no such language: borklang', + pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')")) end) local ts_path = os.getenv("TREE_SITTER_DIR") describe('with C parser', function() if ts_path == nil then - it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) + it("works", function() pending("TREE_SITTER_PATH not set, skipping treesitter parser tests") end) return end before_each(function() - -- TODO the .so/.dylib/.dll thingie local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') exec_lua([[ local path = ... vim.treesitter.add_language(path,'c') - ]], path) end) @@ -48,6 +54,7 @@ describe('tree-sitter API', function() parser = vim.treesitter.get_parser(0, "c") tree = parser:parse() root = tree:root() + lang = vim.treesitter.inspect_language('c') ]]) eq("", exec_lua("return tostring(tree)")) @@ -59,10 +66,21 @@ describe('tree-sitter API', function() eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) + eq("function_definition", exec_lua("return child:type()")) + eq(true, exec_lua("return child:named()")) + eq("number", type(exec_lua("return child:symbol()"))) + eq({'function_definition', true}, exec_lua("return lang.symbols[child:symbol()]")) + + exec_lua("anon = root:descendant_for_range(0,8,0,9)") + eq("(", exec_lua("return anon:type()")) + eq(false, exec_lua("return anon:named()")) + eq("number", type(exec_lua("return anon:symbol()"))) + eq({'(', false}, exec_lua("return lang.symbols[anon:symbol()]")) + exec_lua("descendant = root:descendant_for_range(1,2,1,12)") eq("", exec_lua("return tostring(descendant)")) eq({1,2,1,12}, exec_lua("return {descendant:range()}")) - eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + eq("(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))", exec_lua("return descendant:sexpr()")) eq(true, exec_lua("return child == child")) -- separate lua object, but represents same node @@ -89,6 +107,43 @@ describe('tree-sitter API', function() eq(true, exec_lua("return parser:parse() == tree2")) end) + it('inspects language', function() + local keys, fields, symbols = unpack(exec_lua([[ + local lang = vim.treesitter.inspect_language('c') + local keys, symbols = {}, {} + for k,_ in pairs(lang) do + keys[k] = true + end + + -- symbols array can have "holes" and is thus not a valid msgpack array + -- but we don't care about the numbers here (checked in the parser test) + for _, v in pairs(lang.symbols) do + table.insert(symbols, v) + end + return {keys, lang.fields, symbols} + ]])) + + eq({fields=true, symbols=true}, keys) + + local fset = {} + for _,f in pairs(fields) do + eq("string", type(f)) + fset[f] = true + end + eq(true, fset["directive"]) + eq(true, fset["initializer"]) + + local has_named, has_anonymous + for _,s in pairs(symbols) do + eq("string", type(s[1])) + eq("boolean", type(s[2])) + if s[1] == "for_statement" and s[2] == true then + has_named = true + elseif s[1] == "|=" and s[2] == false then + has_anonymous = true + end + end + eq({true,true}, {has_named,has_anonymous}) + end) end) end) - -- cgit