fixes potential bug uncovered by coverage analysis; adds more test cases
This commit is contained in:
parent
989a9fb353
commit
9c8afbbf69
4
.luacov
4
.luacov
|
@ -26,7 +26,7 @@ return {
|
|||
runreport=true,
|
||||
deletestats=true,
|
||||
exclude={
|
||||
"./env",
|
||||
"./spec"
|
||||
"env",
|
||||
"spec"
|
||||
}
|
||||
}
|
|
@ -28,8 +28,7 @@
|
|||
"isDefault": true
|
||||
},
|
||||
"type": "shell",
|
||||
"command": "${workspaceFolder}/env.sh",
|
||||
"args": ["busted"],
|
||||
"command": "${workspaceFolder}/env.sh busted && sed -n \"/^Total/p\" ${workspaceFolder}/luacov.report.out",
|
||||
"presentation": {
|
||||
"echo": true,
|
||||
"reveal": "always",
|
||||
|
|
|
@ -66,8 +66,10 @@ function mb.register_keymap(keymap)
|
|||
local ckm = mb.current_keymap
|
||||
mb.current_keymap = mb.current_keymap or keymap
|
||||
table.insert(mb.primary_keymaps, keymap)
|
||||
if ckm and ckm.activate then
|
||||
ckm.activate()
|
||||
-- if this is the first keymap to get activated, don't forget to
|
||||
-- call its activation hook.
|
||||
if ckm == nil and keymap.activate then
|
||||
keymap.activate()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -58,7 +58,7 @@ end
|
|||
function mb.led(keyno, color)
|
||||
if color then
|
||||
local b = mb.brightness * 255
|
||||
keybow.set_pixel(keyno, color.r * b, color.g * b, color.b * b)
|
||||
keybow.set_pixel(keyno, math.floor(color.r * b), math.floor(color.g * b), math.floor(color.b * b))
|
||||
else
|
||||
keybow.set_pixel(keyno, 0, 0, 0)
|
||||
end
|
||||
|
|
|
@ -0,0 +1,150 @@
|
|||
--[[
|
||||
Copyright 2019 Harald Albrecht
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
]]--
|
||||
|
||||
require "mocked-keybow"
|
||||
|
||||
describe("multibow", function()
|
||||
|
||||
-- ensure to get a fresh multibow module instance each time we run
|
||||
-- an isolated, nope, insulated test...
|
||||
local mb
|
||||
|
||||
before_each(function()
|
||||
require("keybow")
|
||||
mb = require("snippets/multibow")
|
||||
end)
|
||||
|
||||
inslit("adds permanent keyboard layout, but doesn't activate it", function()
|
||||
local permkm = {
|
||||
name="permanent",
|
||||
permanent=true
|
||||
}
|
||||
assert.is_nil(mb.keymaps["permanent"])
|
||||
mb.register_keymap(permkm)
|
||||
assert.is.equal(mb.keymaps["permanent"], permkm)
|
||||
assert.is_nil(mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("checks multibow module is fresh again", function()
|
||||
assert.is_nil(mb.keymaps["permanent"])
|
||||
end)
|
||||
|
||||
inslit("adds permanent, then two primary layouts, activates only first primary layout", function()
|
||||
local permkm = {
|
||||
name="permanent",
|
||||
permanent=true
|
||||
}
|
||||
mb.register_keymap(permkm)
|
||||
local prim1km = { name="bavaria-one" }
|
||||
local prim2km = { name="primary-two" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
assert.is.equal(prim1km, mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("sequence of primary keymaps is in registration order", function()
|
||||
local prim1km = { name="last" }
|
||||
local prim2km = { name="first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
end)
|
||||
|
||||
inslit("adds secondary, then primary layout, activates only primary layout", function()
|
||||
local primkm = { name="berlin" }
|
||||
local seckm = { name="munich", secondary=true }
|
||||
mb.register_keymap(seckm)
|
||||
mb.register_keymap(primkm)
|
||||
assert.is.equal(primkm, mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("cycles primary keymaps based on primary-secondary names substring match", function()
|
||||
-- on purpose, the names of the primary keymaps are in reverse lexical order,
|
||||
-- to make sure that cycling follows the registration order, but not the
|
||||
-- name order.
|
||||
local prim1km = { name= "last" }
|
||||
local sec1km = { name="last-shift", secondary=true }
|
||||
local sec2km = { name="xlast-shift", secondary=true}
|
||||
local prim2km = { name= "first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
mb.register_keymap(sec1km)
|
||||
mb.register_keymap(sec2km)
|
||||
assert.is.equal(4, #mb.registered_keymaps())
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
|
||||
-- cycles from secondary to next primary
|
||||
mb.activate_keymap(sec1km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim2km.name, mb.current_keymap.name)
|
||||
-- cycles from last primary to first primary
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim1km.name, mb.current_keymap.name)
|
||||
-- cannot cycle from misnamed secondary without shift_to
|
||||
mb.activate_keymap(sec2km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(sec2km.name, mb.current_keymap.name)
|
||||
end)
|
||||
|
||||
inslit("cycles primary keymaps based on shift_to", function()
|
||||
-- on purpose, the names of the primary keymaps are in reverse lexical order,
|
||||
-- to make sure that cycling follows the registration order, but not the
|
||||
-- name order.
|
||||
local prim1km = { name="last", shift_to=nil }
|
||||
local sec1km = { name="last-shift", secondary=true, shift_to=nil }
|
||||
prim1km.shift_to = sec1km
|
||||
local prim2km = { name= "first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
mb.register_keymap(sec1km)
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
|
||||
-- cycles from secondary to next primary
|
||||
mb.activate_keymap(sec1km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim2km.name, mb.current_keymap.name)
|
||||
-- cycles from last primary to first primary
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim1km.name, mb.current_keymap.name)
|
||||
end)
|
||||
|
||||
inslit("exits broken secondary keymap chain", function()
|
||||
local pkm = { name="test", shift_to=nil }
|
||||
local skm1 = { name="test-shift1", secondary=true }
|
||||
local skm2 = { name="test-shift2", secondary=true, shift_to=nil }
|
||||
skm1.shift_to = skm2
|
||||
mb.register_keymap(pkm)
|
||||
mb.register_keymap(skm1)
|
||||
mb.register_keymap(skm2)
|
||||
mb.activate_keymap(skm1)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(skm1.name, mb.current_keymap.name)
|
||||
end)
|
||||
|
||||
inslit("calls activation handler for first registered primary keymap", function()
|
||||
local pkm = { name="test", activate=function() end}
|
||||
local s = spy.on(pkm, "activate")
|
||||
mb.register_keymap(pkm)
|
||||
assert.spy(s).is.called(1)
|
||||
end)
|
||||
|
||||
end)
|
|
@ -67,4 +67,17 @@ describe("multibow LEDs", function()
|
|||
assert.spy(s).was.called_with(1, {r=1, g=1, b=1})
|
||||
end)
|
||||
|
||||
inslit("sets keybow LED", function()
|
||||
local s = spy.on(keybow, "set_pixel")
|
||||
|
||||
mb.set_brightness(0.5)
|
||||
mb.led(0, {r=0, g=1, b=0})
|
||||
assert.spy(s).was.called(1)
|
||||
assert.spy(s).was.called_with(0, 0, 127, 0)
|
||||
|
||||
s:clear()
|
||||
mb.led(0)
|
||||
assert.spy(s).was.called_with(0, 0, 0, 0)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
|
|
@ -33,100 +33,6 @@ describe("multibow", function()
|
|||
mb = require("snippets/multibow")
|
||||
end)
|
||||
|
||||
inslit("adds permanent keyboard layout, but doesn't activate it", function()
|
||||
local permkm = {
|
||||
name="permanent",
|
||||
permanent=true
|
||||
}
|
||||
assert.is_nil(mb.keymaps["permanent"])
|
||||
mb.register_keymap(permkm)
|
||||
assert.is.equal(mb.keymaps["permanent"], permkm)
|
||||
assert.is_nil(mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("checks multibow module is fresh again", function()
|
||||
assert.is_nil(mb.keymaps["permanent"])
|
||||
end)
|
||||
|
||||
inslit("adds permanent, then two primary layouts, activates only first primary layout", function()
|
||||
local permkm = {
|
||||
name="permanent",
|
||||
permanent=true
|
||||
}
|
||||
mb.register_keymap(permkm)
|
||||
local prim1km = { name="bavaria-one" }
|
||||
local prim2km = { name="primary-two" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
assert.is.equal(prim1km, mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("sequence of primary keymaps is in registration order", function()
|
||||
local prim1km = { name="last" }
|
||||
local prim2km = { name="first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
end)
|
||||
|
||||
inslit("adds secondary, then primary layout, activates only primary layout", function()
|
||||
local primkm = { name="berlin" }
|
||||
local seckm = { name="munich", secondary=true }
|
||||
mb.register_keymap(seckm)
|
||||
mb.register_keymap(primkm)
|
||||
assert.is.equal(primkm, mb.current_keymap)
|
||||
end)
|
||||
|
||||
inslit("cycles primary keymaps based on primary-secondary names substring match", function()
|
||||
-- on purpose, the names of the primary keymaps are in reverse lexical order,
|
||||
-- to make sure that cycling follows the registration order, but not the
|
||||
-- name order.
|
||||
local prim1km = { name= "last" }
|
||||
local sec1km = { name="last-shift", secondary=true }
|
||||
local sec2km = { name="xlast-shift", secondary=true}
|
||||
local prim2km = { name= "first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
mb.register_keymap(sec1km)
|
||||
mb.register_keymap(sec2km)
|
||||
assert.is.equal(4, #mb.registered_keymaps())
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
|
||||
-- cycles from secondary to next primary
|
||||
mb.activate_keymap(sec1km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim2km.name, mb.current_keymap.name)
|
||||
-- cycles from last primary to first primary
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim1km.name, mb.current_keymap.name)
|
||||
-- cannot cycle from misnamed secondary without shift_to
|
||||
mb.activate_keymap(sec2km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(sec2km.name, mb.current_keymap.name)
|
||||
end)
|
||||
|
||||
inslit("cycles primary keymaps based on shift_to", function()
|
||||
-- on purpose, the names of the primary keymaps are in reverse lexical order,
|
||||
-- to make sure that cycling follows the registration order, but not the
|
||||
-- name order.
|
||||
local prim1km = { name= "last", shift_to=nil }
|
||||
local sec1km = { name="last-shift", secondary=true, shift_to=nil }
|
||||
prim1km.shift_to = sec1km
|
||||
local prim2km = { name= "first" }
|
||||
mb.register_keymap(prim1km)
|
||||
mb.register_keymap(prim2km)
|
||||
mb.register_keymap(sec1km)
|
||||
assert.is.same(mb.registered_primary_keymaps(), {prim1km, prim2km})
|
||||
|
||||
-- cycles from secondary to next primary
|
||||
mb.activate_keymap(sec1km.name)
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim2km.name, mb.current_keymap.name)
|
||||
-- cycles from last primary to first primary
|
||||
mb.cycle_primary_keymaps()
|
||||
assert.is.equal(prim1km.name, mb.current_keymap.name)
|
||||
end)
|
||||
|
||||
inslit("sets up multibow, activates lights", function()
|
||||
local s = spy.on(_G, "setup")
|
||||
local al = spy.on(mb, "activate_leds")
|
||||
|
|
|
@ -74,4 +74,11 @@ describe("prioqueue", function()
|
|||
assert.is.same({300, "zoo"}, {p, v})
|
||||
end)
|
||||
|
||||
it("removes nothing", function()
|
||||
local q = pq.new()
|
||||
local p, v = q:remove()
|
||||
assert.is.Nil(p)
|
||||
assert.is.Nil(v)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
|
Loading…
Reference in New Issue