Add testGraphics.lua
Fix bug on exactly-zero/exactly-400 ballX
This commit is contained in:
parent
9dc8b10f15
commit
7525daccb6
|
@ -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)
|
||||
|
|
|
@ -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())
|
Loading…
Reference in New Issue