Browse Source

more wip on tests

develop
Harald Albrecht 5 years ago
parent
commit
26efbf6354
  1. 5
      .luacheckrc
  2. 8
      multibow.code-workspace
  3. 9
      sdcard/layouts/empty.lua
  4. 25
      sdcard/layouts/shift.lua
  5. 16
      sdcard/snippets/multibow.lua
  6. 48
      spec/empty_spec.lua
  7. 8
      spec/multibow_spec.lua
  8. 112
      spec/routehandlers_spec.lua

5
.luacheckrc

@ -0,0 +1,5 @@
std = {
read_globals = {
"insl", "require"
}
}

8
multibow.code-workspace

@ -6,7 +6,11 @@
],
"settings": {
"files.associations": {
".busted": "lua"
}
".busted": "lua",
".luacheckrc": "lua"
},
"lua.format.lineWidth": 80,
"lua.luacheckPath": "luacheck",
"lua.preferLuaCheckErrors": true
}
}

9
sdcard/layouts/empty.lua

@ -23,7 +23,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
require "snippets/multibow"
local empty = {} -- module
local mb = require("snippets/multibow")
--[[
The Keybow layout is as follows when in landscape orientation, with the USB
@ -42,4 +44,7 @@ cable going off "northwards":
]]--
mb.register_keymap({name="empty"})
empty.keymap = {name="empty"}
mb.register_keymap(empty.keymap)
return empty -- module

25
sdcard/layouts/shift.lua

@ -22,7 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
require "snippets/multibow"
local shift = {} -- module
local mb = require "snippets/multibow"
--[[
The Keybow layout is as follows when in landscape orientation, with the USB
@ -44,8 +46,6 @@ SHIFT →LAYOUT 🔆BRIGHT
]]--
shift = {}
function shift.grab(key)
mb.grab("shift-shifted")
end
@ -55,28 +55,33 @@ function shift.release(key)
end
function shift.cycle(key)
print("CYCLE")
mb.cycle_primary_keymaps()
end
function shift.brightness(key)
print("BRIGHTNESS")
local b = mb.brightness + 0.3
if b > 1 then; b = 0.4; end
mb.set_brightness(b)
end
mb.register_keymap({
-- define and register keymaps
shift.keymap = {
name="shift",
permanent=true,
[11] = {c={r=1, g=1, b=1}, press=shift.grab, release=shift.release},
})
mb.register_keymap({
}
shift.keymap_shifted = {
name="shift-shifted",
secondary=true,
[11] = {c={r=1, g=1, b=1}, press=shift.grab, release=shift.release},
[8] = {c={r=0, g=1, b=1}, press=shift.cycle},
[5] = {c={r=0.5, g=0.5, b=0.5}, press=shift.brightness}
})
}
mb.register_keymap(shift.keymap)
mb.register_keymap(shift.keymap_shifted)
return shift -- module

16
sdcard/snippets/multibow.lua

@ -20,9 +20,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
mb = {} -- module
require "keybow"
mb = {}
mb.path = (...):match("^(.-)[^%/]+$")
require(mb.path .. "morekeys")
@ -79,6 +80,17 @@ function mb.register_keymap(keymap)
end
-- Returns the list of currently registered keymaps; this list is a table,
-- with its registered keymaps at indices 1, 2, ...
function mb.registered_keymaps()
local keymaps = {}
for name, keymap in pairs(mb.keymaps) do
table.insert(keymaps, keymap)
end
return keymaps
end
-- Cycles through the available (primary) keymaps, ignoring secondary and
-- permanent keymaps. This is convenient for assigning primary keymap switching
-- using a key on the Keybow device itself.
@ -195,4 +207,4 @@ function setup()
mb.activate_leds()
end
return mb
return mb -- module

48
spec/empty_spec.lua

