summaryrefslogtreecommitdiff
path: root/test/tclient
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-01-17 21:12:15 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-01-17 21:12:15 +0800
commitb6eed1d1f301706f51fc3dcef45f64ae4400eadd (patch)
tree89942f14778c012351a066b89c4dd87ac51ebf83 /test/tclient
parentc2d2e7a92461dafab0b3446cd30db67d00eed570 (diff)
downloadlua-language-server-b6eed1d1f301706f51fc3dcef45f64ae4400eadd.zip
stash
Diffstat (limited to 'test/tclient')
-rw-r--r--test/tclient/init.lua1
-rw-r--r--test/tclient/lclient.lua147
-rw-r--r--test/tclient/tests/single-mode.lua46
3 files changed, 194 insertions, 0 deletions
diff --git a/test/tclient/init.lua b/test/tclient/init.lua
new file mode 100644
index 00000000..627aed17
--- /dev/null
+++ b/test/tclient/init.lua
@@ -0,0 +1 @@
+require 'tclient.tests.single-mode'
diff --git a/test/tclient/lclient.lua b/test/tclient/lclient.lua
new file mode 100644
index 00000000..ae876400
--- /dev/null
+++ b/test/tclient/lclient.lua
@@ -0,0 +1,147 @@
+local gc = require 'gc'
+local util = require 'utility'
+local proto = require 'proto'
+local await = require 'await'
+local timer = require 'timer'
+
+require 'provider'
+
+local counter = util.counter()
+
+---@class languageClient
+---@field _outs table
+---@field _gc gc
+---@field _waiting table
+---@field _methods table
+local mt = {}
+mt.__index = mt
+
+function mt:__close()
+ self:remove()
+end
+
+function mt:_fakeProto()
+ proto.send = function (data)
+ self._outs[#self._outs+1] = data
+ end
+end
+
+function mt:_flushServer()
+ -- reset scopes
+ local ws = require 'workspace'
+ local scope = require 'workspace.scope'
+ local files = require 'files'
+ ws.reset()
+ scope.reset()
+ files.reset()
+end
+
+---@param callback async fun(client: languageClient)
+function mt:start(callback)
+ self:_fakeProto()
+ self:_flushServer()
+
+ local finished = false
+
+ ---@async
+ await.call(function ()
+ callback(self)
+ finished = true
+ end)
+
+ while true do
+ if finished then
+ break
+ end
+ if await.step() then
+ goto CONTINUE
+ end
+ timer.update()
+ if await.step() then
+ goto CONTINUE
+ end
+ if self:update() then
+ goto CONTINUE
+ end
+ timer.timeJump(1.0)
+ ::CONTINUE::
+ end
+
+ self:remove()
+end
+
+function mt:gc(obj)
+ return self._gc:add(obj)
+end
+
+function mt:remove()
+ self._gc:remove()
+end
+
+function mt:notify(method, params)
+ proto.doMethod {
+ method = method,
+ params = params,
+ }
+end
+
+function mt:request(method, params, callback)
+ local id = counter()
+ self._waiting[id] = callback
+ proto.doMethod {
+ id = id,
+ method = method,
+ params = params,
+ }
+end
+
+---@async
+function mt:awaitRequest(method, params)
+ return await.wait(function (waker)
+ self:request(method, params, waker)
+ end)
+end
+
+function mt:update()
+ local outs = self._outs
+ if #outs == 0 then
+ return false
+ end
+ self._outs = {}
+ for _, out in ipairs(outs) do
+ if out.method then
+ local callback = self._methods[out.method]
+ if callback then
+ proto.doResponse {
+ id = out.id,
+ params = callback(out.params),
+ }
+ else
+ proto.doResponse {
+ id = out.id,
+ params = nil,
+ }
+ end
+ else
+ local callback = self._waiting[out.id]
+ self._waiting[out.id] = nil
+ callback(out.result, out.error)
+ end
+ end
+ return true
+end
+
+function mt:register(name, callback)
+ self._methods[name] = callback
+end
+
+---@return languageClient
+return function ()
+ local self = setmetatable({
+ _gc = gc(),
+ _outs = {},
+ _waiting = {},
+ _methods = {},
+ }, mt)
+ return self
+end
diff --git a/test/tclient/tests/single-mode.lua b/test/tclient/tests/single-mode.lua
new file mode 100644
index 00000000..bdf831f4
--- /dev/null
+++ b/test/tclient/tests/single-mode.lua
@@ -0,0 +1,46 @@
+local await = require 'await'
+local lclient = require 'tclient.lclient'
+local ws = require 'workspace'
+local util = require 'utility'
+
+---@async
+lclient():start(function (client)
+ client:awaitRequest('initialize', {
+ clientInfo = {
+ name = 'unit-test',
+ version = 'single-mode',
+ },
+ rootUri = nil,
+ })
+
+ client:notify('initialized')
+
+ client:notify('textDocument/didOpen', {
+ textDocument = {
+ uri = 'test://single-file.lua',
+ languageId = 'lua',
+ version = 0,
+ text = [[
+local x
+print(x)
+]]
+ }
+ })
+
+ ws.awaitReady()
+
+ local locations = client:awaitRequest('textDocument.definition', {
+ textDocument = { uri = 'test://single-file.lua' },
+ position = { line = 1, character = 7 },
+ })
+
+ assert(util.equal(locations, {
+ {
+ uri = 'test://single-file.lua',
+ range = {
+ start = { line = 0, character = 6 },
+ ['end'] = { line = 0, character = 7 },
+ }
+ }
+ }))
+end)