Add tests for brushes

This commit is contained in:
David Capello 2019-04-18 22:34:27 -03:00
parent 585ffc91a4
commit b16a07abc7
3 changed files with 169 additions and 4 deletions

69
scripts/brush.lua Normal file
View File

@ -0,0 +1,69 @@
-- Copyright (C) 2019 Igara Studio S.A.
--
-- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information.
dofile('./test_utils.lua')
do
local b = Brush()
assert(b.type == BrushType.CIRCLE)
assert(b.size == 1)
assert(b.angle == 0)
assert(b.center == Point{ x=0, y=0 })
assert(b.pattern == BrushPattern.NONE)
assert(b.patternOrigin == Point{ x=0, y=0 })
local c = Brush(4)
assert(c.type == BrushType.CIRCLE)
assert(c.size == 4)
assert(c.center == Point{ x=2, y=2 })
end
do
local b = Brush{ type=BrushType.SQUARE, size=4 }
assert(b.type == BrushType.SQUARE)
assert(b.size == 4)
assert(b.angle == 0)
end
do
local b = Brush{ type=BrushType.LINE, size=5, angle=90 }
assert(b.type == BrushType.LINE)
assert(b.size == 5)
assert(b.angle == 90)
end
do
local b = Brush{ type=BrushType.IMAGE, image=Image(4, 8) }
local c = Brush{ image=Image(4, 8) }
assert(b.type == BrushType.IMAGE)
assert(c.type == BrushType.IMAGE)
assert(b.image.width == 4)
assert(b.image.height == 8)
assert(b.pattern == BrushPattern.NONE)
assert(b.patternOrigin.x == 0)
assert(b.patternOrigin.y == 0)
end
do
local rgba = app.pixelColor.rgba
local r = rgba(255, 0, 0)
local g = rgba(0, 128, 0)
local b = rgba(0, 0, 255)
local img = Image(2, 2)
img:putPixel(0, 0, r)
img:putPixel(1, 0, b)
img:putPixel(0, 1, b)
img:putPixel(1, 1, r)
local brush = Brush{ image=img }
expect_img(brush.image, { r, b, b, r })
brush:setFgColor(g)
expect_img(brush.image, { r, g, g, r })
brush:setBgColor(b)
expect_img(brush.image, { b, g, g, b })
end

29
scripts/test_utils.lua Normal file
View File

@ -0,0 +1,29 @@
-- Copyright (C) 2019 Igara Studio S.A.
--
-- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information.
function expect_eq(a, b)
if a ~= b then
print(debug.traceback())
print('Expected A == B but:')
print(' - Value A = ' .. tostring(a))
print(' - Value B = ' .. tostring(b))
assert(a == b)
end
end
function expect_img(image, expectedPixels)
local w = image.width
local h = image.height
for y=0,h-1 do
for x=0,w-1 do
local value = image:getPixel(x, y)
local expected = expectedPixels[1+y*w+x]
if value ~= expected then
print('In pixel (' .. x .. ', ' .. y .. '):')
expect_eq(value, expected)
end
end
end
end

View File

@ -3,6 +3,8 @@
-- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information.
dofile('./test_utils.lua')
----------------------------------------------------------------------
-- activeTool
----------------------------------------------------------------------
@ -15,6 +17,11 @@ assert(app.activeTool.id == 'line')
app.activeTool = pencil
assert(app.activeTool.id == 'pencil')
-- default brush is a circle of 1x1 when there is no UI
assert(app.activeBrush.type == BrushType.CIRCLE)
assert(app.activeBrush.size == 1)
assert(app.activeBrush.angle == 0)
----------------------------------------------------------------------
-- create sprite for testing
----------------------------------------------------------------------
@ -69,7 +76,6 @@ do
assert(cel.image.height == 4)
for v=0,3 do
for u=0,3 do
print(u, v, cel.image:getPixel(u, v), expected[1+v*4+u])
assert(cel.image:getPixel(u, v) == expected[1+v*4+u])
end
end
@ -94,7 +100,6 @@ do
assert(cel.image.height == 4)
for v=0,3 do
for u=0,3 do
print(u, v, cel.image:getPixel(u, v), expected[1+v*4+u])
assert(cel.image:getPixel(u, v) == expected[1+v*4+u])
end
end
@ -121,7 +126,6 @@ do
assert(cel.image.height == 4)
for v=0,3 do
for u=0,3 do
print(u, v, cel.image:getPixel(u, v), expected[1+v*4+u])
assert(cel.image:getPixel(u, v) == expected[1+v*4+u])
end
end
@ -149,7 +153,6 @@ do
assert(cel.image.height == 4)
for v=0,3 do
for u=0,3 do
print(u, v, cel.image:getPixel(u, v), expected[1+v*4+u])
assert(cel.image:getPixel(u, v) == expected[1+v*4+u])
end
end
@ -199,3 +202,67 @@ do
assert(fgCel1.image:getPixel(0, 0) == yellow.rgbaPixel)
assert(fgCel2.image:getPixel(0, 0) == yellow.rgbaPixel)
end
----------------------------------------------------------------------
-- draw with brushes
----------------------------------------------------------------------
do
local expectedImages = {
{ 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 },
{ 0, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 },
{ 0, 0, 0, 0,
0, 2, 2, 0,
0, 2, 2, 0,
0, 0, 0, 0 },
{ 3, 3, 3, 0,
3, 3, 3, 3,
3, 3, 3, 3,
0, 3, 3, 3 }
}
local s = Sprite(4, 4, ColorMode.INDEXED)
assert(s == app.activeSprite)
assert(s.cels[1] == app.activeCel)
function expect_cel_is_image(imageIndex)
local a = Image(s.spec)
a:drawSprite(s, 1, Point(0, 0))
local b = expectedImages[imageIndex]
expect_img(a, b)
end
expect_cel_is_image(1)
app.useTool{ tool='pencil', color=1, points={ Point(1, 1) } }
assert(#s.cels == 1)
expect_cel_is_image(2)
app.undo()
expect_cel_is_image(1)
app.useTool{ tool='pencil',
brush=Brush{ size=2, type=BrushType.SQUARE },
color=2, points={ Point(2, 2) } }
expect_cel_is_image(3)
app.undo()
expect_cel_is_image(1)
app.useTool{ tool='pencil',
brush=Brush{ size=2, type=BrushType.SQUARE, center=Point(0, 0) },
color=2, points={ Point(1, 1) } }
expect_cel_is_image(3)
app.undo()
expect_cel_is_image(1)
app.useTool{ tool='line',
brush={ size=3, type=BrushType.SQUARE },
color=3, points={ Point(1, 1), Point(2, 2) } }
expect_cel_is_image(4)
app.undo()
end