|
| 1 | +local assert = require('luassert') |
| 2 | +local ts = vim.treesitter |
| 3 | +local queries_dir = './queries' |
| 4 | + |
| 5 | +local function has_parser(lang) |
| 6 | + local inspect = (ts.language or {}).inspect or ts.inspect |
| 7 | + return pcall(inspect, lang) |
| 8 | +end |
| 9 | + |
| 10 | +---@type string[] paths |
| 11 | +local local_queries = vim.tbl_map(function(path) |
| 12 | + return path:gsub([[\]], '/'):gsub('^%./', '') |
| 13 | +end, vim.fn.glob(queries_dir .. '/**/*.[Ss][Cc][Mm]', true, true, true)) |
| 14 | + |
| 15 | +describe('./queries directory', function() |
| 16 | + it('should have the correct structure', function() |
| 17 | + assert.is.equal( |
| 18 | + 'dir', |
| 19 | + vim.fn.getftype(queries_dir), |
| 20 | + ('`%s` does not exist or is not a directory'):format(queries_dir) |
| 21 | + ) |
| 22 | + |
| 23 | + local langs = vim.fn.readdir(queries_dir) |
| 24 | + |
| 25 | + assert( |
| 26 | + #langs > 0, |
| 27 | + ('the queries directory `%s` appears to be empty'):format(queries_dir) |
| 28 | + ) |
| 29 | + |
| 30 | + for _, lang in ipairs(langs) do |
| 31 | + local path = ('%s/%s'):format(queries_dir, lang) |
| 32 | + |
| 33 | + assert.is.equal( |
| 34 | + 'dir', |
| 35 | + vim.fn.getftype(path), |
| 36 | + ('`%s` is not a directory'):format(path) |
| 37 | + ) |
| 38 | + |
| 39 | + for _, file in ipairs(vim.fn.readdir(path)) do |
| 40 | + path = ('%s/%s'):format(path, file) |
| 41 | + |
| 42 | + assert.is.equal( |
| 43 | + 'file', |
| 44 | + vim.fn.getftype(path), |
| 45 | + ('`%s` is not a file'):format(path) |
| 46 | + ) |
| 47 | + |
| 48 | + assert( |
| 49 | + file:lower():find('%.scm$'), |
| 50 | + ('query file `%s` does not end with `.scm` extension'):format(path) |
| 51 | + ) |
| 52 | + end |
| 53 | + end |
| 54 | + end) |
| 55 | +end) |
| 56 | + |
| 57 | +describe('queries', function() |
| 58 | + if vim.fn.has('nvim-0.9.0') == 1 then |
| 59 | + it('should parse cleanly (no syntax errors, no undefined nodes/symbols)', function() |
| 60 | + local found_query = false |
| 61 | + |
| 62 | + for _, path in ipairs(local_queries) do |
| 63 | + local lang = path:match('^queries/([^/]+)/[^/]+%.[Ss][Cc][Mm]$') |
| 64 | + |
| 65 | + if lang then |
| 66 | + found_query = true |
| 67 | + |
| 68 | + local query |
| 69 | + |
| 70 | + do |
| 71 | + local f = assert(io.open(path, 'r')) |
| 72 | + query = f:read('*a') |
| 73 | + f:close() |
| 74 | + end |
| 75 | + |
| 76 | + if has_parser(lang) == false then |
| 77 | + print( |
| 78 | + ([[::warning title=Test skipped::skipped query parsing test because the treesitter parser for `%s` is missing]]):format( |
| 79 | + lang |
| 80 | + ) |
| 81 | + ) |
| 82 | + else |
| 83 | + local parsed |
| 84 | + |
| 85 | + assert.does_not_error(function() |
| 86 | + -- This function should error if the query is syntactically invalid |
| 87 | + -- or refers to any nodes not defined by `lang`. Unsure if it also |
| 88 | + -- checks used directives and predicates? |
| 89 | + parsed = ts.query.parse(lang, query) |
| 90 | + end) |
| 91 | + |
| 92 | + assert.is.truthy(parsed) |
| 93 | + assert.is_not.boolean(parsed) |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + assert(found_query, "didn't find any queries") |
| 99 | + end) |
| 100 | + end |
| 101 | +end) |
0 commit comments