From b7b2148b7165bdda4a38420c07489c7db9c05e24 Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Tue, 2 Jul 2019 17:44:30 +0800 Subject: [PATCH] Add test cases for image Buffer --- docs/image-format.md | 1 + scripts/test-helper.js | 3 ++- src/node/index.js | 9 +++++++-- tests/recognize.test.js | 19 +++++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/docs/image-format.md b/docs/image-format.md index 48b2126..742671b 100644 --- a/docs/image-format.md +++ b/docs/image-format.md @@ -11,3 +11,4 @@ On a browser, an image can be: In Node.js, an image can be - a path to a local image +- a Buffer storing binary image diff --git a/scripts/test-helper.js b/scripts/test-helper.js index dac751c..f164d7d 100644 --- a/scripts/test-helper.js +++ b/scripts/test-helper.js @@ -1,3 +1,4 @@ global.expect = require('expect.js'); -global.fetch = require('node-fetch'); +global.fs = require('fs'); +global.path = require('path'); global.Tesseract = require('../src'); diff --git a/src/node/index.js b/src/node/index.js index 575c18d..54dcbdd 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -25,6 +25,7 @@ const readFile = util.promisify(fs.readFile); * @access public * @param {string} image - image source, supported formats: * string: URL string or file path + * buffer: image buffer * @returns {array} binary image in array format */ const loadImage = (image) => { @@ -34,8 +35,12 @@ const loadImage = (image) => { }) .then(resp => resp.data); } - if (Buffer.isBuffer( image) ) return new Promise(function(resolve, reject) { resolve(image); }); - else return readFile(image); + + if (Buffer.isBuffer(image)) { + return Promise.resolve(image); + } + + return readFile(image); }; /* diff --git a/tests/recognize.test.js b/tests/recognize.test.js index f9271a6..4d3defe 100644 --- a/tests/recognize.test.js +++ b/tests/recognize.test.js @@ -39,7 +39,7 @@ describe('recognize()', () => { }).timeout(30000) )); }); - + describe('should read bmp, jpg, png and pbm format images', () => { FORMATS.forEach(format => ( it(`support ${format} format`, (done) => { @@ -116,6 +116,21 @@ describe('recognize()', () => { )); }); + (isBrowser ? describe.skip : describe)('should recognize image in Buffer (Node.js only)', () => { + FORMATS.forEach(format => ( + it(`support ${format} format`, (done) => { + const worker = getWorker(); + worker + .recognize(fs.readFileSync(path.join(__dirname, 'assets', 'images', `simple.${format}`))) + .then(({ text }) => { + expect(text).to.be(SIMPLE_TEXT); + worker.terminate(); + done(); + }); + }).timeout(10000) + )); + }); + (isBrowser ? describe : describe.skip)('should read image from img DOM element (browser only)', () => { FORMATS.forEach(format => ( it(`support ${format} format`, (done) => { @@ -133,7 +148,7 @@ describe('recognize()', () => { }).timeout(10000) )); }); - + (isBrowser ? describe : describe.skip)('should read image from video DOM element (browser only)', () => { FORMATS.forEach(format => ( it(`support ${format} format`, (done) => {