diff --git a/.busted b/.busted new file mode 100644 index 0000000..133edb7 --- /dev/null +++ b/.busted @@ -0,0 +1,5 @@ +return { + default = { + verbose = true, + }, +} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3193e0b..aa37578 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,3 +34,11 @@ jobs: else echo "Rockspec unchanged, nothing to do" fi + + unit_tests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Run Busted + uses: lunarmodules/busted@v0 diff --git a/init.lua b/init.lua index a5372f1..7989674 100644 --- a/init.lua +++ b/init.lua @@ -8,8 +8,13 @@ --]] +local requirePrefix= tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix=requirePrefix.."." +end + return { - layout = require(tostring(...):match(".*lain") .. ".layout"), - util = require(tostring(...):match(".*lain") .. ".util"), - widget = require(tostring(...):match(".*lain") .. ".widget") + layout = require(requirePrefix .. "layout"), + util = require(requirePrefix .. "util"), + widget = require(requirePrefix .. "widget") } diff --git a/layout/init.lua b/layout/init.lua index 99e1efb..5bd4c2d 100644 --- a/layout/init.lua +++ b/layout/init.lua @@ -11,9 +11,14 @@ --]] -local wrequire = require(tostring(...):match(".*lain") .. ".helpers").wrequire +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local wrequire = require(requirePrefix .. "helpers").wrequire local setmetatable = setmetatable -local layout = { _NAME = tostring(...):match(".*lain") .. ".layout" } +local layout = { _NAME = requirePrefix .. "layout" } return setmetatable(layout, { __index = wrequire }) diff --git a/spec/data/lines_from.txt b/spec/data/lines_from.txt new file mode 100644 index 0000000..f87e9a8 --- /dev/null +++ b/spec/data/lines_from.txt @@ -0,0 +1,4 @@ +line1 +line2 +line3 +line4 \ No newline at end of file diff --git a/spec/helpers_spec.lua b/spec/helpers_spec.lua new file mode 100644 index 0000000..21833b0 --- /dev/null +++ b/spec/helpers_spec.lua @@ -0,0 +1,138 @@ +describe("Test helpers functions", function() + package.loaded["awful"] = {} + package.loaded["awful.spawn"] = { + easy_async=function (cmd,f) + f(cmd,0,0,1) + end, + easy_async_with_shell=function (cmd,f) + f(cmd,0,0,1) + end, + with_line_callback=function (cmd,f) + f.stdout(cmd) + end + + } + local timer ={ + start= function () end, + connect_signal= function (t,f) end, + emit_signal= function (t) end, + } + local timerMock=mock(timer,true) + package.loaded["gears.timer"] =function() return timerMock end + + + it("file_exists", function() + local helpers = require("helpers") + assert.is_true(helpers.file_exists("init.lua")) + assert.is_false(helpers.file_exists("init2.lua")) + end) + it("lines_from", function() + local result = { + "line1", + "line2", + "line3", + "line4", + } + local helpers = require("helpers") + assert.are.same(helpers.lines_from("spec/data/lines_from.txt"),result) + end) + it("lines_match", function() + local result = { + "line1", + "line3", + } + local helpers = require("helpers") + assert.are.same(helpers.lines_match("line[1,3]","spec/data/lines_from.txt"),result) + end) + it("first_line", function() + local helpers = require("helpers") + assert.are.same(helpers.first_line("spec/data/lines_from.txt"),"line1") + assert.are.equals(helpers.first_line("spec/data/no_file.txt"),nil) + end) + it("first_nonempty_line", function() + local helpers = require("helpers") + assert.are.same(helpers.first_nonempty_line("spec/data/lines_from.txt"),"line1") + end) + + + it("newtimer", function() + local helpers = require("helpers") + + assert.is_false(helpers.newtimer("name",10,"fun",false,false)) + assert.stub(timer.start).was.called_with(timerMock) + assert.stub(timer.connect_signal).was.called_with(timerMock,"timeout", "fun") + assert.stub(timer.emit_signal).was.called_with(timerMock,"timeout") + assert.are.equals(helpers.newtimer("name",10,"fun",true,true),timerMock) + end) + + it("async", function() + local helpers = require("helpers") + helpers.async("date",function (out,code) + assert.is.truthy(#out) + assert.is.truthy(code) + end) + end) + it("async_with_shell", function() + local helpers = require("helpers") + helpers.async_with_shell("date",function (out,code) + assert.is.truthy(#out) + assert.is.truthy(code) + end) + end) + it("line_callback", function() + local helpers = require("helpers") + helpers.line_callback("date",function (out) + assert.is.truthy(#out) + end) + end) + + it("map_table", function() + local helpers = require("helpers") + helpers.set_map("key","value") + assert.are.equals(helpers.get_map("key"),"value") + end) + + it("element_in_table", function() + local helpers = require("helpers") + local table={ + a=1, + b=2, + c=3, + } + assert.is_true(helpers.element_in_table(1,table)) + assert.is_false(helpers.element_in_table(5,table)) + end) + + it("spairs", function() + local helpers = require("helpers") + local table={ + a=1, + c=3, + b=2, + } + local f=helpers.spairs(table) + key,value=f() + assert.are.equals(key,"a") + assert.are.equals(value,1) + key,value=f() + assert.are.equals(key,"b") + assert.are.equals(value,2) + key,value=f() + assert.are.equals(key,"c") + assert.are.equals(value,3) + end) + + it("trivial_partition_set", function() + local helpers = require("helpers") + local table={"a","b","c"} + local result={{"a"}, {"b"}, {"c"}} + assert.are.same(helpers.trivial_partition_set(table),result) + end) + + it("powerset", function() + local helpers = require("helpers") + local table={"a","b","c"} + local result={{},{"a"}, {"b"},{"b","a"}, {"c"},{"c","a"},{"c","b"},{"c","b","a"}} + assert.are.same(helpers.powerset(table),result) + end) +end) diff --git a/spec/init_spec.lua b/spec/init_spec.lua new file mode 100644 index 0000000..8f8a334 --- /dev/null +++ b/spec/init_spec.lua @@ -0,0 +1,17 @@ +describe("Test the requires", function() + package.loaded["awful"] = {} + package.loaded["awful.spawn"] = {} + package.loaded["gears.timer"] = {} + it("init", function() + require("init") + end) + it("layout", function() + require("layout") + end) + it("util", function() + require("util") + end) + it("widget", function() + require("widget") + end) +end) diff --git a/util/init.lua b/util/init.lua index e2b7e81..da97f81 100644 --- a/util/init.lua +++ b/util/init.lua @@ -11,17 +11,22 @@ --]] +local requirePrefix= tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix=requirePrefix.."." +end + local awful = require("awful") local sqrt = math.sqrt local pairs = pairs local client = client local tonumber = tonumber -local wrequire = require(tostring(...):match(".*lain") .. ".helpers").wrequire +local wrequire = require(requirePrefix .. "helpers").wrequire local setmetatable = setmetatable -- Lain utilities submodule -- lain.util -local util = { _NAME = tostring(...):match(".*lain") .. ".util" } +local util = { _NAME = requirePrefix .. "util" } -- Like awful.menu.clients, but only show clients of currently selected tags function util.menu_clients_current_tags(menu, args) diff --git a/util/menu_iterator.lua b/util/menu_iterator.lua index 8f4c397..fd20e86 100644 --- a/util/menu_iterator.lua +++ b/util/menu_iterator.lua @@ -10,8 +10,13 @@ -- Menu iterator with Naughty notifications -- lain.util.menu_iterator +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + local naughty = require("naughty") -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local helpers = require(requirePrefix .. "helpers") local atable = require("awful.util").table local assert = assert local pairs = pairs diff --git a/widget/alsa.lua b/widget/alsa.lua index b88fec7..faf29d7 100644 --- a/widget/alsa.lua +++ b/widget/alsa.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local shell = require("awful.util").shell local wibox = require("wibox") local string = string diff --git a/widget/alsabar.lua b/widget/alsabar.lua index 2db3369..999b572 100644 --- a/widget/alsabar.lua +++ b/widget/alsabar.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local awful = require("awful") local naughty = require("naughty") local wibox = require("wibox") diff --git a/widget/bat.lua b/widget/bat.lua index ba2f312..92a5dbf 100644 --- a/widget/bat.lua +++ b/widget/bat.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local fs = require("gears.filesystem") local naughty = require("naughty") local wibox = require("wibox") diff --git a/widget/cal.lua b/widget/cal.lua index 7e33752..f4a40ed 100644 --- a/widget/cal.lua +++ b/widget/cal.lua @@ -5,8 +5,13 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") -local markup = require(tostring(...):match(".*lain") .. ".util.markup") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") +local markup = require(requirePrefix .. "util.markup") local awful = require("awful") local naughty = require("naughty") local floor = math.floor diff --git a/widget/contrib/init.lua b/widget/contrib/init.lua index 4338690..1b308c9 100644 --- a/widget/contrib/init.lua +++ b/widget/contrib/init.lua @@ -10,9 +10,14 @@ --]] -local wrequire = require(tostring(...):match(".*lain") .. ".helpers").wrequire +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local wrequire = require(requirePrefix .. "helpers").wrequire local setmetatable = setmetatable -local widget = { _NAME = tostring(...):match(".*lain") .. ".widget.contrib" } +local widget = { _NAME = requirePrefix .. "widget.contrib" } return setmetatable(widget, { __index = wrequire }) diff --git a/widget/contrib/moc.lua b/widget/contrib/moc.lua index 2b9a78a..2d7939d 100644 --- a/widget/contrib/moc.lua +++ b/widget/contrib/moc.lua @@ -5,7 +5,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local shell = require("awful.util").shell local focused = require("awful.screen").focused local escape_f = require("awful.util").escape diff --git a/widget/contrib/redshift.lua b/widget/contrib/redshift.lua index eff9152..c27e92e 100644 --- a/widget/contrib/redshift.lua +++ b/widget/contrib/redshift.lua @@ -6,7 +6,12 @@ --]] -local async = require(tostring(...):match(".*lain") .. ".helpers").async +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local async = require(requirePrefix .. ".helpers").async local awful = require("awful") local execute = os.execute local type = type diff --git a/widget/contrib/task.lua b/widget/contrib/task.lua index a14e7df..51ac5bd 100644 --- a/widget/contrib/task.lua +++ b/widget/contrib/task.lua @@ -5,8 +5,13 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") -local markup = require(tostring(...):match(".*lain") .. ".util").markup +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") +local markup = require(requirePrefix .. "util").markup local awful = require("awful") local naughty = require("naughty") local mouse = mouse diff --git a/widget/contrib/tp_smapi.lua b/widget/contrib/tp_smapi.lua index cd1faea..c7a007e 100644 --- a/widget/contrib/tp_smapi.lua +++ b/widget/contrib/tp_smapi.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local focused = require("awful.screen").focused local naughty = require("naughty") local wibox = require("wibox") diff --git a/widget/cpu.lua b/widget/cpu.lua index 929b84b..5ba124d 100644 --- a/widget/cpu.lua +++ b/widget/cpu.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local wibox = require("wibox") local math = math local string = string diff --git a/widget/fs.lua b/widget/fs.lua index 160cc4a..1b9cb9d 100644 --- a/widget/fs.lua +++ b/widget/fs.lua @@ -7,7 +7,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local Gio = require("lgi").Gio local focused = require("awful.screen").focused local wibox = require("wibox") diff --git a/widget/imap.lua b/widget/imap.lua index f162178..00bb7d5 100644 --- a/widget/imap.lua +++ b/widget/imap.lua @@ -5,7 +5,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local naughty = require("naughty") local wibox = require("wibox") local awful = require("awful") diff --git a/widget/init.lua b/widget/init.lua index cc62e68..065d81c 100644 --- a/widget/init.lua +++ b/widget/init.lua @@ -11,9 +11,14 @@ --]] -local wrequire = require(tostring(...):match(".*lain") .. ".helpers").wrequire +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local wrequire = require(requirePrefix .. "helpers").wrequire local setmetatable = setmetatable -local widget = { _NAME = tostring(...):match(".*lain") .. ".widget" } +local widget = { _NAME = requirePrefix .. "widget" } return setmetatable(widget, { __index = wrequire }) diff --git a/widget/mem.lua b/widget/mem.lua index 5851eaf..3ba16c6 100644 --- a/widget/mem.lua +++ b/widget/mem.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local wibox = require("wibox") local gmatch, lines, floor = string.gmatch, io.lines, math.floor diff --git a/widget/mpd.lua b/widget/mpd.lua index 3a39edd..9bb59c5 100644 --- a/widget/mpd.lua +++ b/widget/mpd.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local shell = require("awful.util").shell local escape_f = require("awful.util").escape local focused = require("awful.screen").focused diff --git a/widget/net.lua b/widget/net.lua index 66a7d73..a3723c3 100644 --- a/widget/net.lua +++ b/widget/net.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local naughty = require("naughty") local wibox = require("wibox") local string = string diff --git a/widget/pulse.lua b/widget/pulse.lua index 167641b..e8bb0fc 100644 --- a/widget/pulse.lua +++ b/widget/pulse.lua @@ -5,7 +5,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local shell = require("awful.util").shell local wibox = require("wibox") local string = string diff --git a/widget/pulsebar.lua b/widget/pulsebar.lua index 955f193..8a63d22 100644 --- a/widget/pulsebar.lua +++ b/widget/pulsebar.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local awful = require("awful") local naughty = require("naughty") local wibox = require("wibox") diff --git a/widget/sysload.lua b/widget/sysload.lua index aaa101f..be1e1b5 100644 --- a/widget/sysload.lua +++ b/widget/sysload.lua @@ -6,7 +6,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local wibox = require("wibox") local open, match = io.open, string.match diff --git a/widget/temp.lua b/widget/temp.lua index d478bd4..37421e7 100644 --- a/widget/temp.lua +++ b/widget/temp.lua @@ -5,7 +5,12 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") local wibox = require("wibox") local tonumber = tonumber diff --git a/widget/weather.lua b/widget/weather.lua index 9626afb..1d95835 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -5,8 +5,13 @@ --]] -local helpers = require(tostring(...):match(".*lain") .. ".helpers") -local json = require(tostring(...):match(".*lain") .. ".util").dkjson +local requirePrefix = tostring(...):match(".*lain") or "" +if requirePrefix then + requirePrefix = requirePrefix .. "." +end + +local helpers = require(requirePrefix .. "helpers") +local json = require(requirePrefix .. "util").dkjson local focused = require("awful.screen").focused local naughty = require("naughty") local wibox = require("wibox")