Skip to content

Commit

Permalink
feat: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aire-One committed Nov 17, 2024
1 parent 4468697 commit dcee9d9
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
_all = {
helper = "spec/helper.lua",
},
}
13 changes: 13 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ std = "min"
cache = true

include_files = {
".busted",
".luacheckrc",
"*.rockspec",
"src/",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"stylua.targetReleaseVersion": "latest",
"files.associations": {
".busted": "lua",
".luacheckrc": "lua",
"*.rockspec": "lua",
"config.ld": "lua"
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ lint: luacheck stylua ldoc-dryrun cspell lint-rockspec

ldoc:
ldoc .

test:
luarocks test
8 changes: 8 additions & 0 deletions awesome-slot-dev-1.rockspec
Original file line number Diff line number Diff line change
@@ -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.",
Expand All @@ -15,6 +18,7 @@ description = {
"declarative",
},
}

build = {
type = "builtin",
modules = {
Expand All @@ -29,3 +33,7 @@ build = {
"doc",
},
}

test = {
type = "busted",
}
18 changes: 18 additions & 0 deletions spec/helper.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 124 additions & 0 deletions spec/slot_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions src/awesome-slot/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dcee9d9

Please sign in to comment.