-
Notifications
You must be signed in to change notification settings - Fork 2
/
spec.lua
432 lines (356 loc) · 14.4 KB
/
spec.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
require 'busted'
local Terebi = require 'terebi'
local noop = function()
return spy.new(function() end)
end
describe('Terebi:', function()
local pixelScale
before_each(function()
pixelScale = 1
_G.love = {
graphics = {},
mouse = {},
window = {},
}
_G.love.graphics.newCanvas = spy.new(function(w, h)
return {w, h}
end)
_G.love.graphics.newShader = spy.new(function(w, h)
return {
send = noop(),
}
end)
_G.love.graphics.setShader = noop()
_G.love.window.getDesktopDimensions = spy.new(function()
return 1600, 1200
end)
_G.love.window.getFullscreen = spy.new(function()
return false
end)
_G.love.window.getMode = spy.new(function()
return 640, 480, {'flags'}
end)
_G.love.window.fromPixels = spy.new(function(x, y)
return x / pixelScale, y / pixelScale
end)
_G.love.window.toPixels = spy.new(function(x, y)
return x * pixelScale, y * pixelScale
end)
end)
describe('When calling initializeLoveDefaults:', function()
before_each(function()
_G.love.graphics.setDefaultFilter = noop()
_G.love.graphics.setLineStyle = noop()
end)
it('It should call correct love2d methods.', function()
Terebi.initializeLoveDefaults()
assert.spy(love.graphics.setDefaultFilter).was.called_with('nearest', 'nearest')
assert.spy(love.graphics.setLineStyle).was.called_with('rough')
end)
end)
describe('When creating a new Screen:', function()
it('It should have correct default attributes', function()
local screen = Terebi.newScreen(320, 240, 2)
assert.spy(love.graphics.newCanvas).was.called_with(320, 240)
assert.are.same({0, 0, 0}, screen._backgroundColor)
assert.are.same(320, screen._width)
assert.are.same(240, screen._height)
assert.are.same(2, screen._scale)
assert.are.same(2, screen._savedScale)
end)
it('It should throw an error when passed invalid parameters', function()
assert.error(function() Terebi.newScreen(nil, 240, 2) end)
assert.error(function() Terebi.newScreen(320, nil, 2) end)
assert.error(function() Terebi.newScreen(320, 240, nil) end)
end)
end)
describe('When calling Screen methods:', function()
local screen
before_each(function()
screen = Terebi.newScreen(320, 240, 2)
end)
describe('When calling setBackgroundColor:', function ()
it('setBackgroundColor should set background color', function()
screen:setBackgroundColor(10, 20, 30)
assert.are.same({10, 20, 30}, screen._backgroundColor)
end)
it('setBackgroundColor should throw an error when passed non-numbers', function()
assert.error(function() screen:setBackgroundColor(nil, 1, 1) end)
assert.error(function() screen:setBackgroundColor(1, nil, 1) end)
assert.error(function() screen:setBackgroundColor(1, 1, nil) end)
end)
end)
it('getScale should return scale', function()
assert.are.same(2, screen:getScale())
end)
it('getDimensions should return width and height', function()
assert.are.same({320, 240}, {screen:getDimensions()})
end)
describe('When changing Screen scale:', function()
before_each(function()
_G.love.window.setMode = noop()
end)
describe('When calling setScale:', function ()
it('setScale should set the scale', function()
screen:setScale(1)
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(320, 240, {'flags'})
end)
it('setScale should floor the scale when passed a non-integer with "integer" mode', function()
screen:setMode('integer')
screen:setScale(1.5)
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(320, 240, {'flags'})
end)
it('setScale should set scale when passed a non-integer with "float" mode', function()
screen:setMode('float')
screen:setScale(1.5)
assert.are.same(1.5, screen._scale)
assert.spy(love.window.setMode).was.called_with(480, 360, {'flags'})
end)
it('setScale should set scale to 1 when passed a number below 1', function()
screen:setScale(0)
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(320, 240, {'flags'})
end)
it('setScale should throw an error when passed a non-number', function()
assert.error(function() screen:setScale(nil) end)
end)
end)
describe('When calling increaseScale:', function ()
it('increaseScale should increase the scale', function()
screen:increaseScale()
assert.are.same(3, screen._scale)
assert.spy(love.window.setMode).was.called_with(960, 720, {'flags'})
end)
it('increaseScale should not set scale above maximum scale', function()
screen
:setScale(5)
:increaseScale()
assert.are.same(5, screen._scale)
assert.spy(love.window.setMode).was.called_with(1600, 1200, {'flags'})
end)
end)
describe('When calling decreaseScale:', function ()
it('decreaseScale should decrease the scale', function()
screen:decreaseScale()
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(320, 240, {'flags'})
end)
it('decreaseScale should not set scale below 1', function()
screen
:setScale(1)
:decreaseScale()
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(320, 240, {'flags'})
end)
end)
describe('When calling setMaxScale:', function ()
it('setMaxScale should set the scale', function()
screen:setMaxScale()
assert.are.same(5, screen._scale)
assert.spy(love.window.setMode).was.called_with(1600, 1200, {'flags'})
end)
end)
describe('When calling toggleFullscreen:', function ()
it('toggleFullscreen should toggle fullscreen and maximize scale', function()
_G.love.window.setFullscreen = noop()
screen:toggleFullscreen()
assert.are.same(5, screen._scale)
assert.spy(love.window.setMode).was.called_with(1600, 1200, {'flags'})
assert.spy(love.window.setFullscreen).was.called_with(true)
end)
it('toggleFullscreen should restore scale when toggled on then off', function()
local isFullscreen = false
_G.love.window.setFullscreen = spy.new(function(val)
-- When exiting fullscreen, love will change the window res to the fullscreen res.
if val == false and isFullscreen == true then
_G.love.window.getMode = spy.new(function()
return 1600, 1200, {'flags'}
end)
end
isFullscreen = val
end)
_G.love.window.getFullscreen = spy.new(function()
return isFullscreen
end)
screen
:toggleFullscreen()
:toggleFullscreen()
assert.are.same(2, screen._scale)
assert.are.same(false, isFullscreen)
assert.spy(love.window.setMode).was.called_with(640, 480, {'flags'})
assert.spy(love.window.setFullscreen).was.called(2)
end)
end)
describe('When high density pixel scale:', function ()
before_each(function ()
pixelScale = 2
end)
it('increaseScale should increase the scale', function()
screen:increaseScale()
assert.are.same(3, screen._scale)
assert.spy(love.window.setMode).was.called_with(480, 360, {'flags'})
end)
it('decreaseScale should set the scale', function()
screen:decreaseScale()
assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(160, 120, {'flags'})
end)
it('setMaxScale should set the scale', function()
screen:setMaxScale()
assert.are.same(10, screen._scale)
assert.spy(love.window.setMode).was.called_with(1600, 1200, {'flags'})
end)
end)
end)
describe('When calling draw:', function ()
local originalCanvas
local terebiCanvas
local drawSpy
local drawFunc
before_each(function()
terebiCanvas = screen._canvas
_G.love.graphics.push = noop()
_G.love.graphics.setCanvas = noop()
_G.love.graphics.pop = noop()
_G.love.graphics.clear = noop()
_G.love.graphics.draw = noop()
drawSpy = noop()
drawFunc = function(...) drawSpy(...) end
end)
it('should draw to canvas', function()
screen:draw(drawFunc, 'arg1', 'arg2')
assert.spy(love.graphics.push).was.called_with('all')
assert.spy(love.graphics.setCanvas).was.called_with(terebiCanvas)
assert.spy(love.graphics.clear).was.called()
assert.spy(drawSpy).was.called_with('arg1', 'arg2')
assert.spy(love.graphics.pop).was.called()
assert.spy(love.graphics.draw).was.called_with(terebiCanvas, 0, 0, 0, 2, 2)
end)
it('should draw background when screen would have letterboxing', function()
_G.love.graphics.getColor = spy.new(function()
return 10, 20, 30, 255
end)
_G.love.graphics.getDimensions = spy.new(function()
return 640, 480
end)
_G.love.graphics.rectangle = noop()
_G.love.graphics.setColor = noop()
screen._scale = 1
screen._drawOffsetX = 160
screen._drawOffsetY = 120
screen:draw(drawFunc, 'arg1', 'arg2')
assert.spy(love.graphics.push).was.called_with('all')
assert.spy(love.graphics.setCanvas).was.called_with(terebiCanvas)
assert.spy(love.graphics.clear).was.called()
assert.spy(drawSpy).was.called_with('arg1', 'arg2')
assert.spy(love.graphics.pop).was.called()
assert.spy(love.graphics.getColor).was.called()
assert.spy(love.graphics.getDimensions).was.called()
assert.spy(love.graphics.rectangle).was.called_with('fill', 0, 0, 640, 480)
assert.spy(love.graphics.setColor).was.called_with(10, 20, 30)
assert.spy(love.graphics.draw).was.called_with(terebiCanvas, 160, 120, 0, 1, 1)
end)
it('should throw error when draw function is not provided', function()
assert.error(function() screen:draw() end)
end)
end)
describe('When calling handleResize', function()
local isMaximized
before_each(function()
_G.love.window.getMode = spy.new(function()
return 1000, 1000, {'flags'}
end)
_G.love.window.isMaximized = spy.new(function()
return isMaximized
end)
_G.love.window.setMode = noop()
end)
describe('In integer mode', function()
before_each(function()
screen:setMode('integer')
end)
it('should update scale and resize window when not maximized', function()
isMaximized = false
screen:handleResize()
assert.same(3, screen:getScale())
assert.spy(love.window.getMode).was.called()
assert.spy(love.window.isMaximized).was.called()
assert.spy(love.window.setMode).was.called_with(960, 720, {'flags'})
end)
it('should update scale and not resize window when maximized', function()
isMaximized = true
screen:handleResize()
assert.same(3, screen:getScale())
assert.spy(love.window.getMode).was.called()
assert.spy(love.window.isMaximized).was.called()
assert.spy(love.window.setMode).was.not_called()
end)
end)
describe('In float mode', function()
before_each(function()
screen:setMode('float')
end)
it('should update scale and resize window when not maximized', function()
isMaximized = false
screen:handleResize()
assert.same(3.125, screen:getScale())
assert.spy(love.window.getMode).was.called()
assert.spy(love.window.isMaximized).was.called()
assert.spy(love.window.setMode).was.called_with(1000, 750, {'flags'})
end)
it('should update scale and not resize window when maximized', function()
isMaximized = true
screen:handleResize()
assert.same(3.125, screen:getScale())
assert.spy(love.window.getMode).was.called()
assert.spy(love.window.isMaximized).was.called()
assert.spy(love.window.setMode).was.not_called()
end)
end)
end)
describe('When calling getMousePosition', function ()
it('should convert mouse coordinates on window to screen coordinates', function ()
_G.love.mouse.getPosition = spy.new(function()
return 0, 0
end)
assert.same({0, 0}, {screen:getMousePosition()})
assert.spy(love.mouse.getPosition).was.called()
_G.love.mouse.getPosition = spy.new(function()
return 100, 256
end)
assert.same({50, 128}, {screen:getMousePosition()})
assert.spy(love.mouse.getPosition).was.called()
end)
end)
describe('When calling windowToScreen', function ()
it('should convert window coordinates to screen coordinates', function ()
assert.same({0, 0}, {screen:windowToScreen(0, 0)})
assert.same({50, 128}, {screen:windowToScreen(100, 256)})
end)
describe('When screen has a draw offset', function ()
it('should convert window coordinates to screen coordinates', function ()
screen._drawOffsetX = 30
screen._drawOffsetY = 20
assert.same({0, 0}, {screen:windowToScreen(30, 20)})
assert.same({35, 118}, {screen:windowToScreen(100, 256)})
end)
end)
end)
describe('When calling screenToWindow', function ()
it('should convert screen coordinates to window coordinates', function ()
assert.same({0, 0}, {screen:screenToWindow(0, 0)})
assert.same({100, 256}, {screen:screenToWindow(50, 128)})
end)
describe('When screen has a draw offset', function ()
it('should convert screen coordinates to window coordinates', function ()
screen._drawOffsetX = 30
screen._drawOffsetY = 20
assert.same({30, 20}, {screen:screenToWindow(0, 0)})
assert.same({130, 276}, {screen:screenToWindow(50, 128)})
end)
end)
end)
end)
end)