8000 Fix "Out of the bounds" error by eGavr · Pull Request #592 · gemini-testing/gemini · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/image.js → lib/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const q = require('q');
const looksSame = require('looks-same');
const PngImg = require('png-img');
const utils = require('png-img/utils');
const SafeRect = require('./safe-rect');

class Image {
constructor(buffer) {
Expand All @@ -13,13 +14,16 @@ class Image {
crop(rect, opts) {
rect = this._scale(rect, (opts || {}).scaleFactor);
const imageSize = this.getSize();
const safeRect = SafeRect.create(rect, imageSize);

this._img.crop(
Math.max(rect.left, 0),
Math.max(rect.top, 0),
Math.min(imageSize.width, rect.width),
Math.min(imageSize.height, rect.height)
safeRect.left,
safeRect.top,
safeRect.width,
safeRect.height
);
return q.resolve(this);

return q(this);
}

getSize() {
Expand Down
38 changes: 38 additions & 0 deletions lib/image/safe-rect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

module.exports = class SafeRect {
static create(rect, imageSize) {
return new SafeRect(rect, imageSize);
}

constructor(rect, imageSize) {
this._rect = rect;
this._imageSize = imageSize;
}

get left() {
return this._calcCoord('left');
}

get top() {
return this._calcCoord('top');
}

_calcCoord(coord) {
return Math.max(this._rect[coord], 0);
}

get width() {
return this._calcSize('width', 'left');
}

get height() {
return this._calcSize('height', 'top');
}

_calcSize(size, coord) {
const rectCoord = this._calcCoord(coord);

return Math.min(this._rect[size], this._imageSize[size] - rectCoord);
}
};
52 changes: 40 additions & 12 deletions test/unit/image.test.js → test/unit/image/index.js
8000
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const looksSameStub = sinon.stub();
const Image = proxyquire('lib/image', {
'looks-same': looksSameStub
});
const SafeRect = require('lib/image/safe-rect');

describe('unit-image', () => {
describe('Image', () => {
const sandbox = sinon.sandbox.create();
let image;

Expand All @@ -26,20 +27,47 @@ describe('unit-image', () => {
sandbox.stub(PngImg.prototype, 'size').returns({width: 100500, height: 500100});
});

afterEach(() => {
sandbox.restore();
});
afterEach(() => sandbox.restore());

it('should crop image', () => {
sandbox.stub(PngImg.prototype, 'crop');
describe('crop', () => {
beforeEach(() => {
sandbox.stub(SafeRect, 'create').returns({});

const rect = {top: 1, left: 2, width: 3, height: 4};
sandbox.stub(PngImg.prototype, 'crop');
});

return image.crop(rect)
.then(() => {
assert.calledOnce(PngImg.prototype.crop);
assert.calledWith(PngImg.prototype.crop, 2, 1, 3, 4);
});
it('should create instance of "SafeRect"', () => {
const rect = {left: 1, top: 2, width: 3, height: 4};

return image.crop(rect)
.then(() => {
assert.calledOnce(SafeRect.create);
assert.calledWith(SafeRect.create, rect, image.getSize());
});
});

it('should consider scale factor before creating of instance of "SafeRect"', () => {
const rect = {left: 1, top: 2, width: 3, height: 4};
const scaledRect = {left: 2, top: 4, width: 6, height: 8};

return image.crop(rect, {scaleFactor: 2})
.then(() => assert.calledWith(SafeRect.create, scaledRect));
});

it('should crop an image', () => {
SafeRect.create.returns({left: 1, top: 2, width: 3, height: 4});

return image.crop({})
.then(() => {
assert.calledOnce(PngImg.prototype.crop);
assert.calledWith(PngImg.prototype.crop, 1, 2, 3, 4);
});
});

it('should be resolved with the same instance of "Image" after crop', () => {
return image.crop({})
.then((res) => assert.deepEqual(res, image));
});
});

it('should return correct size', () => {
Expand Down
97 changes: 97 additions & 0 deletions test/unit/image/safe-rect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

const SafeRect = require('lib/image/safe-rect');

describe('SafeRect', () => {
describe('left', () => {
it('should return rectangle left coordinate if it is a positive number', () => {
const safeRect = SafeRect.create({left: 1});

assert.equal(safeRect.left, 1);
});

it('should handle cases when rectangle left coordinate is a negative number', () => {
const safeRect = SafeRect.create({left: -1});

assert.equal(safeRect.left, 0);
});

it('should handle cases when rectangle left coordinate is 0', () => {
const safeRect = SafeRect.create({left: 0});

assert.equal(safeRect.left, 0);
});
});

describe('top', () => {
it('should return rectangle top coordinate if it is a positive number', () => {
const safeRect = SafeRect.create({top: 1});

assert.equal(safeRect.top, 1);
});

it('should handle cases when rectangle top coordinate is a negative number', () => {
const safeRect = SafeRect.create({top: -1});

assert.equal(safeRect.top, 0);
});

it('should handle cases when rectangle top coordinate is 0', () => {
const safeRect = SafeRect.create({top: 0});

assert.equal(safeRect.top, 0);
});
});

describe('width', () => {
it('should return rectangle width when its position is not out of the bounds of image width', () => {
const safeRect = SafeRect.create({left: 1, width: 2}, {width: 4});

assert.equal(safeRect.width, 2);
});

it('should return rectangle width when its position is at the bounds of image width', () => {
const safeRect = SafeRect.create({left: 1, width: 3}, {width: 4});

assert.equal(safeRect.width, 3);
});

it('should handle cases when rectangle position is out of the bounds of image width', () => {
const safeRect = SafeRect.create({left: 1, width: 5}, {width: 4});

assert.equal(safeRect.width, 4 - 1);
});

it('should handle cases when rectangle left coordinate is a negative number', () => {
const safeRect = SafeRect.create({left: -1, width: 5}, {width: 4});

assert.equal(safeRect.width, 4);
});
});

describe('height', () => {
it('should return rectangle height when its position is not out of the bounds of image height', () => {
const safeRect = SafeRect.create({top: 1, height: 2}, {height: 4});

assert.equal(safeRect.height, 2);
});

it('should return rectangle height when its position is at the bounds of image height', () => {
const safeRect = SafeRect.create({top: 1, height: 3}, {height: 4});

assert.equal(safeRect.height, 3);
});

it('should handle cases when rectangle position is out of the bounds of image height', () => {
const safeRect = SafeRect.create({top: 1, height: 5}, {height: 4});

assert.equal(safeRect.height, 4 - 1);
});

it('should handle cases when rectangle top coordinate is a negative number', () => {
const safeRect = SafeRect.create({top: -1, height: 5}, {height: 4});

assert.equal(safeRect.height, 4);
});
});
});
0