experimental: renderer
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 10s

This commit is contained in:
2025-02-15 08:26:05 -07:00
parent de01a95cdc
commit 193933045e
7 changed files with 761 additions and 3 deletions

59
spec/renderer_spec.lua Normal file
View File

@@ -0,0 +1,59 @@
local Renderer = require 'u.renderer'
--- @param markup string
local function parse(markup)
-- call private method:
return (Renderer --[[@as any]])._parse_markup(markup)
end
describe('Renderer', function()
it('_parse_markup: empty string', function()
local nodes = parse [[]]
assert.are.same({}, nodes)
end)
it('_parse_markup: only string', function()
local nodes = parse [[The quick brown fox jumps over the lazy dog.]]
assert.are.same({
{ kind = 'text', value = 'The quick brown fox jumps over the lazy dog.' },
}, nodes)
end)
it('_parse_markup: <', function()
local nodes = parse [[<t value="bleh" />]]
assert.are.same({
{ kind = 'text', value = '<t value="bleh" />' },
}, nodes)
end)
it('_parse_markup: empty tag', function()
local nodes = parse [[</>]]
assert.are.same({ { kind = 'tag', name = '', attributes = {} } }, nodes)
end)
it('_parse_markup: tag', function()
local nodes = parse [[<t value="Hello" />]]
assert.are.same({
{
kind = 'tag',
name = 't',
attributes = {
value = 'Hello',
},
},
}, nodes)
end)
it('_parse_markup: attributes with quotes', function()
local nodes = parse [[<t value="Hello \"there\"" />]]
assert.are.same({
{
kind = 'tag',
name = 't',
attributes = {
value = 'Hello "there"',
},
},
}, nodes)
end)
end)

70
spec/utils_spec.lua Normal file
View File

@@ -0,0 +1,70 @@
local utils = require 'u.utils'
--- @param s string
local function split(s) return vim.split(s, '') end
--- @param original string
--- @param changes LevenshteinChange[]
local function morph(original, changes)
local t = split(original)
for _, change in ipairs(changes) do
if change.kind == 'add' then
table.insert(t, change.index, change.item)
elseif change.kind == 'delete' then
table.remove(t, change.index)
elseif change.kind == 'change' then
t[change.index] = change.to
end
end
return vim.iter(t):join ''
end
describe('utils', function()
it('levenshtein', function()
local original = 'abc'
local result = 'absece'
local changes = utils.levenshtein(split(original), split(result))
assert.are.same(changes, {
{
item = 'e',
kind = 'add',
index = 4,
},
{
item = 'e',
kind = 'add',
index = 3,
},
{
item = 's',
kind = 'add',
index = 3,
},
})
assert.are.same(morph(original, changes), result)
original = 'jonathan'
result = 'ajoanthan'
changes = utils.levenshtein(split(original), split(result))
assert.are.same(changes, {
{
from = 'a',
index = 4,
kind = 'change',
to = 'n',
},
{
from = 'n',
index = 3,
kind = 'change',
to = 'a',
},
{
index = 1,
item = 'a',
kind = 'add',
},
})
assert.are.same(morph(original, changes), result)
end)
end)