-
Notifications
You must be signed in to change notification settings - Fork 29
/
init_test.lua
360 lines (338 loc) · 12.8 KB
/
init_test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
local lu = require('luaunit')
local shiftit = require('init')
local hsmocks = require('hammerspoon_mocks')
-- disable lint errors for mutating global variable TestShiftIt:
-- luacheck: ignore 112
TestShiftIt = {} -- luacheck: ignore 111
shiftit.hs = hsmocks
function TestShiftIt.setUp()
hsmocks:reset()
shiftit:setWindowCyclingSizes({ 50 }, { 50 }, true)
end
function TestShiftIt.testBindDefault()
shiftit:bindHotkeys({})
lu.assertEquals(#hsmocks.hotkey.bindings, 16)
local expected = {
'left', 'right', 'up', 'down',
'1', '2', '3', '4', 'm',
'f', 'z', 'c', 'n', 'p',
'=', '-',
}
for i, item in pairs(expected) do
lu.assertEquals(hsmocks.hotkey.bindings[i][1], shiftit.mash)
lu.assertEquals(hsmocks.hotkey.bindings[i][2], item)
end
end
function TestShiftIt.testBindOverrideVimKeys()
shiftit:bindHotkeys({
left = { { 'ctrl', 'alt', 'cmd' }, 'h' },
down = { { 'ctrl', 'alt', 'cmd' }, 'j' },
up = { { 'ctrl', 'alt', 'cmd' }, 'k' },
right = { { 'ctrl', 'alt', 'cmd' }, 'l' },
})
lu.assertEquals(#hsmocks.hotkey.bindings, 16)
local expected = {
'h', 'l', 'k', 'j',
'1', '2', '3', '4', 'm',
'f', 'z', 'c', 'n', 'p',
'=', '-',
}
for i, item in pairs(expected) do
lu.assertEquals(hsmocks.hotkey.bindings[i][1], shiftit.mash)
lu.assertEquals(hsmocks.hotkey.bindings[i][2], item)
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
function TestShiftIt.testResizeWindowInStepsEdgeCases()
local tests = {
{
desc = 'does not exceed screen width',
before = { x = 5, y = 200, w = 1190, h = 400 },
expect = { x = 0, y = 134, w = 1200, h = 532 },
increase = true,
},
{
desc = 'does not exceed screen height',
before = { x = 200, y = 5, w = 800, h = 790 },
expect = { x = 100, y = 0, w = 1000, h = 800 },
increase = true,
},
{
desc = 'does not exceed screen size',
before = { x = 5, y = 5, w = 1190, h = 790 },
expect = { x = 0, y = 0, w = 1200, h = 800 },
increase = true,
},
{
desc = 'does not become too narrow',
before = { x = 0, y = 0, w = 300, h = 800 },
expect = { x = 0, y = 66, w = 300, h = 668 },
increase = false,
},
{
desc = 'does not become too short',
before = { x = 0, y = 0, w = 1200, h = 198 },
expect = { x = 100, y = 0, w = 1000, h = 198 },
increase = false,
},
{
desc = 'does not become too tiny',
before = { x = 100, y = 100, w = 300, h = 198 },
expect = { x = 100, y = 100, w = 300, h = 198 },
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.testInitialiseSteps()
lu.assertEquals(shiftit.cycleSizesX, { 50 })
lu.assertEquals(shiftit.cycleSizesY, { 50 })
lu.assertEquals(shiftit.nextCycleSizeX, { [50] = 50 })
lu.assertEquals(shiftit.nextCycleSizeY, { [50] = 50 })
shiftit:setWindowCyclingSizes({ 80, 60, 40 }, { 75, 50, 25 }, true)
lu.assertEquals(shiftit.cycleSizesX, { 80, 60, 40 })
lu.assertEquals(shiftit.cycleSizesY, { 75, 50, 25 })
lu.assertEquals(shiftit.nextCycleSizeX, { [40] = 80, [60] = 40, [80] = 60 })
lu.assertEquals(shiftit.nextCycleSizeY, { [25] = 75, [50] = 25, [75] =50 })
end
function TestShiftIt.testDefaultWindowShifts()
local tests = {
{
desc = 'shift to the left',
action = function () shiftit:left() end,
expect = { x = 0, y = 0, w = 600, h = 800 },
},
{
desc = 'shift to the right',
action = function() shiftit:right() end,
expect = { x = 600, y = 0, w = 600, h = 800 },
},
{
desc = 'shift to the top',
action = function() shiftit:up() end,
expect = { x = 0, y = 0, w = 1200, h = 400 },
},
{
desc = 'shift to the bottom',
action = function() shiftit:down() end,
expect = { x = 0, y = 400, w = 1200, h = 400 },
},
{
desc = 'shift to the left top',
action = function() shiftit:upleft() end,
expect = { x = 0, y = 0, w = 600, h = 400 },
},
{
desc = 'shift to the right top',
action = function() shiftit:upright() end,
expect = { x = 600, y = 0, w = 600, h = 400 },
},
{
desc = 'shift to the left bottom',
action = function() shiftit:botleft() end,
expect = { x = 0, y = 400, w = 600, h = 400 },
},
{
desc = 'shift to the right bottom',
action = function() shiftit:botright() end,
expect = { x = 600, y = 400, w = 600, h = 400 },
},
}
for _, test in pairs(tests) do
hsmocks:reset()
test.action()
lu.assertEquals(hsmocks.window.rect, test.expect, test.desc)
end
end
function TestShiftIt.testMultipleWindowSizeSteps()
shiftit:setWindowCyclingSizes({ 50, 75, 25 }, { 50 }, true)
local tests = {
{
desc = 'shift to the left through every option once',
action = function() shiftit:left() end,
expectSteps = {
{ x = 0, y = 0, w = 600, h = 800 },
{ x = 0, y = 0, w = 900, h = 800 },
{ x = 0, y = 0, w = 300, h = 800 },
},
},
{
desc = 'shift to the right bottom through every option twice',
action = function() shiftit:botright() end,
expectSteps = {
{ x = 600, y = 400, w = 600, h = 400 },
{ x = 300, y = 400, w = 900, h = 400 },
{ x = 900, y = 400, w = 300, h = 400 },
{ x = 600, y = 400, w = 600, h = 400 },
{ x = 300, y = 400, w = 900, h = 400 },
{ x = 900, y = 400, w = 300, h = 400 },
},
},
}
for _, test in pairs(tests) do
hsmocks:reset()
for i, expect in pairs(test.expectSteps) do
test.action()
lu.assertEquals(hsmocks.window.rect, expect, test.desc .. '> step ' .. i)
end
end
end
function TestShiftIt.testMultipleWindowSizeStepsWithMultipleWindows()
shiftit:setWindowCyclingSizes({ 50, 75, 25 }, { 50 }, true)
local windowId1 = 99
local windowId2 = 88
hsmocks.window._id = windowId1
shiftit:left()
lu.assertEquals(hsmocks.window.rect, { x = 0, y = 0, w = 600, h = 800 })
shiftit:left()
lu.assertEquals(hsmocks.window.rect, { x = 0, y = 0, w = 900, h = 800 })
-- Mock switching windows, set the window size to differt from the last rect
hsmocks.window._id = windowId2
hsmocks.window.rect = { x = 100, y = 100, w = 600, h = 500 }
shiftit:right()
lu.assertEquals(hsmocks.window.rect, { x = 600, y = 0, w = 600, h = 800 })
shiftit:right()
lu.assertEquals(hsmocks.window.rect, { x = 300, y = 0, w = 900, h = 800 })
shiftit:right()
lu.assertEquals(hsmocks.window.rect, { x = 900, y = 0, w = 300, h = 800 })
-- Mock switching windows, set the window size to the last size of this window
hsmocks.window._id = windowId1
hsmocks.window.rect = { x = 0, y = 0, w = 900, h = 800 }
shiftit:left()
-- because the window was switched, we start at the beginning of the cycle, and expect the 50% value
lu.assertEquals(hsmocks.window.rect, { x = 0, y = 0, w = 600, h = 800 })
-- Mock switching windows, set the window size to the last size of this window
hsmocks.window._id = windowId2
hsmocks.window.rect = { x = 900, y = 0, w = 300, h = 800 }
shiftit:right()
lu.assertEquals(hsmocks.window.rect, { x = 600, y = 0, w = 600, h = 800 })
-- Mock switching windows, set the window size to the last size of this window
hsmocks.window._id = windowId1
hsmocks.window.rect = { x = 0, y = 0, w = 600, h = 800 }
shiftit:left()
-- because the window was switched, we start at the beginning of the cycle, and expect the 50% value
-- but because this did not cause any change, the function will call itself once, going to the 75% value
lu.assertEquals(hsmocks.window.rect, { x = 0, y = 0, w = 900, h = 800 })
shiftit:maximum()
shiftit:left()
-- because the window maximised, we expect the cycle to restart
lu.assertEquals(hsmocks.window.rect, { x = 0, y = 0, w = 600, h = 800 })
end
os.exit(lu.LuaUnit.run())