Add testGraphics.lua

Fix bug on exactly-zero/exactly-400 ballX
This commit is contained in:
Sage Vaillancourt 2025-02-23 11:03:10 -05:00
parent 9dc8b10f15
commit 7525daccb6
2 changed files with 38 additions and 1 deletions

View File

@ -10,7 +10,7 @@ function getDrawOffset(ballX, ballY)
-- Keep the ball approximately in the center, once it's past C.Center.y - 30
offsetY = math.max(0, (-1 * ballY) + C.Center.y - 30)
if ballX > 0 and ballX < C.Screen.W then
if ballX >= 0 and ballX <= C.Screen.W then
offsetX = 0
elseif ballX < 0 then
offsetX = math.max(-1 * C.Screen.W, ballX * -1)

37
src/test/testGraphics.lua Normal file
View File

@ -0,0 +1,37 @@
require("test/setup")
require("graphics")
local function assertSmallDifference(previous, current, ballValue, ballLabel)
local difference = math.abs(previous - current)
local baseError = "Expected a small difference, but received a difference of "
local fullDetails = luaunit.prettystr({ previous = previous, current = current, [ballLabel] = ballValue })
luaunit.assertIsTrue(difference < 2, baseError .. difference .. ":\n " .. luaunit.prettystr(fullDetails))
end
function testNoJumpsInYOffset()
local startY = -240 * 3
local atXValues = { -400, 0, 400 }
for _, xValue in ipairs(atXValues) do
local _, lastYOffset = getDrawOffset(xValue, startY)
for ballY = startY, 240 do
local _, currentYOffset = getDrawOffset(xValue, ballY)
assertSmallDifference(lastYOffset, currentYOffset, ballY, "ballY")
lastYOffset = currentYOffset
end
end
end
function testNoJumpsInXOffset()
local startX = -800
local atYValues = { 240, 0, -240 }
for _, yValue in ipairs(atYValues) do
local lastXOffset = getDrawOffset(startX, yValue)
for ballX = startX, 800 do
local currentXOffset = getDrawOffset(ballX, yValue)
assertSmallDifference(lastXOffset, currentXOffset, ballX, "ballX")
lastXOffset = currentXOffset
end
end
end
os.exit(luaunit.LuaUnit.run())