Skip to content

Commit

Permalink
test: add tests for virtual document change event
Browse files Browse the repository at this point in the history
  • Loading branch information
neubaner committed Nov 11, 2024
1 parent c34ab9e commit 922a248
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/rzls/virtual_document_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("virtual document", function()
vim.cmd.edit({ args = { path } })
local ls = vim.fn.getbufinfo({ buflisted = 1 })
local bufnr = ls[1].bufnr

it("create virtual document", function()
vd = virtual_document:new(bufnr, razor.language_kinds.html)
eq({
Expand All @@ -18,6 +19,9 @@ describe("virtual document", function()
content = "",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
end)

Expand All @@ -42,6 +46,9 @@ describe("virtual document", function()
content = "Hello\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
vd:update_content({
previousWasEmpty = false,
Expand All @@ -63,6 +70,9 @@ describe("virtual document", function()
content = "Hello World\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
vd:update_content({
previousWasEmpty = false,
Expand All @@ -84,6 +94,9 @@ describe("virtual document", function()
content = "Hello stuff\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
vd:update_content({
previousWasEmpty = false,
Expand All @@ -105,6 +118,9 @@ describe("virtual document", function()
content = "Hello in the middle stuff\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
vd:update_content({
previousWasEmpty = false,
Expand All @@ -126,6 +142,9 @@ describe("virtual document", function()
content = "i💩\nHello in the middle stuff\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
vd:update_content({
previousWasEmpty = false,
Expand All @@ -147,6 +166,53 @@ describe("virtual document", function()
content = "Hello in the middle stuff\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = {},
},
}, vd)
end)

it("trigger change event when a document content changes", function()
local update_handler_called = false

local function update_handler()
update_handler_called = true
end

vd.change_event:on(update_handler)

vd:update_content({
previousWasEmpty = false,
hostDocumentVersion = 7,
hostDocumentFilePath = full_path,
changes = {
{
newText = "",
span = {
start = 0,
length = 0,
},
},
},
})

eq({
buf = bufnr,
host_document_version = 7,
content = "Hello in the middle stuff\n",
kind = razor.language_kinds.html,
path = full_path,
change_event = {
listeners = { update_handler },
},
}, vd)

local co = coroutine.running()
vim.schedule(function()
coroutine.resume(co)
end)
coroutine.yield()

eq(true, update_handler_called)
end)
end)

0 comments on commit 922a248

Please sign in to comment.