Skip to content

Commit

Permalink
es6 tests (jimp-dev#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Aug 10, 2018
1 parent a93a891 commit 65497b5
Show file tree
Hide file tree
Showing 25 changed files with 419 additions and 588 deletions.
58 changes: 19 additions & 39 deletions test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,37 @@
const fs = require('fs');
const should = require('should');
const { Jimp, getTestDir } = require('./test-helper');
import fs from 'fs';
import should from 'should';
import { Jimp, getTestDir } from './test-helper';

const imagesDir = getTestDir() + '/samples';

describe('Async functions', () => {
it('write returns promise', done => {
const writePath = './test.png';

it('write returns promise', async () => {
// process.env is undefined in the browser tests. If BABEL_ENV
// isn't found don't run this test in the browser
if (process.env.ENV === 'browser') {
return done();
return;
}

new Jimp(imagesDir + '/dice.png', function(err) {
should.not.exist(err);
const writePath = './test.png';
const image = await Jimp.read(imagesDir + '/dice.png');
const writtenImage = await image.writeAsync(writePath);

this.writeAsync(writePath).then(image => {
should.exist(image);
fs.existsSync(writePath).should.be.true();
fs.unlinkSync(writePath);
done();
});
});
should.exist(writtenImage);
fs.existsSync(writePath).should.be.true();
fs.unlinkSync(writePath);
});

it('getBuffer returns promise', done => {
if (process.env.ENV === 'browser') {
return done();
}
it('getBuffer returns promise', async () => {
const image = await Jimp.read(imagesDir + '/dice.png');
const buffer = await image.getBufferAsync(Jimp.AUTO);

new Jimp(imagesDir + '/dice.png', function(err) {
should.not.exist(err);
this.getBufferAsync(Jimp.AUTO).then(buffer => {
should.exist(buffer);
done();
});
});
should.exist(buffer);
});

it('getBase64 returns promise', done => {
if (process.env.ENV === 'browser') {
return done();
}

new Jimp(imagesDir + '/dice.png', function(err) {
should.not.exist(err);
it('getBase64 returns promise', async () => {
const image = await Jimp.read(imagesDir + '/dice.png');
const bas64 = await image.getBase64Async(Jimp.AUTO);

this.getBase64Async(Jimp.AUTO).then(buffer => {
should.exist(buffer);
done();
});
});
should.exist(bas64);
});
});
153 changes: 70 additions & 83 deletions test/autocrop.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Jimp, mkJGD } = require('./test-helper');
import { Jimp, mkJGD } from './test-helper';

describe('Autocrop', () => {
it('image with transparent surround color', done => {
Jimp.read(
it('image with transparent surround color', async () => {
const imgSrc = await Jimp.read(
mkJGD(
' ',
' ◆◆ ',
Expand All @@ -12,21 +12,18 @@ describe('Autocrop', () => {
' ◆◆ ',
' '
)
)
.then(imgSrc => {
imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
);
done();
})
.catch(done);
);

imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
);
});

it('image with opaque surround color', done => {
Jimp.read(
it('image with opaque surround color', async () => {
const imgSrc = await Jimp.read(
mkJGD(
'▥▥▥▥▥▥▥▥▥▥',
'▥▥▥▥◆◆▥▥▥▥',
Expand All @@ -36,21 +33,18 @@ describe('Autocrop', () => {
'▥▥▥▥◆◆▥▥▥▥',
'▥▥▥▥▥▥▥▥▥▥'
)
)
.then(imgSrc => {
imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD('▥▥◆◆▥▥', '▥◆▦▦◆▥', '◆▦▦▦▦◆', '▥◆▦▦◆▥', '▥▥◆◆▥▥')
);
done();
})
.catch(done);
);

imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD('▥▥◆◆▥▥', '▥◆▦▦◆▥', '◆▦▦▦▦◆', '▥◆▦▦◆▥', '▥▥◆◆▥▥')
);
});

it('image with one color border', done => {
Jimp.read(
it('image with one color border', async () => {
const imgSrc = await Jimp.read(
mkJGD(
'▥▥▥▥▥▥▥▥▥▥▥▥',
'▥▥▥▥▥▥▥▥▥▥▥▥',
Expand All @@ -62,27 +56,24 @@ describe('Autocrop', () => {
'▥▥▥▥▥▥▥▥▥▥▥▥',
'▥▥▥▥▥▥▥▥▥▥▥▥'
)
)
.then(imgSrc => {
imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(
' ◆◆ ',
' ◆▦▦◆ ',
' ◆▦▦▦▦◆ ',
' ◆▦▦◆ ',
' ◆◆ '
)
);
done();
})
.catch(done);
);

imgSrc
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(
' ◆◆ ',
' ◆▦▦◆ ',
' ◆▦▦▦▦◆ ',
' ◆▦▦◆ ',
' ◆◆ '
)
);
});

it('image border with small variation', done => {
Jimp.read(
it('image border with small variation', async () => {
const imgSrc = await Jimp.read(
mkJGD(
'323232323232',
'232323232323',
Expand All @@ -94,40 +85,36 @@ describe('Autocrop', () => {
'232323232323',
'323232323232'
)
)
.then(imgSrc => {
imgSrc
.clone()
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(
'323232323232',
'232323232323',
'32 ◆◆ 32',
'23 ◆▦▦◆ 23',
'32 ◆▦▦▦▦◆ 32',
'23 ◆▦▦◆ 23',
'32 ◆◆ 32',
'232323232323',
'323232323232'
)
);
imgSrc
.clone()
.autocrop(0.005)
.getJGDSync()
.should.be.sameJGD(
mkJGD(
' ◆◆ ',
' ◆▦▦◆ ',
' ◆▦▦▦▦◆ ',
' ◆▦▦◆ ',
' ◆◆ '
)
);
done();
})
.catch(done);
);
imgSrc
.clone()
.autocrop()
.getJGDSync()
.should.be.sameJGD(
mkJGD(
'323232323232',
'232323232323',
'32 ◆◆ 32',
'23 ◆▦▦◆ 23',
'32 ◆▦▦▦▦◆ 32',
'23 ◆▦▦◆ 23',
'32 ◆◆ 32',
'232323232323',
'323232323232'
)
);
imgSrc
.clone()
.autocrop(0.005)
.getJGDSync()
.should.be.sameJGD(
mkJGD(
' ◆◆ ',
' ◆▦▦◆ ',
' ◆▦▦▦▦◆ ',
' ◆▦▦◆ ',
' ◆◆ '
)
);
});
});
3 changes: 2 additions & 1 deletion test/blit.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Jimp, mkJGD } = require('./test-helper');
import { Jimp, mkJGD } from './test-helper';

describe('Blit over image', () => {
const targetJGD = mkJGD(
Expand All @@ -22,6 +22,7 @@ describe('Blit over image', () => {

let targetImg;
let srcImg; // stores the Jimp instances of the JGD images above.

before(done => {
const img1 = Jimp.read(targetJGD);
const img2 = Jimp.read(srcJGD);
Expand Down
3 changes: 2 additions & 1 deletion test/callbacks.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { Jimp, mkJGD, hasOwnProp } = require('./test-helper');
import { Jimp, mkJGD, hasOwnProp } from './test-helper';

describe('Callbacks', () => {
const targetJGD = mkJGD('▴▸▾', '◆▪▰', '▵▹▿');
const miniJGD = mkJGD('□▥', '▥■');

let targetImg;
let miniImg; // stores the Jimp instances of the JGD images above.

before(done => {
const img1 = Jimp.read(targetJGD);
const img2 = Jimp.read(miniJGD);
Expand Down
83 changes: 37 additions & 46 deletions test/color.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
const should = require('should');
const { Jimp, donutJGD } = require('./test-helper');
import { Jimp, donutJGD } from './test-helper';

describe('canvas color transformation', () => {
const redDonutJGD = donutJGD(0x00000000, 0xff000088, 0xff0000ff);

it('can apply more than one color transformation', done => {
new Jimp(redDonutJGD, (err, image) => {
should.not.exist(err);
const newJGD = image
.color([
{ apply: 'hue', params: [-180] },
{ apply: 'lighten', params: [25] }
])
.getJGDSync();
newJGD.should.be.sameJGD(donutJGD(0x40404000, 0x80ffff88, 0x80ffffff));
done();
const redDonutJGD = donutJGD(0x00000000, 0xff000088, 0xff0000ff);

it('can apply more than one color transformation', async () => {
const image = await Jimp.read(redDonutJGD);
const newJGD = image
.color([
{ apply: 'hue', params: [-180] },
{ apply: 'lighten', params: [25] }
])
.getJGDSync();

newJGD.should.be.sameJGD(donutJGD(0x40404000, 0x80ffff88, 0x80ffffff));
});
});

it('lighten', done => {
new Jimp(redDonutJGD, (err, image) => {
should.not.exist(err);
image
.color([{ apply: 'lighten', params: [25] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x40404000, 0xff808088, 0xff8080ff));
done();

it('lighten', async () => {
const image = await Jimp.read(redDonutJGD);

image
.color([{ apply: 'lighten', params: [25] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x40404000, 0xff808088, 0xff8080ff));
});
});

it('brighten', done => {
new Jimp(redDonutJGD, (err, image) => {
should.not.exist(err);
image
.color([{ apply: 'brighten', params: [25] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x40404000, 0xff404088, 0xff4040ff));
done();

it('brighten', async () => {
const image = await Jimp.read(redDonutJGD);

image
.color([{ apply: 'brighten', params: [25] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x40404000, 0xff404088, 0xff4040ff));
});
});

it('spin hue', done => {
new Jimp(redDonutJGD, (err, image) => {
should.not.exist(err);
image
.color([{ apply: 'hue', params: [150] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x00000000, 0x00ff8088, 0x00ff80ff));
done();

it('spin hue', async () => {
const image = await Jimp.read(redDonutJGD);

image
.color([{ apply: 'hue', params: [150] }])
.getJGDSync()
.should.be.sameJGD(donutJGD(0x00000000, 0x00ff8088, 0x00ff80ff));
});
});
});
2 changes: 1 addition & 1 deletion test/colordiff.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Jimp } = require('./test-helper');
import { Jimp } from './test-helper';

// Convert [0..1] float to a percent value with only one decimal.
const pct = n => ((n * 1000) << 0) / 10;
Expand Down
2 changes: 1 addition & 1 deletion test/compare.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Jimp, mkJGD } = require('./test-helper');
import { Jimp, mkJGD } from './test-helper';

describe('Compare image difference', () => {
let imgs = [
Expand Down
Loading

0 comments on commit 65497b5

Please sign in to comment.