Skip to content

Commit

Permalink
Add test cases for image Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromewu committed Jul 2, 2019
1 parent 42012d6 commit b7b2148
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/image-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 1 deletion scripts/test-helper.js
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 2 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
};

/*
Expand Down
19 changes: 17 additions & 2 deletions tests/recognize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit b7b2148

Please sign in to comment.