From 7d0abf064e8af3e1cd1d97afd56c917e3cb9e651 Mon Sep 17 00:00:00 2001 From: Andrew Sliwinski Date: Tue, 30 Jul 2013 11:14:12 -0700 Subject: [PATCH] Re-write / re-factor test suite --- makefile | 13 ++- test/{benchmark.js => benchmark/index.js} | 4 +- test/debt.js | 39 ------- test/governance/lint.js | 5 + test/index.js | 129 ---------------------- test/inject.js | 105 ------------------ test/unit/default_comments.js | 32 ++++++ test/unit/default_fuzz.js | 45 ++++++++ test/unit/default_negative.js | 18 +++ test/unit/default_positive.js | 17 +++ test/unit/inject_blacklist.js | 20 ++++ test/unit/inject_negative.js | 21 ++++ test/unit/inject_positive.js | 20 ++++ test/unit/interface.js | 7 ++ 14 files changed, 194 insertions(+), 281 deletions(-) rename test/{benchmark.js => benchmark/index.js} (96%) delete mode 100644 test/debt.js create mode 100644 test/governance/lint.js delete mode 100644 test/index.js delete mode 100644 test/inject.js create mode 100644 test/unit/default_comments.js create mode 100644 test/unit/default_fuzz.js create mode 100644 test/unit/default_negative.js create mode 100644 test/unit/default_positive.js create mode 100644 test/unit/inject_blacklist.js create mode 100644 test/unit/inject_negative.js create mode 100644 test/unit/inject_positive.js create mode 100644 test/unit/interface.js diff --git a/makefile b/makefile index a2a5d9b..18c9729 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,11 @@ build: node ./build/index.js -generator: - npm install - git init - git add -A - git remote add origin https://thisandagain@github.com/thisandagain/sentiment +test: + ./node_modules/.bin/tap test/governance/*.js + ./node_modules/.bin/tap test/unit/*.js -.PHONY: build generator \ No newline at end of file +benchmark: + node test/benchmark/index.js + +.PHONY: build test benchmark \ No newline at end of file diff --git a/test/benchmark.js b/test/benchmark/index.js similarity index 96% rename from test/benchmark.js rename to test/benchmark/index.js index 83fcb7f..570bb56 100644 --- a/test/benchmark.js +++ b/test/benchmark/index.js @@ -1,5 +1,5 @@ /** - * Test suite + * Naïve benchmark suite. * * @package sentiment * @author Andrew Sliwinski @@ -11,7 +11,7 @@ var async = require('async'), test = require('tap').test, - t1 = require(__dirname + '/../lib/index.js'), + t1 = require(__dirname + '/../../lib/index.js'), t2 = require('Sentimental'); /** diff --git a/test/debt.js b/test/debt.js deleted file mode 100644 index 70f9683..0000000 --- a/test/debt.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Unit test suite. - * - * @package generator - * @author Andrew Sliwinski - */ - -/** - * Dependencies - */ -var async = require('async'), - test = require('tap').test, - bux = require('codebux'); - -/** - * Suite - */ -async.auto({ - - debt: function (callback) { - bux(__dirname + '/../lib', callback); - }, - - test: ['debt', function (callback, obj) { - test('Debt', function (t) { - t.type(obj.debt, 'number', 'Results should be a number'); - t.ok(obj.debt > 50, 'Total should be greater than 50'); - t.end(); - }); - - callback(); - }] - -}, function (err, obj) { - test('Catch errors', function (t) { - t.equal(err, null, 'Errors should be null'); - t.end(); - }); -}); \ No newline at end of file diff --git a/test/governance/lint.js b/test/governance/lint.js new file mode 100644 index 0000000..22b347e --- /dev/null +++ b/test/governance/lint.js @@ -0,0 +1,5 @@ +var fs = require('fs'), + hint = require('hint-hint'); + +var config = fs.readFileSync(__dirname + '/../../.jshintrc'); +hint(__dirname + '/../../lib/*.js', JSON.parse(config)); \ No newline at end of file diff --git a/test/index.js b/test/index.js deleted file mode 100644 index 335d93d..0000000 --- a/test/index.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Test suite - * - * @package sentiment - * @author Andrew Sliwinski - */ - -/** - * Dependencies - */ -var async = require('async'), - test = require('tap').test, - - target = require(__dirname + '/../lib/index.js'); - -/** - * Suite - */ -async.auto({ - - negative: function (callback) { - target('Hey you worthless scumbag', callback); - }, - - positive: function (callback) { - target('This is so cool', callback); - }, - - n2: function (callback) { - target('Cats are stupid.', callback); - }, - - p2: function (callback) { - target('Cats are totally amazing!', callback); - }, - - bb: function (callback) { - var load = [ - 'In America I doubt it could sustain itself without descending into identity politics and ego.', - 'You are deluding yourself if you think that identity politics and ego are purely American.', - 'Posting a show that cost $5 and was for charity is pretty low and I thought better of BoingBoing. A clip or two would be great, but just pirating something like this is lame.', - 'I paid the $5, only got to see the last 30 minutes of the event, and cant download it today, because when I log in, it asks for my credit card information. So the only way it looks like Ill get to watch it is through a pirated copy.', - 'Thanks for posting it, Cory! What Ive seen of the debate was awesome, but from a technical and customer service standpoint, it was a catastrophe.', - 'Bill OReillys total inability to shut up while anyone else is speaking makes me either wonder how he ever got his job, or think that you can be inordinately successful just by bullying.' - ]; - - async.map(load, function (obj, callback) { - target(obj, callback); - }, callback); - }, - - fuzz: function (callback) { - function createRandomWord(length) { - var consonants = 'bcdfghjklmnpqrstvwxyz', - vowels = 'aeiou', - rand = function(limit) { - return Math.floor(Math.random()*limit); - }, - i, word='', length = parseInt(length,10), - consonants = consonants.split(''), - vowels = vowels.split(''); - for (i=0;i - */ - -/** - * Dependencies - */ -var async = require('async'), - test = require('tap').test, - - target = require(__dirname + '/../lib/index.js'); - -/** - * Suite - */ -async.auto({ - - negative: function (callback) { - target('Hey you worthless scumbag', { - worthless: -5, - scumbag: -5 - }, callback); - }, - - positive: function (callback) { - target('This is so cool', { - cool: 5 - }, callback); - }, - - blacklist: function (callback) { - target('I really like your badword cat!', { - badword: -100 - }, callback); - }, - - mix: function (callback) { - target('I really like your badword cat! It is super cool and amazing.', { - badword: -100 - }, callback); - }, - - p1: function (callback) { - target('Cats are totally amazing!', { - 'cats': 5, - 'amazing': 2 - }, callback); - }, - - test: ['negative', 'positive', 'blacklist', 'mix', 'p1', function (callback, obj) { - console.dir(obj); - - test('Component definition', function (t) { - t.type(target, 'function', 'Component should be a function'); - t.end(); - }); - - test('negative', function (t) { - t.type(obj.negative, 'object', 'Results should be an object'); - t.equal(obj.negative.score, -10, 'Expected score'); - t.equal(obj.negative.comparative, -2.5, 'Expected comparative score'); - t.equal(obj.negative.tokens.length, 4, 'Expected tokens length'); - t.equal(obj.negative.words.length, 2, 'Expected match length'); - t.end(); - }); - - test('positive', function (t) { - t.type(obj.positive, 'object', 'Results should be an object'); - t.equal(obj.positive.score, 5, 'Expected score'); - t.equal(obj.positive.comparative, 1.25, 'Expected comparative score'); - t.equal(obj.positive.tokens.length, 4, 'Expected tokens length'); - t.equal(obj.positive.words.length, 1, 'Expected match length'); - t.end(); - }); - - test('blacklist', function (t) { - t.type(obj.blacklist, 'object', 'Results should be an object'); - t.equal(obj.blacklist.score, -98, 'Expected score'); - t.equal(obj.blacklist.comparative, -16.333333333333332, 'Expected comparative score'); - t.equal(obj.blacklist.tokens.length, 6, 'Expected tokens length'); - t.equal(obj.blacklist.words.length, 2, 'Expected match length'); - t.end(); - }); - - test('mix', function (t) { - t.type(obj.mix, 'object', 'Results should be an object'); - t.equal(obj.mix.score, -86, 'Expected score'); - t.equal(obj.mix.comparative, -7.166666666666667, 'Expected comparative score'); - t.equal(obj.mix.tokens.length, 12, 'Expected tokens length'); - t.equal(obj.mix.words.length, 5, 'Expected match length'); - t.end(); - }); - - callback(); - }] - -}, function (err, obj) { - test('Catch errors', function (t) { - t.equal(err, null, 'Errors should be null'); - t.end(); - }); -}); diff --git a/test/unit/default_comments.js b/test/unit/default_comments.js new file mode 100644 index 0000000..9f7b974 --- /dev/null +++ b/test/unit/default_comments.js @@ -0,0 +1,32 @@ +var async = require('async'), + test = require('tap').test, + sentiment = require(__dirname + '/../../lib/index'); + +var load = [ + 'In America I doubt it could sustain itself without descending into identity politics and ego.', + 'You are deluding yourself if you think that identity politics and ego are purely American.', + 'Posting a show that cost $5 and was for charity is pretty low and I thought better of BoingBoing. A clip or two would be great, but just pirating something like this is lame.', + 'I paid the $5, only got to see the last 30 minutes of the event, and cant download it today, because when I log in, it asks for my credit card information. So the only way it looks like Ill get to watch it is through a pirated copy.', + 'Thanks for posting it, Cory! What Ive seen of the debate was awesome, but from a technical and customer service standpoint, it was a catastrophe.', + 'Bill OReillys total inability to shut up while anyone else is speaking makes me either wonder how he ever got his job, or think that you can be inordinately successful just by bullying.' +]; + +async.map(load, function (obj, callback) { + sentiment(obj, callback); +}, function (err, obj) { + console.dir(err); + console.dir(obj); + + test('unit', function (t) { + t.equal(err, null, 'error object should be null'); + t.type(obj, 'object', 'results should be an object'); + t.equal(obj.length, 6, 'results should be of expected length'); + + t.equal(obj[0].score, -1, 'Expected score'); + t.equal(obj[0].comparative, -0.06666666666666667, 'Expected comparative score'); + t.equal(obj[0].tokens.length, 15, 'Expected tokens length'); + t.equal(obj[0].words.length, 1, 'Expected match length'); + + t.end(); + }); +}); \ No newline at end of file diff --git a/test/unit/default_fuzz.js b/test/unit/default_fuzz.js new file mode 100644 index 0000000..72d46ef --- /dev/null +++ b/test/unit/default_fuzz.js @@ -0,0 +1,45 @@ +var async = require('async'), + test = require('tap').test, + sentiment = require(__dirname + '/../../lib/index'); + +function createRandomWord(length) { + var consonants = 'bcdfghjklmnpqrstvwxyz!@#$%^&*()_+":;\'?><~`', + vowels = 'aeiou', + rand = function(limit) { + return Math.floor(Math.random()*limit); + }, + i, word='', length = parseInt(length,10), + consonants = consonants.split(''), + vowels = vowels.split(''); + for (i=0;i