diff options
| author | Stephan Seitz <stephan.seitz@fau.de> | 2021-11-27 13:30:13 +0100 |
|---|---|---|
| committer | Stephan Seitz <stephan.seitz@fau.de> | 2021-12-20 18:16:45 +0100 |
| commit | a3862c9802a3695b35eabfcd458236f93b54810b (patch) | |
| tree | 10972456ab6af5027e3e18f17255a2c986d74724 /tests/query/injection_spec.lua | |
| parent | ci: Remove "ensure_installed" from minimal_init.lua (diff) | |
| download | nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar.gz nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar.bz2 nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar.lz nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar.xz nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.tar.zst nvim-treesitter-a3862c9802a3695b35eabfcd458236f93b54810b.zip | |
ci: add injection tests
Diffstat (limited to 'tests/query/injection_spec.lua')
| -rw-r--r-- | tests/query/injection_spec.lua | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/query/injection_spec.lua b/tests/query/injection_spec.lua new file mode 100644 index 000000000..abbd7a8ae --- /dev/null +++ b/tests/query/injection_spec.lua @@ -0,0 +1,63 @@ +require "nvim-treesitter.highlight" -- yes, this is necessary to set the hlmap +local highlighter = require "vim.treesitter.highlighter" +local utils = require "nvim-treesitter.utils" +local ts_utils = require "nvim-treesitter.ts_utils" +local parsers = require "nvim-treesitter.parsers" + +local function check_assertions(file) + local buf = vim.fn.bufadd(file) + vim.fn.bufload(file) + local lang = parsers.get_buf_lang(buf) + assert.same( + 1, + vim.fn.executable "highlight-assertions", + '"highlight-assertions" not executable!' + .. ' Get it via "cargo install --git https://github.com/theHamsta/highlight-assertions"' + ) + local assertions = vim.fn.json_decode( + vim.fn.system( + "highlight-assertions -p '" .. utils.get_parser_install_dir() .. "/" .. lang .. ".so'" .. " -s '" .. file .. "'" + ) + ) + local parser = parsers.get_parser(buf, lang) + + local self = highlighter.new(parser, {}) + + for _, assertion in ipairs(assertions) do + local row = assertion.position.row + local col = assertion.position.column + + local found = false + self.tree:for_each_tree(function(tstree, tree) + if not tstree then + return + end + + local root = tstree:root() + if ts_utils.is_in_node_range(root, row, col) and assertion.expected_capture_name == tree:lang() then + found = true + end + end, true) + assert.True( + found, + "Error in at " + .. file + .. ":" + .. (row + 1) + .. ":" + .. (col + 1) + .. ': expected "' + .. assertion.expected_capture_name + .. '" to be injected here!' + ) + end +end + +describe("injections", function() + local files = vim.fn.split(vim.fn.glob "tests/query/injections/**/*.*") + for _, file in ipairs(files) do + it(file, function() + check_assertions(file) + end) + end +end) |
