Skip to content

Commit

Permalink
Add some tests for increasing and decreasing windows sticking to the …
Browse files Browse the repository at this point in the history
…sides
  • Loading branch information
peterklijn committed Aug 28, 2022
1 parent f7eafb0 commit 0aac20a
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 3 deletions.
36 changes: 35 additions & 1 deletion hammerspoon_mocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,42 @@ function hotkey.bind(mods, key, fn)
table.insert(hotkey.bindings, { mods, key, fn })
end

local screen = {
rect = { x = 0, y = 0, w = 1200, h = 800 },
}

function screen:frame()
return self.rect
end

local window = {
rect = { x = 0, y = 0, w = 1200, h = 800 },
_screen = screen,
}

function window.focusedWindow()
return window
end

function window:frame()
return self.rect
end

function window:screen()
return self._screen
end

function window:move(rect, _, _, _)
lu.assertIsNumber(rect.x)
lu.assertIsNumber(rect.y)
lu.assertIsNumber(rect.w)
lu.assertIsNumber(rect.h)
self.rect = rect
end

local mocks = {
hotkey = hotkey
hotkey = hotkey,
window = window,
}

function mocks:reset()
Expand Down
119 changes: 117 additions & 2 deletions init_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ local hsmocks = require('hammerspoon_mocks')
TestShiftIt = {} -- luacheck: ignore 111

function TestShiftIt.setUp()
shiftit.hs = hsmocks
hsmocks:reset()
end

function TestShiftIt.testBindDefault()
shiftit.hs = hsmocks
shiftit:bindHotkeys({})
lu.assertEquals(#hsmocks.hotkey.bindings, 16)

Expand All @@ -29,7 +29,6 @@ function TestShiftIt.testBindDefault()
end

function TestShiftIt.testBindOverrideVimKeys()
shiftit.hs = hsmocks
shiftit:bindHotkeys({
left = { { 'ctrl', 'alt', 'cmd' }, 'h' },
down = { { 'ctrl', 'alt', 'cmd' }, 'j' },
Expand All @@ -50,4 +49,120 @@ function TestShiftIt.testBindOverrideVimKeys()
end
end

function TestShiftIt.testResizeWindowInStepsStickingToSides()
local tests = {
{
desc = 'increase window sticking to left',
before = { x = 0, y = 0, w = 600, h = 800 },
expect = { x = 0, y = 0, w = 700, h = 800 },
increase = true,
},
{
desc = 'decrease window sticking to left',
before = { x = 0, y = 0, w = 600, h = 800 },
expect = { x = 0, y = 0, w = 500, h = 800 },
increase = false,
},
{
desc = 'increase window sticking to right',
before = { x = 600, y = 0, w = 600, h = 800 },
expect = { x = 500, y = 0, w = 700, h = 800 },
increase = true,
},
{
desc = 'decrease window sticking to right',
before = { x = 600, y = 0, w = 600, h = 800 },
expect = { x = 700, y = 0, w = 500, h = 800 },
increase = false,
},
{
desc = 'increase window sticking to top',
before = { x = 0, y = 0, w = 1200, h = 400 },
expect = { x = 0, y = 0, w = 1200, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to top',
before = { x = 0, y = 0, w = 1200, h = 400 },
expect = { x = 0, y = 0, w = 1200, h = 334 },
increase = false,
},
{
desc = 'increase window sticking to bottom',
before = { x = 0, y = 400, w = 1200, h = 400 },
expect = { x = 0, y = 334, w = 1200, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to bottom',
before = { x = 0, y = 400, w = 1200, h = 400 },
expect = { x = 0, y = 466, w = 1200, h = 334 },
increase = false,
},
}
for _, test in pairs(tests) do
hsmocks.window.rect = test.before
shiftit:resizeWindowInSteps(test.increase)
lu.assertEquals(hsmocks.window.rect, test.expect, test.desc)
end
end

function TestShiftIt.testResizeWindowInStepsStickingToCorners()
local tests = {
{
desc = 'increase window sticking to left top',
before = { x = 0, y = 0, w = 600, h = 400 },
expect = { x = 0, y = 0, w = 700, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to left top',
before = { x = 0, y = 0, w = 600, h = 400 },
expect = { x = 0, y = 0, w = 500, h = 334 },
increase = false,
},
{
desc = 'increase window sticking to right top',
before = { x = 600, y = 0, w = 600, h = 400 },
expect = { x = 500, y = 0, w = 700, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to right top',
before = { x = 600, y = 0, w = 600, h = 400 },
expect = { x = 700, y = 0, w = 500, h = 334 },
increase = false,
},
{
desc = 'increase window sticking to left bottom',
before = { x = 0, y = 400, w = 600, h = 400 },
expect = { x = 0, y = 334, w = 700, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to left bottom',
before = { x = 0, y = 400, w = 600, h = 400 },
expect = { x = 0, y = 466, w = 500, h = 334 },
increase = false,
},
{
desc = 'increase window sticking to right bottom',
before = { x = 0, y = 400, w = 600, h = 400 },
expect = { x = 0, y = 334, w = 700, h = 466 },
increase = true,
},
{
desc = 'decrease window sticking to right bottom',
before = { x = 600, y = 400, w = 600, h = 400 },
expect = { x = 700, y = 466, w = 500, h = 334 },
increase = false,
},
}
for _, test in pairs(tests) do
hsmocks.window.rect = test.before
shiftit:resizeWindowInSteps(test.increase)
lu.assertEquals(hsmocks.window.rect, test.expect, test.desc)
end
end

os.exit(lu.LuaUnit.run())

0 comments on commit 0aac20a

Please sign in to comment.