Browse Source

Add test cases for image Buffer

pull/305/head
Jerome Wu 6 years ago
parent
commit
b7b2148b71
  1. 1
      docs/image-format.md
  2. 3
      scripts/test-helper.js
  3. 9
      src/node/index.js
  4. 15
      tests/recognize.test.js

1
docs/image-format.md

@ -11,3 +11,4 @@ On a browser, an image can be: @@ -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

3
scripts/test-helper.js

@ -1,3 +1,4 @@ @@ -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');

9
src/node/index.js

@ -25,6 +25,7 @@ const readFile = util.promisify(fs.readFile); @@ -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) => { @@ -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);
};
/*

15
tests/recognize.test.js

@ -116,6 +116,21 @@ describe('recognize()', () => { @@ -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) => {

Loading…
Cancel
Save