From 7525daccb67465757208d4344fca1a4a3b97914a Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 23 Feb 2025 11:03:10 -0500 Subject: [PATCH] Add testGraphics.lua Fix bug on exactly-zero/exactly-400 ballX --- src/graphics.lua | 2 +- src/test/testGraphics.lua | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/test/testGraphics.lua diff --git a/src/graphics.lua b/src/graphics.lua index c9e741e..d776cf5 100644 --- a/src/graphics.lua +++ b/src/graphics.lua @@ -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) diff --git a/src/test/testGraphics.lua b/src/test/testGraphics.lua new file mode 100644 index 0000000..0adfd75 --- /dev/null +++ b/src/test/testGraphics.lua @@ -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())