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"]
}

3
.gitignore vendored
View File

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

View File

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

View File

@ -18,13 +18,16 @@ class BaseFile {
constructor(type, dictPath, posName) {
this.type = type;
this.filePath = `${dictPath}/${type}.${posName}.js`;
this.loadError = null;
}
load() {
if (this.loadError) return Promise.reject(this.loadError);
return import(this.filePath)
.then(exports => this.file = exports.default)
.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;
});
}

View File

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

View File

@ -9,7 +9,13 @@
* 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
@ -112,11 +118,18 @@ function get(isFn) {
start = profile && new Date(),
words = this.parse(text),
results = [],
self = this;
self = this,
first = words.shift();
return Promise
.all(words.map(exec))
.then(done);
// test one first & check for error, otherwise
// map is inoccuous to errors!
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) {
return self[isFn]
@ -232,18 +245,12 @@ function lineDataToJSON(line, location) {
*/
function seek(offset, pos, callback){
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;
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);
function error(msg) {
var err = new Error(msg);
callback && callback(err, {});
return Promise.reject(err);
}
}
const LEX_NAMES = [

View File

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

View File

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

View File

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