wordpos/test/wordpos_test.js

506 lines
13 KiB
JavaScript
Raw Normal View History

/**
* wordpos_test.js
*
* test file for main wordpos functionality
*
* Usage:
2016-01-18 08:09:56 +00:00
* npm install mocha -g
* mocha wordpos_test.js
*
2016-01-18 08:09:56 +00:00
* or
*
* npm test
*
* Copyright (c) 2012-2016 mooster@42at.com
* https://github.com/moos/wordpos
*
* Released under MIT license
2012-05-04 19:23:28 +00:00
*/
2016-01-18 08:09:56 +00:00
//import {describe, it} from 'mocha/lib/mocha.js';
var
chai = require('chai'),
_ = require('underscore'),
2016-01-18 08:09:56 +00:00
assert = chai.assert,
WordPOS = require('../src/wordpos'),
wordpos = new WordPOS({profile: false});
chai.config.showDiff = true;
2012-05-02 23:18:10 +00:00
var str = "The angry bear chased the frightened little squirrel",
2012-05-04 19:23:28 +00:00
expected = {
nouns: [ 'bear', 'squirrel', 'little', 'chased' ],
verbs: [ 'bear' ],
adjectives: [ 'little', 'angry', 'frightened' ],
adverbs: [ 'little' ],
2016-01-18 08:09:56 +00:00
rest: [ 'The' ]
2012-05-04 19:23:28 +00:00
},
garble = 'garblegarble', // expect not to find word
offset = 1285602;
2012-05-02 23:18:10 +00:00
2014-05-03 21:41:39 +00:00
2016-01-18 08:09:56 +00:00
describe('lookup', function() {
it('with callback', function (done) {
wordpos.lookup('hegemony', function (result) {
assert.equal(result.length, 1);
assert.equal(result[0].pos, 'n');
assert.equal(result[0].lemma, 'hegemony');
assert.equal(result[0].synonyms.length, 1);
done();
});
});
2014-05-03 21:41:39 +00:00
2016-01-18 08:09:56 +00:00
it('with Promise', function (done) {
wordpos.lookup('hegemony').then(function (result) {
assert.equal(result.length, 1);
assert.equal(result[0].pos, 'n');
assert.equal(result[0].lemma, 'hegemony');
assert.equal(result[0].synonyms.length, 1);
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
});
describe('options passed to constructor', function() {
var wp,
origProfile = WordPOS.defaults.profile;
it('should override default option', function(){
wp = new WordPOS({profile:123});
assert.equal(wp.options.profile, 123);
assert.equal(WordPOS.defaults.profile, origProfile);
});
2012-05-04 19:23:28 +00:00
2016-01-18 08:09:56 +00:00
it('should not erase default option', function(){
wp = new WordPOS({aaa:123});
assert.equal(wp.options.aaa, 123);
assert.equal(wp.options.profile, WordPOS.defaults.profile);
});
});
describe('getX()...', function() {
2012-05-25 18:09:37 +00:00
it('should get all POS', function(done) {
2012-05-02 23:18:10 +00:00
wordpos.getPOS(str, function(result) {
2016-01-18 08:09:56 +00:00
assert.sameMembers(result.nouns, expected.nouns);
assert.sameMembers(result.verbs, expected.verbs);
assert.sameMembers(result.adjectives, expected.adjectives);
assert.sameMembers(result.adverbs, expected.adverbs);
assert.sameMembers(result.rest, expected.rest);
2012-05-25 18:09:37 +00:00
done();
2012-05-02 23:18:10 +00:00
});
});
2012-05-04 19:23:28 +00:00
2018-10-21 03:51:37 +00:00
it.only('should get nouns', function(done) {
wordpos.getNouns('foot bar', function(result) {
2016-01-18 08:09:56 +00:00
assert.sameMembers(result, expected.nouns);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
2012-05-04 19:23:28 +00:00
2012-05-25 18:09:37 +00:00
it('should get verbs', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.getVerbs(str, function(result) {
2016-01-18 08:09:56 +00:00
assert.sameMembers(result, expected.verbs);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
2012-05-04 19:23:28 +00:00
2012-05-25 18:09:37 +00:00
it('should get adjectives', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.getAdjectives(str, function(result) {
2016-01-18 08:09:56 +00:00
assert.sameMembers(result, expected.adjectives);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
2012-05-04 19:23:28 +00:00
2012-05-25 18:09:37 +00:00
it('should get adverbs', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.getAdverbs(str, function(result) {
2016-01-18 08:09:56 +00:00
assert.sameMembers(result, expected.adverbs);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
});
2016-01-18 08:09:56 +00:00
describe('isX()...', function() {
2012-05-25 18:09:37 +00:00
it('should check if noun', function(done) {
2012-05-02 23:18:10 +00:00
wordpos.isNoun(expected.nouns[0], function(result) {
2016-01-18 08:09:56 +00:00
assert.ok(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-02 23:18:10 +00:00
});
});
2012-05-25 18:09:37 +00:00
it('should check if verb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isVerb(expected.verbs[0], function(result) {
2016-01-18 08:09:56 +00:00
assert.ok(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
2012-05-25 18:09:37 +00:00
it('should check if adjective', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isAdjective(expected.adjectives[0], function(result) {
2016-01-18 08:09:56 +00:00
assert.ok(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
2012-05-25 18:09:37 +00:00
it('should check if adverb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isAdverb(expected.adverbs[0], function(result) {
2016-01-18 08:09:56 +00:00
assert.ok(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
2012-05-02 23:18:10 +00:00
});
});
2016-01-18 08:09:56 +00:00
describe('!isX()...', function() {
2012-05-25 18:09:37 +00:00
it('should check if !noun', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isNoun(garble, function(result) {
2016-01-18 08:09:56 +00:00
assert.notOk(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should check if !verb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isVerb(garble, function(result) {
2016-01-18 08:09:56 +00:00
assert.notOk(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should check if !adjective', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isAdjective(garble, function(result) {
2016-01-18 08:09:56 +00:00
assert.notOk(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should check if !adverb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.isAdverb(garble, function(result) {
2016-01-18 08:09:56 +00:00
assert.notOk(result);
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2012-05-02 23:18:10 +00:00
});
2016-01-18 08:09:56 +00:00
describe('lookupX()...', function() {
2012-05-25 18:09:37 +00:00
it('should lookup noun', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.lookupNoun('squirrel', function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 2);
assert.equal(result[0].pos, 'n');
assert.equal(result[0].lemma, 'squirrel');
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should lookup verb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.lookupVerb('bear', function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 13);
assert.equal(result[0].pos, 'v');
assert.equal(result[0].lemma, 'bear');
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should lookup adjective', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.lookupAdjective('angry', function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 3);
assert.equal(result[0].pos, 'a');
assert.equal(result[0].lemma, 'angry');
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2016-01-18 08:09:56 +00:00
2012-05-25 18:09:37 +00:00
it('should lookup adverb', function(done) {
2012-05-04 19:23:28 +00:00
wordpos.lookupAdverb('little', function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 1);
assert.equal(result[0].pos, 'r');
assert.equal(result[0].lemma, 'little');
2012-05-25 18:09:37 +00:00
done();
2012-05-04 19:23:28 +00:00
});
});
2012-05-02 23:18:10 +00:00
});
2012-05-08 04:41:46 +00:00
describe('profile option', function() {
var wp = new WordPOS({profile : true});
2012-05-25 18:09:37 +00:00
it('should return time argument for isX()', function(done){
wp.isNoun(garble, function(result, word, time) {
2016-01-18 08:09:56 +00:00
assert.equal(word, garble);
assert.isDefined(time);
2012-05-25 18:09:37 +00:00
done();
2012-05-08 04:41:46 +00:00
});
});
2012-05-25 18:09:37 +00:00
it('should return time argument for getX()', function(done){
2012-05-08 04:41:46 +00:00
wp.getNouns(garble, function(result, time) {
2016-01-18 08:09:56 +00:00
assert.isDefined(time);
2012-05-25 18:09:37 +00:00
done();
2012-05-08 04:41:46 +00:00
});
});
2012-05-25 18:09:37 +00:00
it('should return time argument for lookupX()', function(done){
2012-05-08 04:41:46 +00:00
wp.isNoun(garble, function(result, time) {
2016-01-18 08:09:56 +00:00
assert.isDefined(time);
2012-05-25 18:09:37 +00:00
done();
2012-05-08 04:41:46 +00:00
});
});
2016-01-18 08:09:56 +00:00
it('should disable stopword filtering', function(done){
var wp = new WordPOS({stopwords : false}),
strWithStopwords = 'about after all'; // 3 adjective stopwords
2016-01-18 08:09:56 +00:00
wp.getAdjectives(strWithStopwords, function(result){
assert.equal(result.length, 3);
done();
});
});
2016-01-18 08:09:56 +00:00
it('should use custom stopwords', function(done){
var wp = new WordPOS({stopwords : ['all']}),
strWithStopwords = 'about after all'; // 3 adjective stopwords
// 'all' should be filtered
2016-01-18 08:09:56 +00:00
wp.getAdjectives(strWithStopwords, function(result){
assert.equal(result.length, 2);
done();
});
});
2012-05-08 04:41:46 +00:00
});
describe('nested callbacks on same index key', function() {
var wp = new WordPOS(),
word1 = 'head',
word2 = word1 + 'er';
it('should call inner callback', function(done){
wp.getPOS(word1, function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.nouns[0], word1);
// inner call on word2
wp.getPOS(word2, function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.nouns[0], word2);
done();
});
});
});
});
2014-05-03 21:41:39 +00:00
describe('rand()...', function() {
it('should get random word', function(done) {
2016-01-18 08:09:56 +00:00
wordpos.rand(function(result) {
assert.equal(result.length, 1);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
it('should get N random words', function(done) {
wordpos.rand({count: 3}, function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 3);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
it('should get random word starting with', function(done) {
wordpos.rand({startsWith: 'foo'}, function(result, startsWith) {
2016-01-18 08:09:56 +00:00
assert.equal(result[0].indexOf('foo'), 0);
assert.equal(startsWith, 'foo');
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
it('should get nothing starting with not found', function(done) {
2014-05-03 21:41:39 +00:00
wordpos.rand({startsWith: 'zzzz'}, function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 0);
2014-05-03 21:41:39 +00:00
done();
});
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
describe('randX()...', function() {
it('should get random noun', function(done) {
wordpos.randNoun(function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 1);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
it('should get random verb', function(done) {
wordpos.randVerb(function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 1);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
it('should get random adjective', function(done) {
wordpos.randAdjective(function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 1);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
2014-05-03 21:41:39 +00:00
it('should get random adverb', function(done) {
wordpos.randAdverb(function(result) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 1);
2014-05-03 21:41:39 +00:00
done();
});
});
// not found
it('should NOT get random noun starting with', function(done) {
wordpos.randNoun({startsWith: 'zzzz'},function(result, startsWith) {
2016-01-18 08:09:56 +00:00
assert.equal(result.length, 0);
2014-05-03 21:41:39 +00:00
done();
});
});
2016-01-18 08:09:56 +00:00
});
describe('seek()...', function() {
it('should seek offset', function(done) {
wordpos.seek(offset, 'a', function(err, result) {
assert.equal(result.synsetOffset, offset);
assert.equal(result.pos, 's');
assert.equal(result.lemma, 'amazing');
done();
});
});
it('should handle bad offset', function(done) {
wordpos.seek('foobar', 'a', function(err, result){
assert(err instanceof Error);
2018-10-15 05:50:12 +00:00
assert.equal(err.message, 'Offset must be valid positive number: foobar');
done();
2016-12-22 20:47:48 +00:00
}).catch(_.noop); // UnhandledPromiseRejectionWarning
});
it('should handle wrong offset', function(done) {
var bad_offset = offset + 1;
wordpos.seek(bad_offset, 'a', function(err, result) {
assert(err instanceof Error);
assert.equal(err.message, 'Bad data at location ' + bad_offset);
assert.deepEqual(result, {});
done();
2016-12-22 20:47:48 +00:00
}).catch(_.noop); // UnhandledPromiseRejectionWarning;
});
it('should handle very large offset', function(done) {
var bad_offset = offset + 100000000;
wordpos.seek(bad_offset, 'a', function(err, result) {
assert(err instanceof Error);
assert.equal(err.message, 'no data at offset ' + bad_offset);
assert.deepEqual(result, {});
done();
2016-12-22 20:47:48 +00:00
}).catch(_.noop); // UnhandledPromiseRejectionWarning;
});
it('should handle bad POS', function(done) {
wordpos.seek(offset, 'g', function(err, result) {
assert(err instanceof Error);
assert(/Incorrect POS/.test(err.message));
done();
2016-12-22 20:47:48 +00:00
}).catch(_.noop); // UnhandledPromiseRejectionWarning;
});
it('should handle wrong POS', function(done) {
wordpos.seek(offset, 'v', function(err, result){
assert.equal(err.message, 'Bad data at location ' + offset);
2016-12-22 20:47:48 +00:00
}).catch(_.noop); // UnhandledPromiseRejectionWarning;
done();
});
});
2016-01-18 08:09:56 +00:00
describe('Promise pattern', function() {
it('lookup()', function () {
return wordpos.lookup('hegemony').then(function (result) {
assert.equal(result.length, 1);
});
});
it('lookupX()', function () {
return wordpos.lookupNoun('hegemony').then(function (result) {
assert.equal(result.length, 1);
});
});
2014-05-03 21:41:39 +00:00
2016-01-18 08:09:56 +00:00
it('getPOS()', function () {
return wordpos.getPOS('hegemony').then(function (result) {
assert.equal(result.nouns.length, 1);
});
});
it('getX()', function () {
return wordpos.getVerbs('bear').then(function (result) {
assert.equal(result.length, 1);
});
});
it('isX()', function () {
return wordpos.isAdjective('little').then(function (result) {
assert.equal(result, true);
});
});
it('rand()', function () {
2018-03-04 20:04:06 +00:00
return wordpos.rand().then(function (result) {
assert.equal(result.length, 1);
});
});
it('rand({count})', function () {
return wordpos.rand({count: 5}).then(function (result) {
assert.equal(result.length, 5);
});
});
it('randNoun()', function () {
return wordpos.randNoun().then(function (result) {
assert.equal(result.length, 1);
});
});
it('randNoun({count: 3})', function () {
return wordpos.randNoun({count: 3}).then(function (result) {
assert.equal(result.length, 3);
});
});
it('randNoun({startsWith: "foo"})', function () {
return wordpos.randNoun({startsWith: 'foo'}).then(function (result) {
assert.equal(result.length, 1);
assert.equal(result[0].indexOf('foo'), 0);
});
});
it('seek()', function () {
return wordpos.seek(offset, 'a').then(function (result) {
assert.equal(result.synsetOffset, offset);
assert.equal(result.pos, 's');
assert.equal(result.lemma, 'amazing');
});
});
it('seek() - wrong offset', function () {
return wordpos.seek(offset + 1, 'a').catch(function (err) {
assert(err instanceof Error);
assert.equal(err.message, 'Bad data at location ' + (offset+1));
});
});
it('seek() - bad offset', function () {
return wordpos.seek('foobar', 'a').catch(function (err) {
assert(err instanceof Error);
2018-10-15 05:50:12 +00:00
assert.equal(err.message, 'Offset must be valid positive number: foobar');
});
});
2018-10-15 05:50:12 +00:00
});