Skip to content

Commit

Permalink
test: split tests into more files
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Dec 11, 2024
1 parent 7208dd6 commit 3a1017c
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 211 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ Note: support for specific languages is strictly community maintained and can br
<details>
<summary>click to expand</summary
- [x] `ada`
- [x] `apex`
- [x] `bash`
- [x] `c`
- [x] `c_sharp`
- [x] `c`
- [x] `capnp`
- [x] `clojure`
- [x] `cmake`
Expand Down Expand Up @@ -55,6 +56,7 @@ Note: support for specific languages is strictly community maintained and can br
- [x] `json`
- [x] `jsonnet`
- [x] `julia`
- [x] `kdl`
- [x] `latex`
- [x] `liquidsoap`
- [x] `lua`
Expand All @@ -63,12 +65,13 @@ Note: support for specific languages is strictly community maintained and can br
- [x] `nim`
- [x] `nix`
- [x] `norg`
- [x] `nu`
- [x] `objdump`
- [x] `ocaml_interface`
- [x] `ocaml`
- [x] `odin`
- [x] `php`
- [x] `php_only`
- [x] `php`
- [x] `prisma`
- [x] `proto`
- [x] `python`
Expand Down Expand Up @@ -99,7 +102,6 @@ Note: support for specific languages is strictly community maintained and can br
- [x] `yaml`
- [x] `yang`
- [x] `zig`
- [x] `ada`
- [ ] `agda`
- [ ] `arduino`
- [ ] `astro`
Expand Down Expand Up @@ -149,7 +151,6 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `json5`
- [ ] `jsonc`
- [ ] `jsx`
- [x] `kdl`
- [ ] `kotlin`
- [ ] `lalrpop`
- [ ] `ledger`
Expand All @@ -160,7 +161,6 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `mermaid`
- [ ] `meson`
- [ ] `nickel`
- [x] `nu`
- [ ] `ocamllex`
- [ ] `pascal`
- [ ] `perl`
Expand Down Expand Up @@ -200,8 +200,8 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `v`
- [ ] `vala`
- [ ] `vhs`
- [ ] `wgsl`
- [ ] `wgsl_bevy`
- [ ] `wgsl`
- [ ] `yuck`
</details>
Expand Down
113 changes: 113 additions & 0 deletions test/contexts_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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 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<integer, integer[]>? contexts
local function parse_directives(filename)
local f = io.open(filename, 'r')
if not f then
return
end

local context = {} --- @type table<integer,integer[]>
local contexts = {} --- @type table<integer,integer[]>

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

for _, lang in ipairs(get_langs()) do
describe('contexts (' .. lang .. '):', function()
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']])
tc_helpers.install_langs(lang)
end)

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

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<C-y>', #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
63 changes: 63 additions & 0 deletions test/helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 "<lang> has been installed".
print(' ')
]],
langs
)
end

local langs --- @type string[]?

local lang_alias = {
janet_simple = 'janet'
}

function M.get_langs()
if langs then
return langs
end

langs = {}
local f = assert(io.open('README.md', 'r'))
local readme_langs = {} --- @type table<string,true>
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
k = lang_alias[k] or k
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
return langs
end

return M
21 changes: 14 additions & 7 deletions test/lang/test.go
Original file line number Diff line number Diff line change
@@ -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}}


}
Expand All @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions test/queries_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--- Test the query for each language is valid and update the README.
local helpers = require('nvim-test.helpers')
local exec_lua = helpers.exec_lua

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()
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()
install_langs(lang)

local index --- @type integer
local line_orig --- @type string

-- Find the line in the README for this lang
for i, l in pairs(readme_lines) do
--- @type string?
local lang1 = l:match('%- %[x%] `([^`]+)`')
if lang1 == lang then
index, line_orig = i, l
l = l:gsub(' %(broken%)', '')
-- Mark as broken now, unmark later.
readme_lines[i] = l .. ' (broken)'
break
end
end

assert(index)

exec_lua([[vim.treesitter.query.get(...)]], lang, 'context')

readme_lines[index] = line_orig:gsub(' %(broken%)', '')
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)
Loading

0 comments on commit 3a1017c

Please sign in to comment.