From e4e661623a14160a824087c6a66059e3b6dba5a0 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Fri, 14 Jan 2022 19:04:24 +0300 Subject: [PATCH] tests: cleanup/polish --- test/brightness.js | 11 ++++---- test/fixture_resize.js | 2 +- test/pica.js | 27 ++++++++++---------- test/stepper.js | 12 ++++----- test/unsharp.js | 58 ++++++++++++++++++------------------------ test/unsharp_mask.js | 14 +++++----- 6 files changed, 58 insertions(+), 66 deletions(-) diff --git a/test/brightness.js b/test/brightness.js index e0771b9..2ccf58f 100644 --- a/test/brightness.js +++ b/test/brightness.js @@ -28,20 +28,21 @@ function createTest(color) { let test_name = `test ${SRC_W}x${SRC_H} -> ${DEST_W}x${DEST_H} with color #${hexColor}${hexColor}${hexColor}`; - it(test_name, function () { - return pica.resizeBuffer({ + it(test_name, async () => { + const result = await pica.resizeBuffer({ src: src, width: SRC_W, height: SRC_H, toWidth: DEST_W, toHeight: DEST_H - }) - .then(result => assert.deepStrictEqual(result, correct)); + }); + + assert.deepStrictEqual(result, correct); }); } -describe('Brightness should not change', function () { +describe('Brightness should not change', () => { createTest(255); createTest(127); }); diff --git a/test/fixture_resize.js b/test/fixture_resize.js index d831dae..a13de4e 100644 --- a/test/fixture_resize.js +++ b/test/fixture_resize.js @@ -13,7 +13,7 @@ const FIXTURES_DIRECTORY = path.join(__dirname, 'fixtures'); const OUTPUT_DIRECTORY = path.join(__dirname, '..'); -describe('Fixture resize', function () { +describe('Fixture resize', () => { it.skip('.resizeCanvas() should be correct for the given fixture', async () => { this.timeout(3000); diff --git a/test/pica.js b/test/pica.js index 3629f4e..94eab8f 100644 --- a/test/pica.js +++ b/test/pica.js @@ -5,24 +5,24 @@ const _pica = require('../index.js'); const assert = require('assert'); -describe('API', function () { +describe('API', () => { // Need node 8 to run - it('Upscale (unexpected use) via wasm should not crash', function () { + it('Upscale (unexpected use) via wasm should not crash', async () => { const p = _pica({ features: [ 'wasm' ] }); const input = new Uint8Array(500 * 500 * 4); - return p.init().then(() => p.resizeBuffer({ + await p.resizeBuffer({ src: input, width: 500, height: 500, toWidth: 1000, toHeight: 1000 - })); + }); }); - it('Should return result in promise', function () { + it('Should return result in promise', async () => { let src = document.createElement('canvas'); src.width = 1000; @@ -33,23 +33,22 @@ describe('API', function () { to.width = 100; to.height = 100; - return _pica().resize(src, to).then(result => { - assert.strictEqual(result, to); - }); + const result = await _pica().resize(src, to); + assert.strictEqual(result, to); }); - it('Resize with bad output size should fail', function () { + it('Resize with bad output size should fail', async () => { let src = document.createElement('canvas'); src.width = 1000; src.height = 1000; let to = document.createElement('canvas'); to.width = 0; to.height = 0; - return _pica().resize(src, to) - .then(() => { throw new Error('Resize should fail'); }) - .catch(err => { - assert.strictEqual(err.message, 'Invalid output size: 0x0'); - }); + + await assert.rejects( + async () => _pica().resize(src, to), + { message: 'Invalid output size: 0x0' } + ); }); }); diff --git a/test/stepper.js b/test/stepper.js index b3d7971..df82ee5 100644 --- a/test/stepper.js +++ b/test/stepper.js @@ -8,8 +8,8 @@ const TILE_SIZE = 1024; const TILE_BORDER = 3; -describe('createStages', function () { - it('1024x1024 -> 300x300 (1 stage)', function () { +describe('createStages', () => { + it('1024x1024 -> 300x300 (1 stage)', () => { assert.deepStrictEqual(createStages( 1024, 1024, @@ -20,7 +20,7 @@ describe('createStages', function () { ), [ [ 300, 300 ] ]); }); - it('1024x1024 -> 2x2 (2 stages)', function () { + it('1024x1024 -> 2x2 (2 stages)', () => { assert.deepStrictEqual(createStages( 1024, 1024, @@ -31,7 +31,7 @@ describe('createStages', function () { ), [ [ 45, 45 ], [ 2, 2 ] ]); }); - it('102400x100 -> 1x1 (3 stages)', function () { + it('102400x100 -> 1x1 (3 stages)', () => { assert.deepStrictEqual(createStages( 102400, 100, @@ -42,7 +42,7 @@ describe('createStages', function () { ), [ [ 2189, 22 ], [ 47, 5 ], [ 1, 1 ] ]); }); - it('20000x1 -> 1x20000 (magnifying along another axis)', function () { + it('20000x1 -> 1x20000 (magnifying along another axis)', () => { assert.deepStrictEqual(createStages( 20000, 1, @@ -53,7 +53,7 @@ describe('createStages', function () { ), [ [ 737, 27 ], [ 27, 737 ], [ 1, 20000 ] ]); }); - it('1x1 -> 20000x20000 (magnifying should always be single stage)', function () { + it('1x1 -> 20000x20000 (magnifying should always be single stage)', () => { assert.deepStrictEqual(createStages( 1, 1, diff --git a/test/unsharp.js b/test/unsharp.js index 4865db9..372d2d1 100644 --- a/test/unsharp.js +++ b/test/unsharp.js @@ -3,8 +3,8 @@ const pica = require('../index.js')(); const assert = require('assert'); -describe('Unsharp mask', function () { - it('save random image untouched when amount, radius and threshold are equal to 0', function () { +describe('Unsharp mask', () => { + it('save random image untouched when amount, radius and threshold are equal to 0', async () => { let width = 100, height = 100, size = width * height * 4, @@ -15,55 +15,47 @@ describe('Unsharp mask', function () { image[i] = srcImage[i] = (Math.random() * 0xff + 0.5) | 0; } - return pica.init() - .then(() => { - pica.__mathlib.unsharp_mask(image, width, height, 1e-4, 0, 0); + await pica.init(); + pica.__mathlib.unsharp_mask(image, width, height, 1e-4, 0, 0); - for (let i = 0; i < size; i++) { - assert.strictEqual(image[i], srcImage[i]); - } - }); + for (let i = 0; i < size; i++) { + assert.strictEqual(image[i], srcImage[i]); + } }); - it('save white color untouched', function () { + it('save white color untouched', async () => { let srcImage = [ 255, 255, 255, 255 ]; let image = [ 255, 255, 255, 255 ]; - return pica.init() - .then(() => { - pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); + await pica.init(); + pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); - for (var i = 0; i < image.length; i++) { - assert.strictEqual(image[i], srcImage[i]); - } - }); + for (var i = 0; i < image.length; i++) { + assert.strictEqual(image[i], srcImage[i]); + } }); - it('save black color untouched', function () { + it('save black color untouched', async () => { let srcImage = [ 0, 0, 0, 0 ]; let image = [ 0, 0, 0, 0 ]; - return pica.init() - .then(() => { - pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); + await pica.init(); + pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); - for (var i = 0; i < image.length; i++) { - assert.strictEqual(image[i], srcImage[i]); - } - }); + for (var i = 0; i < image.length; i++) { + assert.strictEqual(image[i], srcImage[i]); + } }); - it('save red color untouched', function () { + it('save red color untouched', async () => { let srcImage = [ 255, 0, 0, 0 ]; let image = [ 255, 0, 0, 0 ]; - return pica.init() - .then(() => { - pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); + await pica.init(); + pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0); - for (var i = 0; i < image.length; i++) { - assert.strictEqual(image[i], srcImage[i]); - } - }); + for (var i = 0; i < image.length; i++) { + assert.strictEqual(image[i], srcImage[i]); + } }); }); diff --git a/test/unsharp_mask.js b/test/unsharp_mask.js index e641d48..4b5861d 100644 --- a/test/unsharp_mask.js +++ b/test/unsharp_mask.js @@ -11,11 +11,11 @@ function fill(target, arr) { } -describe('unsharp_mask', function () { +describe('unsharp_mask', () => { - describe('glur_mono16', function () { + describe('glur_mono16', () => { - it('js', function () { + it('js', () => { const glur_js = require('glur/mono16'); let sample = new Uint16Array(100 * 100); @@ -30,7 +30,7 @@ describe('unsharp_mask', function () { }); - it('wasm', function () { + it('wasm', () => { const glur_js = require('glur/mono16'); const mlib_wasm = mathlib_raw({ js: false }).use(require('../lib/mm_unsharp_mask')); @@ -84,7 +84,7 @@ describe('unsharp_mask', function () { }); - describe('unsharp_mask', function () { + describe('unsharp_mask', () => { function createSample(width, height) { const result = new Uint8Array(width * height * 4); @@ -95,7 +95,7 @@ describe('unsharp_mask', function () { } - it('js should not throw without wasm', function () { + it('js should not throw without wasm', () => { const mlib = mathlib_raw({ wasm: false }).use(require('../lib/mm_unsharp_mask')); let sample = createSample(100, 100); @@ -103,7 +103,7 @@ describe('unsharp_mask', function () { }); - it('wasm', function () { + it('wasm', () => { const mlib_js = mathlib_raw({ wasm: false }).use(require('../lib/mm_unsharp_mask')); const mlib_wasm = mathlib_raw({ js: false }).use(require('../lib/mm_unsharp_mask'));