Browse Source

wip test refactoring

develop
Harald Albrecht 5 years ago
parent
commit
f565f5815f
  1. 1
      .busted
  2. 2
      mock/mocked-keybow.lua
  3. 24
      mock/mockkeybow.lua
  4. 58
      sdcard/layouts/vsc-golang.lua
  5. 31
      spec/hwkeys.lua
  6. 24
      spec/hwkeys_spec.lua
  7. 66
      spec/integration_spec.lua
  8. 0
      spec/layouts/empty_spec.lua
  9. 4
      spec/layouts/kdenlive_spec.lua
  10. 28
      spec/layouts/shift_spec.lua
  11. 0
      spec/snippets/multibow_spec.lua
  12. 26
      spec/snippets/routehandlers_spec.lua
  13. 48
      test.lua

1
.busted

@ -33,6 +33,7 @@ return {
e = "INSL = '[⛔]';"
.. "_BUSTED = require('busted');"
.. "function insl(f) _BUSTED.insulate(_INSL, f) end;"
.. "function inslit(d, f) _BUSTED.insulate(_INSL, function() _BUSTED.it(d, f) end) end;"
,
verbose = true
}

2
mock/mocked-keybow.lua

@ -11,3 +11,5 @@ busted.stub(keybow, "tap_key")
busted.stub(keybow, "sleep") -- FIXME
busted.stub(keybow, "usleep") -- FIXME
return keybow

24
mock/mockkeybow.lua

@ -1,24 +0,0 @@
require "keybow"
function keybow.set_pixel(pix, r, g, b)
-- print("set_pixel " .. pix .. "(" .. r .. "," .. g .. "," .. b .. ")")
end
function keybow.auto_lights(onoff)
-- print("auto_lights", onoff)
end
function keybow.clear_lights()
-- print("clear_lights")
end
function keybow.tap_key(key)
print("tap_key " .. tostring(key))
end
function keybow.set_modifier(mod, key)
print("set_modifier " .. mod .. "," .. tostring(key))
end
require "keys"
setup()

58
sdcard/layouts/vsc-golang.lua

