diff options
| author | Jędrzej Boczar <yendreij@gmail.com> | 2021-04-23 00:41:44 +0200 |
|---|---|---|
| committer | Kiyan <yazdani.kiyan@protonmail.com> | 2021-04-23 21:21:38 +0200 |
| commit | af3537fbe57a2a37ab2b620c9ecc487e31b4da64 (patch) | |
| tree | 1142bbbe292ff58f19bf5db9732a20550c4c0206 /tests/indent/common.lua | |
| parent | tests/indent: move common code out of main lua/ directory (diff) | |
| download | nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar.gz nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar.bz2 nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar.lz nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar.xz nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.tar.zst nvim-treesitter-af3537fbe57a2a37ab2b620c9ecc487e31b4da64.zip | |
tests/indent: factor out most of the code into common.lua
Diffstat (limited to 'tests/indent/common.lua')
| -rw-r--r-- | tests/indent/common.lua | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/indent/common.lua b/tests/indent/common.lua index 96caf17f9..ff30b5a5d 100644 --- a/tests/indent/common.lua +++ b/tests/indent/common.lua @@ -2,6 +2,8 @@ local M = {} local assert = require('luassert') local say = require('say') +local scan_dir = require('plenary.scandir').scan_dir +local Path = require('plenary.path') local function same_indent(state, arguments) local before = arguments[1] @@ -73,7 +75,7 @@ local function set_buf_indent_opts(opts) end function M.run_indent_test(file, runner, opts) - assert.are.same(1, vim.fn.filereadable(file)) + assert.are.same(1, vim.fn.filereadable(file), string.format('File "%s" not readable', file)) -- load reference file vim.cmd(string.format('edit %s', file)) @@ -122,4 +124,45 @@ function M.indent_new_line(file, spec, opts) compare_indent(before, after) end +local Runner = {} +Runner.__index = Runner + +-- Helper to avoid boilerplate when defining tests +-- @param it the "it" function that busted defines globally in spec files +-- @param base_dir all other paths will be resolved relative to this directory +-- @param buf_opts buffer options passed to set_buf_indent_opts +function Runner:new(it, base_dir, buf_opts) + local runner = {} + runner.it = it + runner.base_dir = Path:new(base_dir) + runner.buf_opts = buf_opts + return setmetatable(runner, self) +end + +function Runner:whole_file(dirs) + dirs = type(dirs) == "table" and dirs or {dirs} + dirs = vim.tbl_map(function(dir) + dir = self.base_dir / Path:new(dir) + assert.is.same(1, vim.fn.isdirectory(dir.filename)) + return dir.filename + end, dirs) + local files = vim.tbl_flatten(vim.tbl_map(scan_dir, dirs)) + for _, file in ipairs(files) do + local relpath = Path:new(file):make_relative(self.base_dir.filename) + self.it(relpath, function() + M.indent_whole_file(file, self.buf_opts) + end) + end +end + +function Runner:new_line(file, spec, title) + title = title and title or tostring(spec.on_line) + self.it(string.format('%s[%s]', file, title), function() + local path = self.base_dir / file + M.indent_new_line(path.filename, spec, self.buf_opts) + end) +end + +M.Runner = Runner + return M |