@ -0,0 +1,48 @@
--[[
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("empty multibow keymap", function()
local mb = require("snippets/multibow")
insl(function()
it("installs a single empty primary keymap", function()
-- Sanity check that there are no registered keymaps yet.
assert.is.equal(#mb.registered_keymaps(), 0)
local empty = require("layouts/empty")
assert.is_not_nil(empty) -- we're going over the top here...
assert.is_not_nil(empty.keymap) -- ...even more so.
-- empty must register exactly one keymap, and it must be
-- a primary keymap, not permanent or secondary.
local kms = mb.registered_keymaps()
assert.is.equal(#kms, 1)
local keymap = kms[1]
assert.is_falsy(keymap.permanent)
assert.is_falsy(keymap.secondary)
end)
end)
end)

8
spec/multibow_spec.lua

@ -30,7 +30,6 @@ describe("multibow", function()
before_each(function()
require("keybow")
stub(keybow, "set_pixel")
mb = require("snippets/multibow")
end)
@ -90,4 +89,11 @@ describe("multibow", function()
end)
end)
insl(function()
it("has more keys", function()
assert.is_not_nil(keybow.F13)
assert.is.equal(keybow.F13, 0x68)
end)
end)
end)

112
spec/routehandlers_spec.lua

@ -0,0 +1,112 @@
--[[
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("routehandlers", function()
-- ensure to get a fresh multibow module instance each time we run
-- an isolated test...
local mb
local spies = mock({
prim_key_press=function(key) end,
prim_key_release=function(key) end,
prim_otherkey_press=function(key) end,
prim_otherkey_release=function(key) end,
perm_key_press=function(key) end,
perm_key_release=function(key) end,
})
local primary_keymap = {
name="test",
[0]={press=spies.prim_key_press, release=spies.prim_key_release},
[1]={press=spies.prim_otherkey_press, release=spies.prim_otherkey_release},
}
local permanent_keymap = {
name="permanent",
permanent=true,
[0]={press=spies.perm_key_press, release=spies.perm_key_release}
}
before_each(function()
require("keybow")
mb = require("snippets/multibow")
-- make sure to clear our spies
for name, schlapphut in pairs(spies) do
schlapphut:clear()
end
end)
insl(function()
it("defines all Keybow key handlers for routing", function()
for keyno = 0, 11 do
assert.is_function(_G[string.format("handle_key_%02d", keyno)])
end
end)
end)
insl(function()
it("routes key press to primary keymap", function()
assert.is_not_truthy(primary_keymap.permanent or primary_keymap.secondary)
mb.register_keymap(primary_keymap)
assert.spy(spies.prim_key_press).was_not.called() -- just a safety guard
handle_key_00(true)
assert.spy(spies.prim_key_press).was.called(1)
assert.spy(spies.prim_key_press).was.called_with(0)
assert.spy(spies.prim_key_release).was_not.called()
end)
it("routes key release to primary keymap", function()
assert.spy(spies.prim_key_press).was_not.called() -- just a safety guard
handle_key_00(false)
assert.spy(spies.prim_key_press).was_not.called()
assert.spy(spies.prim_key_release).was.called(1)
assert.spy(spies.prim_key_release).was.called_with(0)
end)
it("routes with priority key press/release to permanent key", function()
assert.is_truthy(permanent_keymap.permanent)
assert.is_not_truthy(permanent_keymap.secondary)
mb.register_keymap(permanent_keymap)
handle_key_00(true)
assert.spy(spies.perm_key_press).was.called(1)
assert.spy(spies.perm_key_press).was.called_with(0)
assert.spy(spies.perm_key_release).was_not.called()
assert.spy(spies.prim_key_press).was_not_called()
end)
it("correctly routes key press to un-overlaid primary key 2", function()
assert.spy(spies.prim_otherkey_press).was_not_called()
handle_key_01(true)
assert.spy(spies.prim_otherkey_press).was.called(1)
assert.spy(spies.prim_otherkey_press).was.called_with(1)
end)
end)
end)
Loading…
Cancel
Save