From 890f3dd353a9a7fbee99cb6fb85b8f3304ea5222 Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 20 Oct 2018 20:51:37 -0700 Subject: [PATCH] browser unit testing (WIP) --- .babelrc | 2 +- .gitignore | 3 ++- package.json | 1 + src/browser/baseFile.js | 5 ++++- src/browser/index.js | 2 ++ src/common.js | 33 ++++++++++++++++++++------------- src/util.js | 3 ++- src/wordpos.js | 4 +++- test/wordpos_test.js | 4 ++-- 9 files changed, 37 insertions(+), 20 deletions(-) diff --git a/.babelrc b/.babelrc index 1067c95..603209a 100644 --- a/.babelrc +++ b/.babelrc @@ -1,4 +1,4 @@ { - "presets": ["env", "stage-2"], + "presets": ["env", "stage-1"], "plugins": ["transform-class-properties"] } diff --git a/.gitignore b/.gitignore index 61fd493..473b32d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -dict node_modules .idea *.iml .cache build +dict +dist diff --git a/package.json b/package.json index 2d6be1f..60c50be 100755 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "files": [ "bench", "bin", + "dict", "dist", "lib", "src", diff --git a/src/browser/baseFile.js b/src/browser/baseFile.js index acb56d1..ada200e 100644 --- a/src/browser/baseFile.js +++ b/src/browser/baseFile.js @@ -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; }); } diff --git a/src/browser/index.js b/src/browser/index.js index 3764c43..b059fa4 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -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', diff --git a/src/common.js b/src/common.js index b03fd8b..004ee8a 100644 --- a/src/common.js +++ b/src/common.js @@ -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 = [ diff --git a/src/util.js b/src/util.js index 28e6718..0820c73 100644 --- a/src/util.js +++ b/src/util.js @@ -56,10 +56,11 @@ function prepText(text) { } module.exports = { + isString, stopwords, nextTick, normalize, tokenizer, prepText, makeStopwordString -} +}; diff --git a/src/wordpos.js b/src/wordpos.js index e1a974e..b597902 100644 --- a/src/wordpos.js +++ b/src/wordpos.js @@ -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'); diff --git a/test/wordpos_test.js b/test/wordpos_test.js index aff5849..96756e6 100644 --- a/test/wordpos_test.js +++ b/test/wordpos_test.js @@ -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(); });