repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/tests/bugabinga/treesitter_spec.lua
Rawlocal ignored = require('std.ignored')
local treesitter = require('bugabinga.treesitter')
local function deep_copy(t)
local copy = {}
for k, v in pairs(t) do
copy[k] = v
end
return copy
end
local function table_eq(a, b)
if type(a) ~= 'table' or type(b) ~= 'table' then return a == b end
for k, v in pairs(a) do
if not table_eq(v, b[k]) then return false end
end
for k, v in pairs(b) do
if not table_eq(v, a[k]) then return false end
end
return true
end
describe('is_ignored', function()
local original_filetypes
local original_filetypes_kv
local original_filetypes_patterns
before_each(function()
original_filetypes = deep_copy(ignored.filetypes)
original_filetypes_kv = deep_copy(ignored.filetypes_kv)
if ignored.filetypes_patterns then
original_filetypes_patterns = deep_copy(ignored.filetypes_patterns)
end
end)
after_each(function()
ignored.filetypes = original_filetypes
ignored.filetypes_kv = original_filetypes_kv
ignored.filetypes_patterns = original_filetypes_patterns
end)
describe('correctness', function()
local function expect_ignored(match, expected)
local result = treesitter.is_ignored(match)
assert.is.equal(expected, result,
string.format('is_ignored(%q) = %s, expected %s', match, tostring(result), tostring(expected)))
end
it('returns true for exact match "help"', function()
expect_ignored('help', true)
end)
it('returns true for exact match "oil"', function()
expect_ignored('oil', true)
end)
it('returns true for exact match "lazy"', function()
expect_ignored('lazy', true)
end)
it('returns true for pattern match "gitcommit" (matches ^git.*)', function()
expect_ignored('gitcommit', true)
end)
it('returns true for pattern match "git" (matches ^git.*)', function()
expect_ignored('git', true)
end)
it('returns false for non-ignored filetype "lua"', function()
expect_ignored('lua', false)
end)
it('returns false for non-ignored filetype "rust"', function()
expect_ignored('rust', false)
end)
it('returns false for non-ignored filetype "python"', function()
expect_ignored('python', false)
end)
it('returns false for "text" (caller handles it separately)', function()
expect_ignored('text', false)
end)
it('returns false for empty string', function()
expect_ignored('', false)
end)
end)
describe('benchmark results (informational only)', function()
local warmup = 10000
local iterations = 100000
local function bench(name, fn, warmup, iterations)
for _ = 1, warmup do fn() end
local sink = false
local start = vim.uv.hrtime()
for _ = 1, iterations do
sink = sink or fn()
end
local elapsed = vim.uv.hrtime() - start
local ns_op = elapsed / iterations
print(string.format('\n %s: %.1f ns/op', name, ns_op))
return ns_op
end
it('informational: exact match "help" — kv short-circuits before pattern loop', function()
bench('linear', function()
for _, v in ipairs(ignored.filetypes) do
if v == 'help' or ('help'):match(v) then return true end
end
return false
end, warmup, iterations)
bench('optimized (kv+patterns)', function()
if ignored.filetypes_kv['help'] then return true end
for _, v in ipairs(ignored.filetypes_patterns) do
if ('help'):match(v) then return true end
end
return false
end, warmup, iterations)
end)
it('informational: pattern match "gitcommit" — kv misses, falls back to patterns', function()
bench('linear', function()
for _, v in ipairs(ignored.filetypes) do
if v == 'gitcommit' or ('gitcommit'):match(v) then return true end
end
return false
end, warmup, iterations)
bench('optimized (kv+patterns)', function()
if ignored.filetypes_kv['gitcommit'] then return true end
for _, v in ipairs(ignored.filetypes_patterns) do
if ('gitcommit'):match(v) then return true end
end
return false
end, warmup, iterations)
end)
it('informational: no match "lua" — full scan in both, but kv is O(1)', function()
bench('linear', function()
for _, v in ipairs(ignored.filetypes) do
if v == 'lua' or ('lua'):match(v) then return true end
end
return false
end, warmup, iterations)
bench('optimized (kv+patterns)', function()
if ignored.filetypes_kv['lua'] then return true end
for _, v in ipairs(ignored.filetypes_patterns) do
if ('lua'):match(v) then return true end
end
return false
end, warmup, iterations)
end)
end)
end)