Skip to content

Commit

Permalink
Add resized images to rotation test
Browse files Browse the repository at this point in the history
To trigger the "shouldRotateBefore" code path.
  • Loading branch information
happycollision committed Jul 6, 2024
1 parent e39c747 commit 2074771
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions test/unit/rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,46 @@ describe('Rotation', function () {
});
});

[90, 180, 270, 45].forEach(function (angle) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle}`, function (done) {
const [width, height] = angle === 45 ? [742, 742] : [inputWidth, inputHeight];
const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];

sharp(input)
.rotate()
.rotate(angle)
.toBuffer(function (err, data, info) {
[true, false].forEach((doResize) => {
[90, 180, 270, 45].forEach(function (angle) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (done) {
const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x);
const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];

const img = sharp(input)
.rotate()
.rotate(angle);
doResize && img.resize(expectedWidth);

img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
});
});

[[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_');
const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & ');
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName}`, function (done) {
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);
[[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_');
const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & ');
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) {
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);

const img = sharp(input).rotate();
flip && img.flip();
flop && img.flop();
const img = sharp(input).rotate();
flip && img.flip();
flop && img.flop();
doResize && img.resize(orientation === 'Landscape' ? 320 : 240);

img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, inputWidth);
assert.strictEqual(info.height, inputHeight);
fixtures.assertSimilar(expectedOutput, data, done);
img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1));
assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1));
fixtures.assertSimilar(expectedOutput, data, done);
});
});
});
});
Expand Down

0 comments on commit 2074771

Please sign in to comment.