@ -1,6 +1,8 @@
-- VSC Go extension debug Keybow layout
require "snippets/multibow"
local vscgo = _G.vscgo or {} -- module
local mb = require "snippets/multibow"
--[[
The Keybow layout is as follows when in landscape orientation, with the USB
@ -22,6 +24,15 @@ cable going off "northwards":
]]--
-- Default hardware key to function assignments, can be overriden by users
vscgo.KEY_STOP = vscgo.KEY_STOP or 10
vscgo.KEY_RELOAD = vscgo.KEY_RELOAD or 7
vscgo.KEY_TESTPKG = vscgo.KEY_TESTPKG or 1
vscgo.KEY_CONT = vscgo.KEY_CONT or 9
vscgo.KEY_STEPOVER = vscgo.KEY_STEPOVER or 6
vscgo.KEY_STEPINTO = vscgo.KEY_STEPINTO or 3
vscgo.KEY_STEPOUT = vscgo.KEY_STEPOUT or 0
RED = { r=1, g=0, b=0 }
YELLOW = { r=1, g=0.8, b=0 }
GREEN = { r=0, g=1, b=0 }
@ -33,52 +44,49 @@ CYAN = { r=0, g=1, b=1 }
-- AND NOW FOR SOMETHING DIFFERENT: THE REAL MEAT --
function debug_stop(key)
function vscgo.debug_stop(key)
mb.tap(key, keybow.F5, keybow.LEFT_SHIFT)
end
function debug_restart(key)
function vscgo.debug_restart(key)
mb.tap(key, keybow.F5, keybow.LEFT_SHIFT, keybow.LEFT_CTRL)
end
function debug_continue(key)
function vscgo.debug_continue(key)
mb.tap(key, keybow.F5)
end
function debug_stepover(key)
function vscgo.debug_stepover(key)
mb.tap(key, keybow.F10)
end
function debug_stepinto(key)
function vscgo.debug_stepinto(key)
mb.tap(key, keybow.F11)
end
function debug_stepout(key)
function vscgo.debug_stepout(key)
mb.tap(key, keybow.F11, keybow.LEFT_SHIFT)
end
function go_test_package(key)
function vscgo.go_test_package(key)
mb.tap(key, "P", keybow.LEFT_SHIFT, keybow.LEFT_CTRL)
keybow.sleep(250)
keybow.text("go test package")
keybow.tap_enter()
end
mb.register_keymap({
vscgo.keymap = {
name="vsc-golang-debug",
[10] = {c=RED, press=debug_stop},
[7] = {c=YELLOW, press=debug_restart},
[1] = {c=CYAN, press=go_test_package},
[9] = {c=GREEN, press=debug_continue},
[6] = {c=BLUECYAN, press=debug_stepinto},
[3] = {c=BLUE, press=debug_stepover},
[0] = {c=BLUEGRAY, press=debug_stepout},
})
mb.register_keymap({
name="vsc-golang-debug-shifted",
secondary=true,
[0] = {c=RED}
})
[vscgo.KEY_STOP] = {c=RED, press=vscgo.debug_stop},
[vscgo.KEY_RELOAD] = {c=YELLOW, press=vscgo.debug_restart},
[vscgo.KEY_TESTPKG] = {c=CYAN, press=vscgo.go_test_package},
[vscgo.KEY_CONT] = {c=GREEN, press=vscgo.debug_continue},
[vscgo.KEY_STEPINTO] = {c=BLUECYAN, press=vscgo.debug_stepinto},
[vscgo.KEY_STEPOVER] = {c=BLUE, press=vscgo.debug_stepover},
[vscgo.KEY_STEPOUT] = {c=BLUEGRAY, press=vscgo.debug_stepout},
}
mb.register_keymap(vscgo.keymap)
return vscgo -- module

31
spec/keybowhandler.lua → spec/hwkeys.lua

