browser unit testing (WIP)

This commit is contained in:
Moos 2018-10-20 20:51:37 -07:00
parent 896b0545cf
commit 890f3dd353
9 changed files with 37 additions and 20 deletions

View File

@ -1,4 +1,4 @@
{ {
"presets": ["env", "stage-2"], "presets": ["env", "stage-1"],
"plugins": ["transform-class-properties"] "plugins": ["transform-class-properties"]
} }

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
dict
node_modules node_modules
.idea .idea
*.iml *.iml
.cache .cache
build build
dict
dist

View File

@ -19,6 +19,7 @@
"files": [ "files": [
"bench", "bench",
"bin", "bin",
"dict",
"dist", "dist",
"lib", "lib",
"src", "src",

View File

@ -18,13 +18,16 @@ class BaseFile {
constructor(type, dictPath, posName) { constructor(type, dictPath, posName) {
this.type = type; this.type = type;
this.filePath = `${dictPath}/${type}.${posName}.js`; this.filePath = `${dictPath}/${type}.${posName}.js`;
this.loadError = null;
} }
load() { load() {
if (this.loadError) return Promise.reject(this.loadError);
return import(this.filePath) return import(this.filePath)
.then(exports => this.file = exports.default) .then(exports => this.file = exports.default)
.catch(err => { .catch(err => {
console.error(`Error loading ${this.type} file for ${this.filePath}.`, err); console.error(`Error loading "${this.type}" file ${this.filePath}.`, err);
this.loadError = err;
throw err; throw err;
}); });
} }

View File

@ -12,6 +12,8 @@ import { is, get, lookup, seek } from '../common';
import IndexFile from './indexFile'; import IndexFile from './indexFile';
import DataFile from './dataFile'; import DataFile from './dataFile';
console.log(4545, ' borwser index')
const POS = { const POS = {
n: 'noun', n: 'noun',
v: 'verb', v: 'verb',

View File

@ -9,7 +9,13 @@
* Released under MIT license * Released under MIT license
*/ */
var { normalize, nextTick } = require('./util'); var { normalize, nextTick, isString } = require('./util');
function error(err, callback) {
if (isString(err)) err = new Error(err);
callback && callback(err, {});
return Promise.reject(err);
}
/** /**
* factory for main lookup function * factory for main lookup function
@ -112,11 +118,18 @@ function get(isFn) {
start = profile && new Date(), start = profile && new Date(),
words = this.parse(text), words = this.parse(text),
results = [], results = [],
self = this; self = this,
first = words.shift();
return Promise // test one first & check for error, otherwise
.all(words.map(exec)) // map is inoccuous to errors!
.then(done); return exec(first)
.then(() => Promise.all(words.map(exec)))
.then(done)
.catch(err => {
done(); // callback signature is same!
return Promise.reject(err);
});
function exec(word) { function exec(word) {
return self[isFn] return self[isFn]
@ -232,18 +245,12 @@ function lineDataToJSON(line, location) {
*/ */
function seek(offset, pos, callback){ function seek(offset, pos, callback){
var offsetTmp = Number(offset); var offsetTmp = Number(offset);
if (isNaN(offsetTmp) || offsetTmp <= 0) return error('Offset must be valid positive number: ' + offset); if (isNaN(offsetTmp) || offsetTmp <= 0) return error('Offset must be valid positive number: ' + offset, callback);
var data = this.getFilesFor(pos).data; var data = this.getFilesFor(pos).data;
if (!data) return error('Incorrect POS - 2nd argument must be a, r, n or v.'); if (!data) return error('Incorrect POS - 2nd argument must be a, r, n or v.', callback);
return data.lookup(offset, callback); return data.lookup(offset, callback);
function error(msg) {
var err = new Error(msg);
callback && callback(err, {});
return Promise.reject(err);
}
} }
const LEX_NAMES = [ const LEX_NAMES = [

View File

@ -56,10 +56,11 @@ function prepText(text) {
} }
module.exports = { module.exports = {
isString,
stopwords, stopwords,
nextTick, nextTick,
normalize, normalize,
tokenizer, tokenizer,
prepText, prepText,
makeStopwordString makeStopwordString
} };

View File

@ -9,7 +9,9 @@
* Released under MIT license * Released under MIT license
*/ */
if (process.browser) { console.log(333, process.browser);
if (11 || process.browser) {
module.exports = require('./browser'); module.exports = require('./browser');
} else { } else {
module.exports = require('./node'); module.exports = require('./node');

View File

@ -94,8 +94,8 @@ describe('getX()...', function() {
}); });
}); });
it('should get nouns', function(done) { it.only('should get nouns', function(done) {
wordpos.getNouns(str, function(result) { wordpos.getNouns('foot bar', function(result) {
assert.sameMembers(result, expected.nouns); assert.sameMembers(result, expected.nouns);
done(); done();
}); });