Skip to content

Commit

Permalink
test: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Oct 1, 2023
1 parent 88be2b4 commit badaef0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 242 deletions.
1 change: 1 addition & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lua",
"$VIMRUNTIME",
"${3rd}/busted/library",
"${3rd}/luassert/library",
"${3rd}/luv/library"
],
"checkThirdParty": false
Expand Down
30 changes: 18 additions & 12 deletions test/client/session.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local uv = require('luv')
local uv = vim.loop
local MsgpackRpcStream = require('test.client.msgpack_rpc_stream')

--- @class Session
--- @class NvimSession
--- @field private _msgpack_rpc_stream MsgpackRpcStream
--- @field private _pending_messages string[]
--- @field private _prepare uv_prepare_t
--- @field private _timer uv_timer_t
--- @field private _prepare uv.uv_prepare_t
--- @field private _timer uv.uv_timer_t
--- @field private _is_running boolean
local Session = {}
Session.__index = Session
Expand Down Expand Up @@ -57,7 +57,17 @@ function Session.new(stream)
}, Session)
end

--- @param timeout integer
--- @return string
function Session:next_message(timeout)
if self._is_running then
error('Event loop already running')
end

if #self._pending_messages > 0 then
return table.remove(self._pending_messages, 1)
end

local function on_request(method, args, response)
table.insert(self._pending_messages, {'request', method, args, response})
uv.stop()
Expand All @@ -68,14 +78,6 @@ function Session:next_message(timeout)
uv.stop()
end

if self._is_running then
error('Event loop already running')
end

if #self._pending_messages > 0 then
return table.remove(self._pending_messages, 1)
end

self:_run(on_request, on_notification, timeout)
return table.remove(self._pending_messages, 1)
end
Expand All @@ -100,6 +102,10 @@ function Session:request(method, ...)
return true, result
end

---@param request_cb fun()?
---@param notification_cb fun()?
---@param setup_cb fun()?
---@param timeout integer?
function Session:run(request_cb, notification_cb, setup_cb, timeout)
local function on_request(method, args, response)
coroutine_exec(request_cb, method, args, function(status, result, flag)
Expand Down
4 changes: 2 additions & 2 deletions test/gitsigns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ local eq = helpers.eq
helpers.env()

describe('gitsigns', function()
local screen
local config
local screen --- @type NvimScreen
local config --- @type table

before_each(function()
clear()
Expand Down
Loading

0 comments on commit badaef0

Please sign in to comment.