From d1307584e541bdacec377ffc3e1f2df17aed8cb9 Mon Sep 17 00:00:00 2001 From: ell <77150506+ellraiser@users.noreply.github.com> Date: Thu, 16 Nov 2023 23:28:50 +0000 Subject: [PATCH] add graphics pixel comparison tests - added an automatic check for expected vs actual with the images already shown in the report - used a 1px tolerance for now for each of the pixels checked --- testing/classes/TestMethod.lua | 52 ++++++- .../expected/love.test.graphics.Canvas-1.png | Bin 0 -> 92 bytes .../expected/love.test.graphics.Canvas-2.png | Bin 0 -> 92 bytes .../love.test.graphics.setColor-1.png | Bin 128 -> 110 bytes testing/readme.md | 18 ++- testing/tests/graphics.lua | 135 +++++++++--------- 6 files changed, 122 insertions(+), 83 deletions(-) create mode 100644 testing/output/expected/love.test.graphics.Canvas-1.png create mode 100644 testing/output/expected/love.test.graphics.Canvas-2.png diff --git a/testing/classes/TestMethod.lua b/testing/classes/TestMethod.lua index a89ba2035..023bae2bd 100644 --- a/testing/classes/TestMethod.lua +++ b/testing/classes/TestMethod.lua @@ -260,11 +260,57 @@ TestMethod = { end, - -- @method - TestMethod:exportImg() - -- @desc - used to export actual test img results to compare to the expected + -- @method - TestMethod:compareImg() + -- @desc - compares a given image to the 'expected' version, with a tolerance of + -- 1px in any direction, and then saves it as the 'actual' version for + -- report viewing -- @param {table} imgdata - imgdata to save as a png -- @return {nil} - exportImg = function(self, imgdata) + compareImg = function(self, imgdata) + local expected = love.image.newImageData( + 'tempoutput/expected/love.test.graphics.' .. self.method .. '-' .. + tostring(self.imgs) .. '.png' + ) + local iw = imgdata:getWidth()-2 + local ih = imgdata:getHeight()-2 + for ix=2,iw do + for iy=2,ih do + local ir, ig, ib, ia = imgdata:getPixel(ix, iy) + local tolerance = { + {expected:getPixel(ix, iy)}, + {expected:getPixel(ix+1, iy+1)}, + {expected:getPixel(ix+1, iy)}, + {expected:getPixel(ix+1, iy-1)}, + {expected:getPixel(ix, iy+1)}, + {expected:getPixel(ix, iy-1)}, + {expected:getPixel(ix-1, iy+1)}, + {expected:getPixel(ix-1, iy)}, + {expected:getPixel(ix-1, iy-1)} + } + local has_match_r = false + local has_match_g = false + local has_match_b = false + local has_match_a = false + for t=1,9 do + if ir == tolerance[t][1] then has_match_r = true; end + if ig == tolerance[t][2] then has_match_g = true; end + if ib == tolerance[t][3] then has_match_b = true; end + if ia == tolerance[t][4] then has_match_a = true; end + end + local matching = has_match_r and has_match_g and has_match_b and has_match_a + local ymatch = '' + local nmatch = '' + if has_match_r then ymatch = ymatch .. 'r' else nmatch = nmatch .. 'r' end + if has_match_g then ymatch = ymatch .. 'g' else nmatch = nmatch .. 'g' end + if has_match_b then ymatch = ymatch .. 'b' else nmatch = nmatch .. 'b' end + if has_match_a then ymatch = ymatch .. 'a' else nmatch = nmatch .. 'a' end + local pixel = tostring(ir)..','..tostring(ig)..','..tostring(ib)..','..tostring(ia) + self:assertEquals(true, matching, 'compare image pixel (' .. pixel .. ') at ' .. + tostring(ix) .. ',' .. tostring(iy) .. ', matching = ' .. ymatch .. + ', not matching = ' .. nmatch .. ' (' .. self.method .. ')' + ) + end + end local path = 'tempoutput/actual/love.test.graphics.' .. self.method .. '-' .. tostring(self.imgs) .. '.png' imgdata:encode('png', path) diff --git a/testing/output/expected/love.test.graphics.Canvas-1.png b/testing/output/expected/love.test.graphics.Canvas-1.png new file mode 100644 index 0000000000000000000000000000000000000000..3727aa3a00f18fefc4a9b162397da721e7142619 GIT binary patch literal 92 zcmeAS@N?(olHy`uVBq!ia0vp^DImwg9Y$w!>#K(2_Vi(^Q| moa6)v*2M`TJxzfQ{tOJP6%33Ve_fk_@(iA?elF{r5}E+<9TOY? literal 0 HcmV?d00001 diff --git a/testing/output/expected/love.test.graphics.Canvas-2.png b/testing/output/expected/love.test.graphics.Canvas-2.png new file mode 100644 index 0000000000000000000000000000000000000000..3727aa3a00f18fefc4a9b162397da721e7142619 GIT binary patch literal 92 zcmeAS@N?(olHy`uVBq!ia0vp^DImwg9Y$w!>#K(2_Vi(^Q| moa6)v*2M`TJxzfQ{tOJP6%33Ve_fk_@(iA?elF{r5}E+<9TOY? literal 0 HcmV?d00001 diff --git a/testing/output/expected/love.test.graphics.setColor-1.png b/testing/output/expected/love.test.graphics.setColor-1.png index 4b1036b2926606064f11abc8f9de0f08d616b528..3062177c17e05198ba34977cffd5de027f562e89 100644 GIT binary patch delta 72 zcmZo*%$uO&^#4BtLp=in1CV04!j;Jiq@+Au978JRBqua5I%*^YG%>LyYa|pjGFcfg Y++HNJQFQ**zd%(Cp00i_>zopr00-k0_W%F@ delta 90 zcmd06V4R>6!oa}r|33pmJ&4Y_ImZ%6>v+02hE&W+P7o0h5MdGG>1uLnYI0~g7!VK< pAmCwO!PUjV#l*?xsIh>VnPH