feat: add unit tests

This commit is contained in:
Aire-One 2024-11-17 18:50:52 +01:00
parent 44686975f4
commit dcee9d9553
9 changed files with 175 additions and 2 deletions

5
.busted Normal file
View File

@ -0,0 +1,5 @@
return {
_all = {
helper = "spec/helper.lua",
},
}

13
.github/workflows/test.yaml vendored Normal file
View File

@ -0,0 +1,13 @@
name: test
on:
push:
jobs:
busted:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Run Busted
uses: lunarmodules/busted@v2.2.0

View File

@ -3,6 +3,7 @@ std = "min"
cache = true
include_files = {
".busted",
".luacheckrc",
"*.rockspec",
"src/",

View File

@ -8,6 +8,7 @@
},
"stylua.targetReleaseVersion": "latest",
"files.associations": {
".busted": "lua",
".luacheckrc": "lua",
"*.rockspec": "lua",
"config.ld": "lua"

View File

@ -22,3 +22,6 @@ lint: luacheck stylua ldoc-dryrun cspell lint-rockspec
ldoc:
ldoc .
test:
luarocks test

View File

@ -1,9 +1,12 @@
rockspec_format = "3.0"
package = "awesome-slot"
version = "dev-1"
source = {
url = "git+https://github.com/Aire-One/awesome-slot.git",
}
description = {
summary = "A declarative API to connect signals for the AwesomeWM.",
detailed = "A declarative API to connect signals for the AwesomeWM.",
@ -15,6 +18,7 @@ description = {
"declarative",
},
}
build = {
type = "builtin",
modules = {
@ -29,3 +33,7 @@ build = {
"doc",
},
}
test = {
type = "busted",
}

18
spec/helper.lua Normal file
View File

@ -0,0 +1,18 @@
-- Fake awesome modules
package.loaded["gears.table"] = {
hasitem = function(t, item)
for k, v in pairs(t) do
if v == item then
return k
end
end
end,
}
package.loaded["ruled.client"] = {}
package.loaded["ruled.notification"] = {}
package.loaded["awful.keyboard"] = {
append_client_keybindings = function() end,
}

124
spec/slot_spec.lua Normal file
View File

@ -0,0 +1,124 @@
local slot = require "awesome-slot"
local function new_target()
return {
connect_signal = spy.new(function() end),
disconnect_signal = spy.new(function() end),
}
end
describe("Awesome-slot", function()
it("should create slot", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
assert.is_not_nil(s)
end)
it("should remove slot", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
slot.remove(s)
assert.is_nil(slot.get_slot(s))
end)
it("should connect slot (from constructor parameters)", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
connect = true,
}
assert.is_true(s.connected)
end)
it("should connect signal", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
slot.connect(s)
assert.is_true(s.connected)
end)
it("should disconnect slot", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
connect = true,
}
slot.disconnect(s)
assert.is_false(s.connected)
end)
it("should get slot", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
assert.is_not_nil(slot.get_slot(s))
end)
it("should get slot by id", function()
local target = new_target()
local id = "SOME_ID"
slot {
id = id,
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
assert.is_not_nil(slot.get_slot(id))
end)
it("should generate id", function()
local target = new_target()
local s = slot {
target = target,
signal = "signal",
slot = function() end,
slot_params = { key = "value" },
}
assert.is_not_nil(s.id)
end)
end)

View File

@ -186,9 +186,9 @@ function awesome_slot.disconnect(slot)
-- Please check the `:connect_slot` method to understand why we do this.
if gtable.hasitem(awesome_slot.static_connect, s.target) then
s.target.disconnect_slot(s.signal, s.callback)
s.target.disconnect_signal(s.signal, s.callback)
else
s.target:disconnect_slot(s.signal, s.callback)
s.target:disconnect_signal(s.signal, s.callback)
end
s.connected = false