add unit test for piskel MergeUtils

This commit is contained in:
juliandescottes 2017-05-09 00:33:51 +02:00 committed by Julian Descottes
parent 92934502e8
commit 80a1edf027
2 changed files with 154 additions and 1 deletions

View File

@ -12,7 +12,7 @@
* - index: {Number} index where the new frames should be appended
* - resize: {String} either "expand" or "keep"
* - origin: {String} can be any of the existing AnchorWidget origins.
* - addFrames: {Boolean} true if all the imported frames should be added as new frames.
* - insertMode: {String} either "insert" or "add"
*
* @return {Piskel} The new Piskel instance created
*/

View File

@ -0,0 +1,153 @@
describe("MergeUtils suite", function() {
var B = '#000000';
var R = '#ff0000';
var T = Constants.TRANSPARENT_COLOR;
var createPiskelFromGrid = function (grid, name) {
var frame = pskl.model.Frame.fromPixelGrid(grid);
var layer = pskl.model.Layer.fromFrames("l1", [frame]);
return pskl.model.Piskel.fromLayers([layer], 12, {
name: name || "piskel",
description: "desc"
});
};
/**
* Simple helper to create a monochrome sprite for the provided color,
* number of rows and columns.
*/
var getPiskel = function (color, rows, cols) {
var grid = [];
for (var i = 0 ; i < rows ; i++) {
grid[i] = [];
for (var j = 0 ; j < cols ; j++) {
grid[i][j] = color;
}
}
return createPiskelFromGrid(grid);
};
it("merges 2 piskel - insertMode:add same size", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 2, 2);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "add"
});
expect(mergedPiskel.getWidth()).toBe(2);
expect(mergedPiskel.getHeight()).toBe(2);
expect(mergedPiskel.getLayers().length).toBe(2);
expect(mergedPiskel.getLayers()[0].getFrames().length).toBe(2);
});
it("merges 2 piskel - insertMode:insert same size", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 2, 2);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(2);
expect(mergedPiskel.getHeight()).toBe(2);
expect(mergedPiskel.getLayers().length).toBe(2);
expect(mergedPiskel.getLayers()[0].getFrames().length).toBe(1);
});
it("merges 2 piskel - resize:expand with bigger imported piskel", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 4, 4);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(4);
expect(mergedPiskel.getHeight()).toBe(4);
});
it("merges 2 piskel - resize:keep with bigger imported piskel", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 4, 4);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "keep",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(2);
expect(mergedPiskel.getHeight()).toBe(2);
});
it("merges 2 piskel - resize:expand with taller but thinner imported piskel", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 1, 4);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(2);
expect(mergedPiskel.getHeight()).toBe(4);
});
it("merges 2 piskel - resize:expand with wider but shorter imported piskel", function () {
var piskel1 = getPiskel(B, 2, 2);
var piskel2 = getPiskel(R, 4, 1);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(4);
expect(mergedPiskel.getHeight()).toBe(2);
});
it("merges 2 piskel - resize:expand with bigger original piskel", function () {
var piskel1 = getPiskel(B, 3, 3);
var piskel2 = getPiskel(R, 1, 1);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "expand",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(3);
expect(mergedPiskel.getHeight()).toBe(3);
});
it("merges 2 piskel - resize:keep with bigger original piskel", function () {
var piskel1 = getPiskel(B, 3, 3);
var piskel2 = getPiskel(R, 1, 1);
var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, {
index: 0,
resize: "keep",
origin: "TOPLEFT",
insertMode: "insert"
});
expect(mergedPiskel.getWidth()).toBe(3);
expect(mergedPiskel.getHeight()).toBe(3);
});
});