Skip to content

Commit

Permalink
added coroutines for test delays
Browse files Browse the repository at this point in the history
  • Loading branch information
ellraiser committed Oct 14, 2023
1 parent 93b7cc4 commit 37d4e8c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 147 deletions.
13 changes: 4 additions & 9 deletions classes/TestMethod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ TestMethod = {
imgs = 1,
delay = 0,
delayed = false,
store = {}
store = {},
co = nil
}
setmetatable(test, self)
self.__index = self
Expand Down Expand Up @@ -281,14 +282,8 @@ TestMethod = {
end,


-- currently unused
setDelay = function(self, frames)
self.delay = frames
self.delayed = true
love.test.delayed = self
end,
isDelayed = function(self)
return self.delayed
waitFrames = function(self, frames)
for i=1,frames do coroutine.yield() end
end,


Expand Down
2 changes: 0 additions & 2 deletions classes/TestModule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ TestModule = {
-- @return {table} - returns the new Suite object
new = function(self, module, method)
local testmodule = {
timer = 0,
time = 0,
delay = 0.01,
spacer = ' ',
colors = {
PASS = 'green', FAIL = 'red', SKIP = 'grey'
Expand Down
52 changes: 15 additions & 37 deletions classes/TestSuite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ TestSuite = {

-- stagger between tests
if self.module ~= nil then
self.module.timer = self.module.timer + delta
if self.module.timer >= self.module.delay then
self.module.timer = self.module.timer - self.module.delay

if self.module.start == true then

-- work through each test method 1 by 1
Expand All @@ -74,55 +72,35 @@ TestSuite = {
self.test = TestMethod:new(method, self.module)
TextRun:set('love.' .. self.module.module .. '.' .. method)

-- check method exists in love first
if love[self.module.module] == nil then
local tested = 'love.' .. self.module.module .. '.' .. method .. '()'
local matching = string.sub(self.module.spacer, string.len(tested), 40)
self.module:log(self.module.colors['FAIL'],
tested .. matching,
' ==> FAIL (0/0) - call failed - method does not exist'
)
-- otherwise run the test method
else
local ok, chunk, err = pcall(self[self.module.module][method], self.test)
self.test.co = coroutine.create(function()
local ok, chunk, err = pcall(love.test[love.test.module.module][method], love.test.test)
if ok == false then
self.test.passed = false
self.test.fatal = tostring(chunk) .. tostring(err)
love.test.test.passed = false
love.test.test.fatal = tostring(chunk) .. tostring(err)
end
end
end)

-- once we've run check delay + eval

-- once called we have a corouting, so just call resume every frame
-- until we have finished
else

-- @TODO use coroutines?
-- if we have a test method that needs a delay
-- we wait for the delay to run out first
if self.delayed ~= nil then
self.delayed.delay = self.delayed.delay - 1
-- re-run the test method again when delay ends
-- its up to the test to handle the :isDelayed() property
if self.delayed.delay <= 0 then
local ok, chunk, err = pcall(self[self.module.module][self.delayed.method], self.test)
if ok == false then
self.test.passed = false
self.test.fatal = tostring(chunk) .. tostring(err)
end
self.delayed = nil
end
else
-- move onto next yield if any
-- pauses can be set with TestMethod:waitFrames(frames)
coroutine.resume(self.test.co)

-- when wait finished (or no yields)
if coroutine.status(self.test.co) == 'dead' then
-- now we're all done evaluate the test
local ok, chunk, err = pcall(self.test.evaluateTest, self.test)
if ok == false then
self.test.passed = false
self.test.fatal = tostring(chunk) .. tostring(err)
end
-- save having to :release() anything we made in the last test
-- 7251ms > 7543ms
collectgarbage("collect")
-- move onto the next test
self.module.index = self.module.index + 1

end

end
Expand All @@ -148,7 +126,7 @@ TestSuite = {
end
end
end
end

end,


Expand Down
Binary file added resources/clickmono.ogg
Binary file not shown.
54 changes: 25 additions & 29 deletions tests/audio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,32 @@ love.test.audio.RecordingDevice = function(test)
if #devices == 0 then
return test:skipTest('cant test this works: no recording devices found')
end
-- test device
if test:isDelayed() == false then
-- check object created and basics
local device = devices[1]
test.store.device = device
test:assertObject(device)
test:assertMatch({1, 2}, device:getChannelCount(), 'check channel count is 1 or 2')
test:assertNotEquals(nil, device:getName(), 'check has name')
-- check initial data is empty as we haven't recorded anything yet
test:assertNotNil(device:getBitDepth())
test:assertEquals(nil, device:getData(), 'check initial data empty')
test:assertEquals(0, device:getSampleCount(), 'check initial sample empty')
test:assertNotNil(device:getSampleRate())
test:assertEquals(false, device:isRecording(), 'check not recording')
-- start recording for a short time
local startrecording = device:start(32000, 4000, 16, 1)
test:assertEquals(true, startrecording, 'check recording started')
test:assertEquals(true, device:isRecording(), 'check now recording')
test:assertEquals(4000, device:getSampleRate(), 'check sample rate set')
test:assertEquals(16, device:getBitDepth(), 'check bit depth set')
test:assertEquals(1, device:getChannelCount(), 'check channel count set')
test:setDelay(20)
-- check object created and basics
local device = devices[1]
test:assertObject(device)
test:assertMatch({1, 2}, device:getChannelCount(), 'check channel count is 1 or 2')
test:assertNotEquals(nil, device:getName(), 'check has name')
-- check initial data is empty as we haven't recorded anything yet
test:assertNotNil(device:getBitDepth())
test:assertEquals(nil, device:getData(), 'check initial data empty')
test:assertEquals(0, device:getSampleCount(), 'check initial sample empty')
test:assertNotNil(device:getSampleRate())
test:assertEquals(false, device:isRecording(), 'check not recording')
-- start recording for a short time
-- @TODO needs delay for VMs
local startrecording = device:start(32000, 4000, 16, 1)
test:waitFrames(10)
test:assertEquals(true, startrecording, 'check recording started')
test:assertEquals(true, device:isRecording(), 'check now recording')
test:assertEquals(4000, device:getSampleRate(), 'check sample rate set')
test:assertEquals(16, device:getBitDepth(), 'check bit depth set')
test:assertEquals(1, device:getChannelCount(), 'check channel count set')
local recording = device:stop()
test:waitFrames(10)
-- after recording
else
local device = test.store.device
local recording = device:stop()
test:assertEquals(false, device:isRecording(), 'check not recording')
test:assertEquals(nil, device:getData(), 'using stop should clear buffer')
test:assertObject(recording)
end
test:assertEquals(false, device:isRecording(), 'check not recording')
test:assertEquals(nil, device:getData(), 'using stop should clear buffer')
test:assertObject(recording)
end


Expand Down
11 changes: 4 additions & 7 deletions tests/graphics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,11 @@ end

-- love.graphics.captureScreenshot
love.test.graphics.captureScreenshot = function(test)
if test:isDelayed() == false then
love.graphics.captureScreenshot('example-screenshot.png')
test:setDelay(10)
love.graphics.captureScreenshot('example-screenshot.png')
test:waitFrames(10)
-- need to wait until end of the frame for the screenshot
else
test:assertNotNil(love.filesystem.openFile('example-screenshot.png', 'r'))
love.filesystem.remove('example-screenshot.png')
end
test:assertNotNil(love.filesystem.openFile('example-screenshot.png', 'r'))
love.filesystem.remove('example-screenshot.png')
end


Expand Down
104 changes: 41 additions & 63 deletions tests/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,25 @@ end

-- love.window.isMaximized
love.test.window.isMaximized = function(test)
if test:isDelayed() == false then
test:assertEquals(false, love.window.isMaximized(), 'check window not maximized')
love.window.maximize()
test:setDelay(10)
else
-- on MACOS maximize wont get recognised immedietely so wait a few frames
test:assertEquals(true, love.window.isMaximized(), 'check window now maximized')
love.window.restore()
end
test:assertEquals(false, love.window.isMaximized(), 'check window not maximized')
love.window.maximize()
test:waitFrames(10)
-- on MACOS maximize wont get recognised immedietely so wait a few frames
test:assertEquals(true, love.window.isMaximized(), 'check window now maximized')
love.window.restore()
end


-- love.window.isMinimized
love.test.window.isMinimized = function(test)
if test:isDelayed() == false then
-- check not minimized to start
test:assertEquals(false, love.window.isMinimized(), 'check window not minimized')
-- try to minimize
love.window.minimize()
test:setDelay(10)
else
-- on linux minimize won't get recognized immediately, so wait a few frames
test:assertEquals(true, love.window.isMinimized(), 'check window minimized')
love.window.restore()
end
-- check not minimized to start
test:assertEquals(false, love.window.isMinimized(), 'check window not minimized')
-- try to minimize
love.window.minimize()
test:waitFrames(10)
-- on linux minimize won't get recognized immediately, so wait a few frames
test:assertEquals(true, love.window.isMinimized(), 'check window minimized')
love.window.restore()
end


Expand Down Expand Up @@ -220,31 +214,25 @@ end

-- love.window.maximize
love.test.window.maximize = function(test)
if test:isDelayed() == false then
test:assertEquals(false, love.window.isMaximized(), 'check window not maximized')
-- check maximizing is set
love.window.maximize()
test:setDelay(10)
else
-- on macos we need to wait a few frames
test:assertEquals(true, love.window.isMaximized(), 'check window maximized')
love.window.restore()
end
test:assertEquals(false, love.window.isMaximized(), 'check window not maximized')
-- check maximizing is set
love.window.maximize()
test:waitFrames(10)
-- on macos we need to wait a few frames
test:assertEquals(true, love.window.isMaximized(), 'check window maximized')
love.window.restore()
end


-- love.window.minimize
love.test.window.minimize = function(test)
if test:isDelayed() == false then
test:assertEquals(false, love.window.isMinimized(), 'check window not minimized')
-- check minimizing is set
love.window.minimize()
test:setDelay(10)
else
-- on linux we need to wait a few frames
test:assertEquals(true, love.window.isMinimized(), 'check window maximized')
love.window.restore()
end
test:assertEquals(false, love.window.isMinimized(), 'check window not minimized')
-- check minimizing is set
love.window.minimize()
test:waitFrames(10)
-- on linux we need to wait a few frames
test:assertEquals(true, love.window.isMinimized(), 'check window maximized')
love.window.restore()
end


Expand All @@ -256,20 +244,13 @@ end

-- love.window.restore
love.test.window.restore = function(test)

-- TODO: for linux runner
-- test doesn't pass because the current test delay system can't wait twice

if test:isDelayed() == false then
-- check minimized to start
love.window.minimize()
love.window.restore()
test:setDelay(10)
else
-- check restoring the state of the window
test:assertEquals(false, love.window.isMinimized(), 'check window restored')
end

-- check minimized to start
love.window.minimize()
test:waitFrames(10)
love.window.restore()
test:waitFrames(10)
-- check restoring the state of the window
test:assertEquals(false, love.window.isMinimized(), 'check window restored')
end


Expand Down Expand Up @@ -327,15 +308,12 @@ end

-- love.window.setPosition
love.test.window.setPosition = function(test)
if test:isDelayed() == false then
-- check position is returned
love.window.setPosition(100, 100, 1)
test:setDelay(10)
else
local x, y, _ = love.window.getPosition()
test:assertEquals(100, x, 'check position x')
test:assertEquals(100, y, 'check position y')
end
-- check position is returned
love.window.setPosition(100, 100, 1)
test:waitFrames(10)
local x, y, _ = love.window.getPosition()
test:assertEquals(100, x, 'check position x')
test:assertEquals(100, y, 'check position y')
end


Expand Down

0 comments on commit 37d4e8c

Please sign in to comment.