@ -1,3 +1,7 @@
-- Stimulates Keybow "hardware key" presses and releases at the Keybow handler
-- level, thus involving everything starting with the Keybow "firmware" key
-- Lua handlers handle_key_xx.
--[[
Copyright 2019 Harald Albrecht
@ -20,24 +24,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
kbh = {} -- module
hwk = {} -- module
-- Convenience: returns the name of a Keybow key handler function.
function kbh.handler_name(keyno)
-- Convenience: returns the name of a Keybow key handler function for the
-- given key number.
function hwk.handler_name(keyno)
return string.format("handle_key_%02d", keyno)
end
-- Convenience: calls Keybow key handler by key number instead of name.
function kbh.handle_key(keyno, pressed)
_G[kbh.handler_name(keyno)](pressed)
-- Convenience: call Keybow key handler for key presses and releases by key
-- number instead of name.
function hwk.press(keyno)
_G[hwk.handler_name(keyno)](true)
end
function hwk.release(keyno)
_G[hwk.handler_name(keyno)](false)
end
-- Convenience: taps a Keybow key, triggering the corresponding key handler
-- twice.
function kbh.tap(keyno)
local h = _G[kbh.handler_name(keyno)]
h(true)
h(false)
function hwk.tap(keyno)
hwk.press(keyno)
hwk.release(keyno)
end
return kbh -- module
return hwk -- module

24
spec/keybowhandler_spec.lua → spec/hwkeys_spec.lua

@ -20,25 +20,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
local kbh = require("spec/keybowhandler")
local hwk = require("spec/hwkeys")
describe("Keybow keyhandler", function()
describe("Keybow hardware key handler module", function()
it("returns proper Keybow handler name", function()
assert.equals(kbh.handler_name(0), "handle_key_00")
assert.equals(hwk.handler_name(0), "handle_key_00")
end)
it("calls correct Keybow key handler", function()
stub(_G, "handle_key_00")
inslit("calls correct Keybow key handler", function()
stub(_G, "handle_key_00") -- note: no need to revert later due to insulation.
stub(_G, "handle_key_01")
kbh.handle_key(0, true)
hwk.press(0)
assert.stub(handle_key_00).was.called(1)
assert.stub(handle_key_00).was.called_with(true)
assert.stub(handle_key_01).was_not.called()
handle_key_00:revert()
handle_key_01:revert()
end)
describe("wrapped Keybow key handler 00", function()
@ -58,10 +55,11 @@ describe("Keybow keyhandler", function()
_G.handle_key_00 = old
end)
it("taps Keybow key handlers", function()
kbh.tap(0)
assert.equals(#seq, 2)
assert.same(seq, {true, false})
it("taps Keybow key handler with press and release", function()
-- this also implicitly tests hwk.press() and hw.release()
hwk.tap(0)
assert.equals(2, #seq)
assert.same({true, false}, seq)
end)
end)

66
spec/integration_spec.lua

@ -0,0 +1,66 @@
-- Tests all available multibow layouts in a single setup, thus trying to
-- mimic the final Keybow firmware installation as close as possible (for some
-- suitable definition of "close").
--[[
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")
require "layouts/shift"
require "layouts/kdenlive"
local vscgolang = require "layouts/vsc-golang"
require "layouts/empty"
local mb = require("snippets/multibow")
local hwk = require("spec/hwkeys")
describe("final Multibow integration", function()
it("correctly integrates", function()
local kms = mb.registered_keymaps()
-- shift: 2 registered keymaps
-- vsc-golang: 1 reg keymap
-- kdenlive: 2 reg keymaps
-- empty: 1 reg keymap
assert.is.equal(6, #kms)
end)
-- inslit() is an insulated(it()) ensuring that all stubs and spies get
-- removed after this test in any case.
inslit("starts gonelang debugging :)", function()
-- Switches to the VSC Golang Multibow keymap, regardless of the
-- order of keymap imports.
mb.activate_keymap(vscgolang.keymap.name)
assert.is.equal(vscgolang.keymap.name, mb.current_keymap.name)
assert.is.equal(vscgolang.debug_continue, vscgolang.keymap[vscgolang.KEY_CONT].press)
-- Checks that a press of the "Continue Debugging" key does in fact
-- trigger the corresponding keymap handler.
local s = stub(vscgolang.keymap[vscgolang.KEY_CONT], "press")
hwk.tap(vscgolang.KEY_STEPOVER)
assert.stub(s).was_not.called()
hwk.tap(vscgolang.KEY_CONT)
assert.stub(s).was.called(1)
end)
end)

0
spec/empty_spec.lua → spec/layouts/empty_spec.lua

4
spec/kdenlive_spec.lua → spec/layouts/kdenlive_spec.lua

@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
local kbh = require("spec/keybowhandler")
local hwk = require("spec/hwkeys")
local mb = require("snippets/multibow")
local k = require("layouts/kdenlive")
@ -29,7 +29,7 @@ describe("Kdenlive keymap", function()
it("...", function()
local kms = mb.registered_keymaps()
assert.is.equal(#kms, 2)
assert.is.equal(2, #kms)
end)
end)

28
spec/shift_spec.lua → spec/layouts/shift_spec.lua

@ -22,7 +22,7 @@ SOFTWARE.
require "mocked-keybow"
require "snippets/multibow"
local kbh = require("spec/keybowhandler")
local hwk = require("spec/hwkeys")
local mb = require("snippets/multibow")
describe("SHIFT multibow keymap", function()
@ -83,14 +83,14 @@ describe("SHIFT multibow keymap", function()
spy.on(mb, "ungrab")
-- route in the SHIFT permanent keymap
kbh.handle_key(shift.KEY_SHIFT, true)
hwk.press(shift.KEY_SHIFT)
assert.spy(mb.grab).was.called(1)
assert.spy(mb.grab).was.called_with(shift.keymap_shifted.name)
-- route in the shifted(!) SHIFT keymap, so this checks
-- that we ungrab correctly
mb.grab:clear()
kbh.handle_key(shift.KEY_SHIFT, false)
hwk.release(shift.KEY_SHIFT)
assert.spy(mb.grab).was_not.called()
assert.spy(mb.ungrab).was.called(1)
@ -104,7 +104,7 @@ describe("SHIFT multibow keymap", function()
stub(shift, "shift_secondary_keymap")
-- test that lonely SHIFT triggers...
kbh.tap(shift.KEY_SHIFT)
hwk.tap(shift.KEY_SHIFT)
assert.stub(shift.shift_secondary_keymap).was.called(1)
-- but that SHIFT followed by another function doesn't shift.
@ -113,9 +113,9 @@ describe("SHIFT multibow keymap", function()
shift.KEY_LAYOUT,
shift.KEY_BRIGHTNESS
}) do
kbh.handle_key(shift.KEY_SHIFT, true)
kbh.tap(key)
kbh.handle_key(shift.KEY_SHIFT, false)
hwk.press(shift.KEY_SHIFT)
hwk.tap(key)
hwk.release(shift.KEY_SHIFT)
assert.stub(shift.shift_secondary_keymap).was_not.called()
end
end)
@ -137,10 +137,10 @@ describe("SHIFT multibow keymap", function()
spy.on(mb, "activate_keymap")
kbh.tap(shift.KEY_SHIFT)
hwk.tap(shift.KEY_SHIFT)
assert.spy(mb.activate_keymap).was.called_with(keymap_shifted.name)
assert.is.equal(mb.current_keymap, keymap_shifted)
kbh.tap(shift.KEY_SHIFT)
hwk.tap(shift.KEY_SHIFT)
assert.is.equal(mb.current_keymap, keymap)
mb.activate_keymap:revert()
@ -151,17 +151,17 @@ describe("SHIFT multibow keymap", function()
describe("while SHIFTed", function()
before_each(function()
kbh.handle_key(shift.KEY_SHIFT, true)
hwk.press(shift.KEY_SHIFT)
end)
after_each(function()
kbh.handle_key(shift.KEY_SHIFT, false)
hwk.release(shift.KEY_SHIFT)
end)
it("cycles primary keymaps", function()
stub(mb, "cycle_primary_keymaps")
kbh.tap(shift.KEY_LAYOUT)
hwk.tap(shift.KEY_LAYOUT)
assert.stub(mb.cycle_primary_keymaps).was.called(1)
mb.cycle_primary_keymaps:revert()
@ -170,8 +170,8 @@ describe("SHIFT multibow keymap", function()
it("changes brightness", function()
stub(mb, "set_brightness")
kbh.tap(shift.KEY_BRIGHTNESS)
kbh.tap(shift.KEY_BRIGHTNESS)
hwk.tap(shift.KEY_BRIGHTNESS)
hwk.tap(shift.KEY_BRIGHTNESS)
assert.stub(mb.set_brightness).was.called(2)
mb.set_brightness:revert()

0
spec/multibow_spec.lua → spec/snippets/multibow_spec.lua

26
spec/routehandlers_spec.lua → spec/snippets/routehandlers_spec.lua

@ -21,7 +21,7 @@ SOFTWARE.
]]--
require "mocked-keybow"
local kbh = require("spec/keybowhandler")
local hwk = require("spec/hwkeys")
describe("routehandlers", function()
@ -86,11 +86,11 @@ describe("routehandlers", function()
stub(mb, "route")
for keyno = 0, 11 do
mb.route:clear()
kbh.handle_key(keyno, true)
hwk.press(keyno)
assert.stub(mb.route).was.called(1)
assert.stub(mb.route).was.called_with(keyno, true)
mb.route:clear()
kbh.handle_key(keyno, false)
hwk.release(keyno)
assert.stub(mb.route).was.called(1)
assert.stub(mb.route).was.called_with(keyno, false)
end
@ -102,8 +102,8 @@ describe("routehandlers", function()
insl(function()
it("doesn't fail for unroutable keys", function()
kbh.handle_key(0, true)
kbh.handle_key(0, false)
hwk.press(0)
hwk.release(0)
end)
-- start with secondary keymap only :)
@ -113,14 +113,14 @@ describe("routehandlers", function()
mb.register_keymap(secondary_keymap)
assert.spy(spies.sec_key_press).was_not.called() -- just a safety guard
kbh.handle_key(0, true)
hwk.press(0)
assert.spy(spies.sec_key_press).was_not.called()
end)
it("routes key press to activated secondary keymap", function()
mb.activate_keymap(secondary_keymap.name)
kbh.handle_key(0, true)
hwk.press(0)
assert.spy(spies.sec_key_press).was.called(1)
assert.spy(spies.sec_key_press).was.called_with(0)
end)
@ -132,7 +132,7 @@ describe("routehandlers", function()
mb.activate_keymap(primary_keymap.name)
assert.spy(spies.prim_key_press).was_not.called() -- just a safety guard
kbh.handle_key(0, true)
hwk.press(0)
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()
@ -140,7 +140,7 @@ describe("routehandlers", function()
it("routes key release to primary keymap", function()
assert.spy(spies.prim_key_press).was_not.called() -- just a safety guard
kbh.handle_key(0, false)
hwk.release(0)
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)
@ -152,7 +152,7 @@ describe("routehandlers", function()
assert.is_not_truthy(permanent_keymap.secondary)
mb.register_keymap(permanent_keymap)
kbh.handle_key(0, true)
hwk.press(0)
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()
@ -162,7 +162,7 @@ describe("routehandlers", function()
it("correctly routes key press to un-overlaid primary key 2", function()
assert.spy(spies.prim_otherkey_press).was_not_called()
kbh.handle_key(1, true)
hwk.press(1)
assert.spy(spies.prim_otherkey_press).was.called(1)
assert.spy(spies.prim_otherkey_press).was.called_with(1)
end)
@ -173,13 +173,13 @@ describe("routehandlers", function()
mb.grab(grab_keymap.name)
assert.spy(spies.grab_key_press).was.called(0)
kbh.handle_key(0, true)
hwk.press(0)
assert.spy(spies.grab_key_press).was.called(1)
assert.spy(spies.grab_key_press).was.called_with(0)
spies.grab_key_press:clear()
mb.ungrab()
kbh.handle_key(0, true)
hwk.press(0)
assert.spy(spies.grab_key_press).was.called(0)
assert.spy(spies.perm_key_press).was.called(1)
assert.spy(spies.prim_key_press).was.called(0)

48
test.lua

@ -1,48 +0,0 @@
package.path = "./sdcard/?.lua;./mock/?.lua;" .. package.path
require "mockkeybow"
print()
print("**** key 00...")
handle_key_00(true)
handle_key_00(false)
print()
print("**** key 03...")
handle_key_03(true)
handle_key_03(false)
print()
print("**** key 00...")
handle_key_00(true)
handle_key_00(false)
print()
print("**** key 11+05...")
handle_key_11(true)
handle_key_05(true)
handle_key_05(false)
handle_key_11(false)
print()
print("**** key 00...")
handle_key_00(true)
handle_key_00(false)
print()
print("**** key 11+08...")
handle_key_11(true)
handle_key_08(true)
handle_key_08(false)
handle_key_11(false)
print()
print("**** key 11...")
handle_key_11(true)
handle_key_11(false)
print()
print("**** key 11+08...")
handle_key_11(true)
handle_key_08(true)
handle_key_08(false)
handle_key_11(false)
Loading…
Cancel
Save