Add custom properties serialization/deserialization lua tests

This commit is contained in:
Martín Capello 2023-01-06 15:47:38 -03:00 committed by David Capello
parent d590d3f39a
commit 6534d37b40
4 changed files with 223 additions and 1 deletions

View File

@ -32,7 +32,7 @@ function expect_img(image, expectedPixels)
local h = image.height
if w*h ~= #expectedPixels then
print(debug.traceback())
print('Expected pixels: #=' .. #expectedPixels)
print('Expected pixels: #=' .. #expectedPixels)
print('Image size: w=' .. w .. ' h=' .. h .. ' #=' .. w*h)
dump_img(image)
assert(w*h == #expectedPixels)
@ -132,3 +132,126 @@ function expect_rendered_layers(expectedImage, sprite, layerNames, frame)
error("render doesn't match to the expected image")
end
end
-- Asserts that the passed sprites are equals without taking
-- into account:
-- * User data custom properties.
-- * Filename.
-- * Pixel ratio.
-- * Grid bounds.
function assert_sprites_eq(expectedSprite, sprite)
assert(expectedSprite.width == sprite.width)
assert(expectedSprite.height == sprite.height)
assert(expectedSprite.colorMode == sprite.colorMode)
assert(expectedSprite.transparentColor == sprite.transparentColor)
assert_layers_eq(expectedSprite.layers, sprite.layers)
assert_frames_eq(expectedSprite.frames, sprite.frames)
assert_tags_eq(expectedSprite.tags, sprite.tags)
assert_slices_eq(expectedSprite.slices, sprite.slices)
end
function assert_layers_eq(expectedLayers, layers)
assert(#expectedLayers == #layers)
for i = 1,#layers do
assert_layer_eq(expectedLayers[i], layers[i])
end
end
function assert_layer_eq(expectedLayer, layer)
assert(expectedLayer.name == layer.name)
assert(expectedLayer.opacity == layer.opacity)
assert(expectedLayer.blendMode == layer.blendMode)
assert(expectedLayer.stackIndex == layer.stackIndex)
assert(expectedLayer.isGroup == layer.isGroup)
assert(expectedLayer.isImage == layer.isImage)
assert(expectedLayer.isTransparent == layer.isTransparent)
assert(expectedLayer.isGroup == layer.isGroup)
assert(expectedLayer.isBackground == layer.isBackground)
assert(expectedLayer.isContinuous == layer.isContinuous)
assert(expectedLayer.isReference == layer.isReference)
assert(expectedLayer.color == layer.color)
assert(expectedLayer.data == layer.data)
if expectedLayer.isGroup then
assert_layers_eq(expectedLayer.layers, layer.layers);
else
assert_cels_eq(expectedLayer.cels, layer.cels)
end
end
function assert_frames_eq(expectedFrames, frames)
assert(#expectedFrames == #frames)
for i = 1,#frames do
assert(expectedFrames[i].frameNumber == frames[i].frameNumber)
assert(expectedFrames[i].duration == frames[i].duration)
end
end
function assert_cels_eq(expectedCels, cels)
assert(#expectedCels == #cels)
for i = 1,#cels do
local expCel = expectedCels[i]
local cel = cels[i]
assert(expCel.frameNumber == cel.frameNumber)
assert(expCel.bounds.x == cel.bounds.x)
assert(expCel.bounds.y == cel.bounds.y)
assert(expCel.bounds.width == cel.bounds.width)
assert(expCel.bounds.height == cel.bounds.height)
assert(expCel.position.x == cel.position.x)
assert(expCel.position.y == cel.position.y)
assert(expCel.opacity == cel.opacity)
assert(expCel.color == cel.color)
assert(expCel.data == cel.data)
assert(expCel.image:isEqual(cel.image))
end
end
function assert_tags_eq(expectedTags, tags)
assert(#expectedTags == #tags)
for i = 1,#tags do
local expTag = expectedTags[i]
local tag = tags[i]
local a = expTag.fromFrame
local b = tag.fromFrame
assert(expTag.fromFrame.frameNumber == tag.fromFrame.frameNumber)
assert(expTag.toFrame.frameNumber == tag.toFrame.frameNumber)
assert(expTag.name == tag.name)
assert(expTag.aniDir == tag.aniDir)
assert(expTag.color == tag.color)
assert(expTag.data == tag.data)
end
end
function assert_slices_eq(expectedSlices, slices)
assert(#expectedSlices == #slices)
for i = 1,#slices do
local expSlice = expectedSlices[i]
local slice = slices[i]
assert(expSlice.bounds.x == slice.bounds.x)
assert(expSlice.bounds.y == slice.bounds.y)
assert(expSlice.bounds.width == slice.bounds.width)
assert(expSlice.bounds.height == slice.bounds.height)
assert(expSlice.color == slice.color)
assert(expSlice.data == slice.data)
assert(expSlice.name == slice.name)
if expSlice.center == nil or slice.center == nil then
assert(expSlice.center == slice.center)
else
assert(expSlice.center.x == slice.center.x)
assert(expSlice.center.y == slice.center.y)
assert(expSlice.center.width == slice.center.width)
assert(expSlice.center.height == slice.center.height)
end
if expSlice.pivot == nil or slice.pivot == nil then
assert(expSlice.pivot == slice.pivot)
else
assert(expSlice.pivot.x == slice.pivot.x)
assert(expSlice.pivot.y == slice.pivot.y)
end
end
end

View File

@ -0,0 +1,97 @@
-- Copyright (C) 2023 Igara Studio S.A.
--
-- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information.
--
-- Codification and decodification of aseprite files with user data properties.
--
dofile('./test_utils.lua')
do
local spr = Sprite{ fromFile="sprites/file-tests-props.aseprite" }
-- Set sprite custom properties
spr.properties.a = true
spr.properties.b = 1
spr.properties.c = "hi"
spr.properties.d = 2.3
spr.properties("ext").a = {"one", "two", "three"}
-- Set layer custom properties
spr.layers[2].properties.a = "i'm the layer 3"
spr.layers[2].properties("ext").a = 100
-- Set tileset custom properties
spr.layers[1].tileset.properties.a = "i'm a tilemap"
spr.layers[1].tileset.properties.b = 11
spr.layers[1].tileset.properties("ext").a = "text from extension"
-- Set tags custom properties
spr.tags[1].properties.a = Point(1,2)
spr.tags[1].properties.b = {a="text",b=35.567,c=Rectangle(1,2,3,4)}
spr.tags[1].properties("ext").a = {a=Size(50,60),b=43985943434}
-- Set cels custom properties
spr.layers[4].cels[4].properties = {a={1,2,3}, b={a="4",b="5",c="6"}}
spr.layers[4].cels[4].properties("ext").a = {"a","b","c"}
-- Set slices custom properties
spr.slices[1].properties = {a=Point(3,4), b=Size(10,20)}
spr.slices[1].properties("ext", {a=Rectangle(10,20,30,40)})
spr:saveAs("_test_userdata_codec_1.aseprite")
spr:close()
local origSpr = Sprite{ fromFile="sprites/file-tests-props.aseprite" }
spr = Sprite{ fromFile="_test_userdata_codec_1.aseprite" }
assert_sprites_eq(origSpr, spr)
origSpr:close()
assert(#spr.properties == 4)
assert(#spr.properties("ext") == 1)
assert(spr.properties.a == true)
assert(spr.properties.b == 1)
assert(spr.properties.c == "hi")
assert(spr.properties.d == 2.3)
assert(spr.properties("ext").a[1] == "one")
assert(spr.properties("ext").a[2] == "two")
assert(spr.properties("ext").a[3] == "three")
assert(#spr.layers[2].properties == 1)
assert(#spr.layers[2].properties("ext") == 1)
assert(spr.layers[2].properties.a == "i'm the layer 3")
assert(spr.layers[2].properties("ext").a == 100)
assert(#spr.layers[1].tileset.properties == 2)
assert(#spr.layers[1].tileset.properties("ext") == 1)
assert(spr.layers[1].tileset.properties.a == "i'm a tilemap")
assert(spr.layers[1].tileset.properties.b == 11)
assert(spr.layers[1].tileset.properties("ext").a == "text from extension")
assert(#spr.tags[1].properties == 2)
assert(#spr.tags[1].properties("ext") == 1)
assert(spr.tags[1].properties.a.x == 1)
assert(spr.tags[1].properties.a.y == 2)
assert(spr.tags[1].properties.b.a == "text")
assert(spr.tags[1].properties.b.b == 35.567)
assert(spr.tags[1].properties.b.c.x == 1)
assert(spr.tags[1].properties.b.c.y == 2)
assert(spr.tags[1].properties.b.c.width == 3)
assert(spr.tags[1].properties.b.c.height == 4)
assert(spr.tags[1].properties("ext").a.a.width == 50)
assert(spr.tags[1].properties("ext").a.a.height == 60)
assert(spr.tags[1].properties("ext").a.b == 43985943434)
assert(#spr.layers[4].cels[4].properties == 2)
assert(#spr.layers[4].cels[4].properties("ext") == 1)
assert(spr.layers[4].cels[4].properties.a[1] == 1)
assert(spr.layers[4].cels[4].properties.a[2] == 2)
assert(spr.layers[4].cels[4].properties.a[3] == 3)
assert(spr.layers[4].cels[4].properties.b.a == "4")
assert(spr.layers[4].cels[4].properties.b.b == "5")
assert(spr.layers[4].cels[4].properties.b.c == "6")
assert(spr.layers[4].cels[4].properties("ext").a[1] == "a")
assert(spr.layers[4].cels[4].properties("ext").a[2] == "b")
assert(spr.layers[4].cels[4].properties("ext").a[3] == "c")
assert(#spr.slices[1].properties == 2)
assert(#spr.slices[1].properties("ext") == 1)
assert(spr.slices[1].properties.a.x == 3)
assert(spr.slices[1].properties.a.y == 4)
assert(spr.slices[1].properties.b.width == 10)
assert(spr.slices[1].properties.b.height == 20)
assert(spr.slices[1].properties("ext").a.x == 10)
assert(spr.slices[1].properties("ext").a.y == 20)
assert(spr.slices[1].properties("ext").a.width == 30)
assert(spr.slices[1].properties("ext").a.height == 40)
end

View File

@ -25,3 +25,5 @@
merged in the same texture atlas.
* `2f-index-3x3.aseprite`: Indexed, 2 frames, 1 layer, mask color set
to index 21.
* `file-tests-props.aseprite`: Indexed, 64x64, 6 frames, 4 layers (one
of them is a tilemap), 13 cels, 1 tag.

Binary file not shown.