Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristojorg committed Mar 28, 2017
1 parent d272fef commit e728e79
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,17 @@ const tiff = function tiff (options) {
if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'none'])) {
this.options.tiffCompression = options.compression;
} else {
throw new Error('Invalid compression option [' + options.compression + ']. Should be one of: lzw, deflate, jpeg, none');
const message = `Invalid compression option "${options.compression}". Should be one of: lzw, deflate, jpeg, none`;
throw new Error(message);
}
}
// predictor
if (is.defined(options) && is.defined(options.predictor)) {
if (is.string(options.predictor) && is.inArray(options.predictor, ['none', 'horizontal', 'float'])) {
this.options.tiffPredictor = options.predictor;
} else {
throw new Error('Invalid predictor option [' + options.predictor + ']. Should be one of: none, horizontal, float');
const message = `Invalid predictor option "${options.predictor}". Should be one of: none, horizontal, float`;
throw new Error(message);
}
}
return this._updateFormatOut('tiff', options);
Expand Down
55 changes: 54 additions & 1 deletion test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,15 @@ describe('Input/output', function () {
});
});

it('TIFF lzw compression shrinks test file', function (done) {
it('TIFF lzw compression with horizontal predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
.tiff({
compression: 'lzw',
force: true,
// note: lzw compression is imperfect and sometimes
// generates larger files, as it does with this input
// if no predictor is used.
predictor: 'horizontal'
})
.toFile(fixtures.outputTiff, (err, info) => {
Expand All @@ -878,6 +881,56 @@ describe('Input/output', function () {
});
});

it('TIFF deflate compression with hoizontal predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
.tiff({
compression: 'deflate',
force: true,
predictor: 'horizontal'
})
.toFile(fixtures.outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert(info.size < startSize);
fs.unlinkSync(fixtures.outputTiff);
done();
});
});

it('TIFF deflate compression without predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
.tiff({
compression: 'deflate',
force: true,
predictor: 'none'
})
.toFile(fixtures.outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert(info.size < startSize);
fs.unlinkSync(fixtures.outputTiff);
done();
});
});

it('TIFF jpeg compression shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
.tiff({
compression: 'jpeg',
force: true
})
.toFile(fixtures.outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert(info.size < startSize);
fs.unlinkSync(fixtures.outputTiff);
done();
});
});

it('TIFF none compression does not throw error', function () {
assert.doesNotThrow(function () {
sharp().tiff({ compression: 'none' });
Expand Down

0 comments on commit e728e79

Please sign in to comment.