Skip to content

Commit

Permalink
[lua] Test Image:flip() with sprite and without sprite (aseprite#3854)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed May 18, 2023
1 parent ccc5780 commit 637d71a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/app/script/image_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "app/util/autocrop.h"
#include "app/util/resize_image.h"
#include "base/fs.h"
#include "doc/algorithm/flip_image.h"
#include "doc/algorithm/flip_type.h"
#include "doc/algorithm/shrink_bounds.h"
#include "doc/cel.h"
Expand Down Expand Up @@ -585,12 +586,17 @@ int Image_flip(lua_State* L)
auto obj = get_obj<ImageObj>(L, 1);
doc::Image* img = obj->image(L);
doc::algorithm::FlipType flipType = doc::algorithm::FlipType::FlipHorizontal;
if (lua_isinteger(L, 2) && lua_tointeger(L, 2) > 0)
flipType = doc::algorithm::FlipType::FlipVertical;
if (lua_isinteger(L, 2))
flipType = (doc::algorithm::FlipType)lua_tointeger(L, 2);

Tx tx;
tx(new cmd::FlipImage(img, img->bounds(), flipType));
tx.commit();
if (obj->cel(L) == nullptr) {
doc::algorithm::flip_image(img, img->bounds(), flipType);
}
else {
Tx tx;
tx(new cmd::FlipImage(img, img->bounds(), flipType));
tx.commit();
}
return 0;
}

Expand Down
14 changes: 11 additions & 3 deletions tests/scripts/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ do
end

-- Tests for Image:flip()
do
local img = Image(3, 3)
function test_image_flip(img)
local r = Color(255, 0, 0).rgbaPixel
local g = Color(0, 255, 0).rgbaPixel
img:clear(0)
Expand All @@ -420,6 +419,10 @@ do
expect_img(img, { 0, 0, g,
0, r, 0,
r, 0, 0 })

-- Without sprite, don't test undo
if not app.sprite then return end

app.undo()
expect_img(img, { g, 0, 0,
0, r, 0,
Expand All @@ -442,4 +445,9 @@ do
expect_img(img, { g, 0, 0,
0, r, 0,
0, 0, r })
end
end

local spr = Sprite(3, 3) -- Test with sprite (with transactions & undo/redo)
test_image_flip(app.image)
app.sprite = nil -- Test without sprite (without transactions)
test_image_flip(Image(3, 3))

0 comments on commit 637d71a

Please sign in to comment.