From 81fdd0781e3592d61889fbb9888c08818d55978a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 11 Dec 2024 13:44:43 +0000 Subject: [PATCH] test: split tests into more files --- README.md | 152 ++++++++++++++++++++++++++--- test/contexts_spec.lua | 123 ++++++++++++++++++++++++ test/helpers.lua | 58 +++++++++++ test/lang/test.go | 21 ++-- test/queries_spec.lua | 80 ++++++++++++++++ test/ts_context_spec.lua | 201 +-------------------------------------- 6 files changed, 417 insertions(+), 218 deletions(-) create mode 100644 test/contexts_spec.lua create mode 100644 test/helpers.lua create mode 100644 test/queries_spec.lua diff --git a/README.md b/README.md index c3e7f727..34f90081 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Note: support for specific languages is strictly community maintained and can br
click to expand diff --git a/test/contexts_spec.lua b/test/contexts_spec.lua new file mode 100644 index 00000000..4713ff26 --- /dev/null +++ b/test/contexts_spec.lua @@ -0,0 +1,123 @@ +local helpers = require('nvim-test.helpers') +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local cmd = helpers.api.nvim_command +local feed = helpers.feed +local api = helpers.api +local fn = helpers.fn + +local tc_helpers = require('test.helpers') +local install_langs = tc_helpers.install_langs +local get_langs = tc_helpers.get_langs + +---@param line string +---@return string? +local function parse_directive(line) + --- @type string? + local directive = line:match('{{([A-Z]+)}}') + return directive +end + +--- @param filename string +--- @return table? contexts +local function parse_directives(filename) + local f = io.open(filename, 'r') + if not f then + return + end + + local context = {} --- @type table + local contexts = {} --- @type table + + local i = 0 + for l in f:lines() do + local directive = parse_directive(l) + if directive then + if directive == 'TEST' then + context = {} + elseif directive == 'CURSOR' then + contexts[i] = vim.deepcopy(context) + elseif directive == 'CONTEXT' then + table.insert(context, i) + elseif directive == 'POPCONTEXT' then + table.remove(context, #context) + end + end + i = i + 1 + end + f:close() + + for _, c in pairs(contexts) do + table.sort(c) + end + + return contexts +end + +local langs = get_langs() +local langs_with_queries = {} +for _, lang in ipairs(langs) do + if vim.uv.fs_stat('queries/' .. lang .. '/context.scm') then + table.insert(langs_with_queries, lang) + end +end + +for _, lang in ipairs(langs_with_queries) do + describe('contexts (' .. lang .. '):', function() + local test_file = 'test/lang/test.' .. lang + if not vim.uv.fs_stat(test_file) then + pending('No test file') + return + end + + local contexts = parse_directives(test_file) + + if not contexts or not next(contexts) then + pending('No tests') + return + end + + setup(function() + clear() + cmd([[set runtimepath+=.,./nvim-treesitter]]) + + -- Required to load custom predicates + exec_lua([[require'nvim-treesitter'.setup()]]) + + cmd([[let $XDG_CACHE_HOME='scratch/cache']]) + + install_langs(lang) + end) + + for cursor_row, context_rows in pairs(contexts) do + it(('line %s in %s'):format(cursor_row, test_file), function() + cmd('edit ' .. test_file) + local bufnr = api.nvim_get_current_buf() + local winid = api.nvim_get_current_win() + api.nvim_win_set_cursor(winid, { cursor_row + 1, 0 }) + assert(fn.getline('.'):match('{{CURSOR}}')) + feed(string.format('zt%d', #context_rows + 2)) + + --- @type [integer,integer,integer,integer][] + local ranges = exec_lua( + [[ + return require('treesitter-context.context').get(...) + ]], + bufnr, + winid + ) + + local act_context_rows = {} --- @type integer[] + for _, r in ipairs(ranges) do + table.insert(act_context_rows, r[1]) + end + + helpers.eq( + context_rows, + act_context_rows, + string.format('test for cursor %d failed', cursor_row) + ) + end) + end + end) +end diff --git a/test/helpers.lua b/test/helpers.lua new file mode 100644 index 00000000..e9a9bc0e --- /dev/null +++ b/test/helpers.lua @@ -0,0 +1,58 @@ +local helpers = require('nvim-test.helpers') +local exec_lua = helpers.exec_lua + +local M = {} + +function M.install_langs(langs) + if type(langs) == 'string' then + langs = { langs } + end + exec_lua( + [[ + local langs = ... + require'nvim-treesitter.configs'.setup { + ensure_installed = langs, + sync_install = true, + } + + -- Clear the message " has been installed". + print(' ') + ]], + langs + ) +end + +local langs --- @type string[]? + +function M.get_langs() + if langs then + return langs + end + + langs = {} + local f = assert(io.open('README.md', 'r')) + local readme_langs = {} --- @type table + for l in f:lines() do + --- @type string? + local lang = l:match('%- %[x%] `([^`]+)`') + if lang then + readme_langs[lang] = true + end + end + f:close() + + f = assert(io.open('nvim-treesitter/lockfile.json', 'r')) + + for k in vim.spairs(vim.json.decode(f:read('*a'))) do + langs[#langs + 1] = k + if readme_langs[k] then + readme_langs[k] = nil + end + end + if next(readme_langs) then + print('Invalid languages in README:', table.concat(vim.tbl_keys(readme_langs), ', ')) + end + return langs +end + +return M diff --git a/test/lang/test.go b/test/lang/test.go index 6ee64646..66439dd3 100644 --- a/test/lang/test.go +++ b/test/lang/test.go @@ -1,17 +1,21 @@ -import ( +// {{TEST}} + +import ( // {{CONTEXT}} "errors" "fmt" + // {{CURSOR}} ) -func (r *rect) area(a int, +// {{TEST}} + +func (r *rect) area(a int, // {{CONTEXT}} b int) int { return r.width * r.height - - +// {{CURSOR}} } @@ -20,18 +24,21 @@ var b ,c ,d int = 1, 2 -func foo(a int, +// {{TEST}} + +func foo(a int, // {{CONTEXT}} b int) (int, int) { i := 1 - select { + select { // {{CONTEXT}} case msg1 := <-c1: fmt.Println("received", msg1) - case msg2 := <-c2: + case msg2 := <-c2: // {{CONTEXT}} + // {{CURSOR}} fmt.Println("received", msg2) diff --git a/test/queries_spec.lua b/test/queries_spec.lua new file mode 100644 index 00000000..92b16be1 --- /dev/null +++ b/test/queries_spec.lua @@ -0,0 +1,80 @@ +--- Test the query for each language is valid and update the README. +local helpers = require('nvim-test.helpers') +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local cmd = helpers.api.nvim_command + +local tc_helpers = require('test.helpers') +local install_langs = tc_helpers.install_langs +local get_langs = tc_helpers.get_langs + +describe('query:', function() + local readme_lines = {} --- @type string[] + + setup(function() + clear() + cmd([[set runtimepath+=.,./nvim-treesitter]]) + -- Required to load custom predicates + exec_lua([[require'nvim-treesitter'.setup()]]) + cmd([[let $XDG_CACHE_HOME='scratch/cache']]) + + local f = assert(io.open('README.md', 'r')) + for l in f:lines() do + readme_lines[#readme_lines + 1] = l + end + f:close() + end) + + for _, lang in ipairs(get_langs()) do + it(lang, function() + local lang_index --- @type integer + local last_supported_lang_index --- @type integer + local last_lang_index --- @type integer + + -- Find the line in the README for this lang + for i, l in ipairs(readme_lines) do + --- @type string? + local tick, lang1 = l:match('%- %[(.)%] `([^`]+)`') + if lang1 then + if tick == 'x' then + last_supported_lang_index = i + else + last_lang_index = i + end + + if lang1 == lang then + lang_index = i + end + end + end + + if lang_index then + table.remove(readme_lines, lang_index) + end + + if not vim.uv.fs_stat('queries/' .. lang .. '/context.scm') then + table.insert(readme_lines, last_lang_index, (' - [ ] `%s`'):format(lang)) + pending('no queries/' .. lang .. '/context.scm') + else + install_langs(lang) + local ok = exec_lua([[return pcall(vim.treesitter.query.get, ...)]], lang, 'context') + table.insert( + readme_lines, + last_supported_lang_index, + (' - [x] `%s`%s'):format(lang, ok and '' or ' (broken)') + ) + assert(ok) + end + end) + end + + teardown(function() + -- Update the README. + local f = assert(io.open('README.md', 'w')) + for _, l in ipairs(readme_lines) do + f:write(l) + f:write('\n') + end + f:close() + end) +end) diff --git a/test/ts_context_spec.lua b/test/ts_context_spec.lua index fb2a59f8..817a70db 100644 --- a/test/ts_context_spec.lua +++ b/test/ts_context_spec.lua @@ -1,11 +1,12 @@ local helpers = require('nvim-test.helpers') local Screen = require('nvim-test.screen') +local install_langs = require('test.helpers').install_langs + local clear = helpers.clear local exec_lua = helpers.exec_lua local cmd = helpers.api.nvim_command local feed = helpers.feed -local api = helpers.api local fn = helpers.fn local function requires_nvim10() @@ -14,95 +15,6 @@ local function requires_nvim10() end end -local function install_langs(langs) - if type(langs) == 'string' then - langs = { langs } - end - exec_lua( - [[ - local langs = ... - require'nvim-treesitter.configs'.setup { - ensure_installed = langs, - sync_install = true, - } - - -- Clear the message " has been installed". - print(' ') - ]], - langs - ) -end - ----@param line string ----@return string? -local function parse_directive(line) - --- @type string? - local directive = line:match('{{([A-Z]+)}}') - return directive -end - ---- @param filename string ---- @return table? contexts -local function parse_directives(filename) - local f = io.open(filename, 'r') - if not f then - return - end - - local context = {} --- @type table - local contexts = {} --- @type table - - local i = 0 - for l in f:lines() do - local directive = parse_directive(l) - if directive then - if directive == 'TEST' then - context = {} - elseif directive == 'CURSOR' then - contexts[i] = vim.deepcopy(context) - elseif directive == 'CONTEXT' then - table.insert(context, i) - elseif directive == 'POPCONTEXT' then - table.remove(context, #context) - end - end - i = i + 1 - end - f:close() - - for _, c in pairs(contexts) do - table.sort(c) - end - - return contexts -end - -local langs = {} --- @type string[] -do - local f = assert(io.open('README.md', 'r')) - local readme_langs = {} --- @type table - for l in f:lines() do - --- @type string? - local lang = l:match('%- %[x%] `([^`]+)`') - if lang then - readme_langs[lang] = true - end - end - f:close() - - f = assert(io.open('nvim-treesitter/lockfile.json', 'r')) - - for k in pairs(vim.json.decode(f:read('*a'))) do - if readme_langs[k] then - langs[#langs + 1] = k - readme_langs[k] = nil - end - end - if next(readme_langs) then - print('Invalid languages:', table.concat(vim.tbl_keys(readme_langs), ', ')) - end -end - describe('ts_context', function() local screen --- @type test.screen @@ -135,7 +47,7 @@ describe('ts_context', function() [14] = { background = Screen.colors.LightMagenta, foreground = Screen.colors.SlateBlue }, [15] = { foreground = Screen.colors.SlateBlue }, [16] = { foreground = tonumber('0x6a0dad') }, - [17] = { background = Screen.colors.Plum1, bold = true, foreground = Screen.colors.Magenta1}, + [17] = { background = Screen.colors.Plum1, bold = true, foreground = Screen.colors.Magenta1 }, } -- Use the classic vim colorscheme, not the new defaults in nvim >= 0.10 @@ -146,8 +58,6 @@ describe('ts_context', function() cmd('hi link @type.builtin Special') cmd('hi link @keyword.type Type') cmd('hi link @attribute PreProc') - -- cmd('hi link @property Identifier') - -- -- default_attrs[2] = { background = Screen.colors.LightMagenta } end screen:set_default_attr_ids(default_attrs) @@ -212,111 +122,6 @@ describe('ts_context', function() }) end) - describe('query:', function() - local readme_lines = {} --- @type string[] - - setup(function() - local f = assert(io.open('README.md', 'r')) - for l in f:lines() do - readme_lines[#readme_lines + 1] = l - end - f:close() - end) - - for _, lang in ipairs(langs) do - it(lang, function() - install_langs(lang) - - local index --- @type integer - local line_orig --- @type string - - for i, l in pairs(readme_lines) do - --- @type string? - local lang1 = l:match('%- %[x%] `([^`]+)`') - if lang1 == lang then - l = l:gsub(' %(broken%)', '') - index, line_orig = i, l - readme_lines[i] = l .. ' (broken)' - else - readme_lines[i] = l - end - end - - assert(index) - - exec_lua( - [[ - local lang = ... - vim.treesitter.query.get(lang, 'context') - ]], - lang - ) - - readme_lines[index] = line_orig - end) - end - - teardown(function() - local f = assert(io.open('README.md', 'w')) - for _, l in ipairs(readme_lines) do - f:write(l) - f:write('\n') - end - f:close() - end) - end) - - describe('contexts:', function() - for _, lang in ipairs(langs) do - it(lang, function() - install_langs(lang) - - local test_file = 'test/lang/test.' .. lang - if not vim.uv.fs_stat(test_file) then - pending('No test file') - return - end - - local contexts = parse_directives(test_file) - - if not contexts or not next(contexts) then - pending('No tests') - return - end - - cmd('edit ' .. test_file) - - for cursor_row, context_rows in pairs(contexts) do - local bufnr = api.nvim_get_current_buf() - local winid = api.nvim_get_current_win() - api.nvim_win_set_cursor(winid, { cursor_row + 1, 0 }) - assert(fn.getline('.'):match('{{CURSOR}}')) - feed(string.format('zt%d', #context_rows + 2)) - - --- @type [integer,integer,integer,integer][] - local ranges = exec_lua( - [[ - return require('treesitter-context.context').get(...) - ]], - bufnr, - winid - ) - - local act_context_rows = {} --- @type integer[] - for _, r in ipairs(ranges) do - table.insert(act_context_rows, r[1]) - end - - helpers.eq( - context_rows, - act_context_rows, - string.format('test for cursor %d failed', cursor_row) - ) - end - end) - end - end) - describe('language:', function() before_each(function() exec_lua([[require'treesitter-context'.setup{