From 81599fed93f0a7d2a1c23955d2413045e367e1d6 Mon Sep 17 00:00:00 2001 From: ell <77150506+ellraiser@users.noreply.github.com> Date: Wed, 6 Mar 2024 09:18:44 +0000 Subject: [PATCH 1/2] small update - removed assertPixels() - added exportImg() to help graphics test writing (sets the "expected" image for the test) - added "deflate" variants to love.data.compress() --- testing/classes/TestMethod.lua | 51 ++--- .../expected/love.test.graphics.scale-1.png | Bin 0 -> 84 bytes testing/readme.md | 1 - testing/tests/data.lua | 9 +- testing/tests/graphics.lua | 207 +----------------- 5 files changed, 29 insertions(+), 239 deletions(-) create mode 100644 testing/output/expected/love.test.graphics.scale-1.png diff --git a/testing/classes/TestMethod.lua b/testing/classes/TestMethod.lua index 2061a8187..a90e6921e 100644 --- a/testing/classes/TestMethod.lua +++ b/testing/classes/TestMethod.lua @@ -123,38 +123,6 @@ TestMethod = { end, - -- @method - TestMethod:assertPixels() - -- @desc - checks a list of coloured pixels agaisnt given imgdata - -- @param {ImageData} imgdata - image data to check - -- @param {table} pixelchecks - map of colors to list of pixel coords, i.e. - -- { blue = { {1, 1}, {2, 2}, {3, 4} } } - -- @return {nil} - assertPixels = function(self, imgdata, pixelchecks, label) - for i, v in pairs(pixelchecks) do - local col = self.colors[i] - local pixels = v - for p=1,#pixels do - local coord = pixels[p] - local tr, tg, tb, ta = imgdata:getPixel(coord[1], coord[2]) - local compare_id = tostring(coord[1]) .. ',' .. tostring(coord[2]) - -- prevent us getting stuff like 0.501960785 for 0.5 red - tr = math.floor((tr*10)+0.5)/10 - tg = math.floor((tg*10)+0.5)/10 - tb = math.floor((tb*10)+0.5)/10 - ta = math.floor((ta*10)+0.5)/10 - col[1] = math.floor((col[1]*10)+0.5)/10 - col[2] = math.floor((col[2]*10)+0.5)/10 - col[3] = math.floor((col[3]*10)+0.5)/10 - col[4] = math.floor((col[4]*10)+0.5)/10 - self:assertEquals(col[1], tr, 'check pixel r for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')') - self:assertEquals(col[2], tg, 'check pixel g for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')') - self:assertEquals(col[3], tb, 'check pixel b for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')') - self:assertEquals(col[4], ta, 'check pixel a for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')') - end - end - end, - - -- @method - TestMethod:assertRange() -- @desc - used to check a value is within an expected range -- @param {number} actual - actual value of the test @@ -350,13 +318,30 @@ TestMethod = { ) end end - local path = 'tempoutput/actual/love.test.graphics.' .. + local path = 'tempoutput/actual/love.test.graphics.' .. self.method .. '-' .. tostring(self.imgs) .. '.png' imgdata:encode('png', path) self.imgs = self.imgs + 1 end, + -- @method - TestMethod:exportImg() + -- @desc - exports the given imgdata to the 'output/expected/' folder, to use when + -- writing new graphics tests to set the expected image output + -- @NOTE - you should not leave this method in when you are finished this is + -- for test writing only + -- @param {table} imgdata - imgdata to save as a png + -- @param {integer} imgdata - index of the png, graphic tests are run sequentially + -- and each test image is numbered in order that its + -- compared to, so set the number here to match + -- @return {nil} + exportImg = function(self, imgdata, index) + local path = 'tempoutput/expected/love.test.graphics.' .. + self.method .. '-' .. tostring(index) .. '.png' + imgdata:encode('png', path) + end, + + -- @method - TestMethod:skipTest() -- @desc - used to mark this test as skipped for a specific reason -- @param {string} reason - reason why method is being skipped diff --git a/testing/output/expected/love.test.graphics.scale-1.png b/testing/output/expected/love.test.graphics.scale-1.png new file mode 100644 index 0000000000000000000000000000000000000000..a97348e509ee00fea8cf1432a82a5b89df82c91a GIT binary patch literal 84 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!D3?x-;bCrM;bAV5X>wg9Y$w!>#KrWA`i(^Q| doa6)rULenafl;h;) Date: Wed, 6 Mar 2024 09:41:57 +0000 Subject: [PATCH 2/2] handle missing expected img for compareImg() --- testing/classes/TestMethod.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/testing/classes/TestMethod.lua b/testing/classes/TestMethod.lua index a90e6921e..1f336d06b 100644 --- a/testing/classes/TestMethod.lua +++ b/testing/classes/TestMethod.lua @@ -270,10 +270,11 @@ TestMethod = { -- @param {table} imgdata - imgdata to save as a png -- @return {nil} compareImg = function(self, imgdata) - local expected = love.image.newImageData( - 'tempoutput/expected/love.test.graphics.' .. self.method .. '-' .. - tostring(self.imgs) .. '.png' - ) + local expected_path = 'tempoutput/expected/love.test.graphics.' .. + self.method .. '-' .. tostring(self.imgs) .. '.png' + local ok, chunk, _ = pcall(love.image.newImageData, expected_path) + if ok == false then return self:assertEquals(true, false, chunk) end + local expected = chunk local iw = imgdata:getWidth()-2 local ih = imgdata:getHeight()-2 local rgba_tolerance = self.rgba_tolerance * (1/255)