From 2de23c3d731eb969f02b3b4aac0d1da8ff4765c1 Mon Sep 17 00:00:00 2001 From: Harold Putman Date: Wed, 9 Nov 2016 21:27:26 -0500 Subject: [PATCH] Improve the test suite. Add more coverage. --- .gitignore | 6 ++-- .npmignore | 8 +++-- src/main.js | 19 +++++++----- test/main.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 1fa9bb5..4a1dbd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ +.hubot_history +coverage +dist node_modules npm-debug.log -dist -coverage -.hubot_history scripts diff --git a/.npmignore b/.npmignore index 2cdba68..27828e7 100644 --- a/.npmignore +++ b/.npmignore @@ -1,8 +1,10 @@ +.gitignore +.hubot_history +.travis.yml +coverage node_modules npm-debug.log +scripts src test -.travis.yml webpack.config.js -.gitignore -coverage diff --git a/src/main.js b/src/main.js index 6fcd69c..1ae2f23 100644 --- a/src/main.js +++ b/src/main.js @@ -38,11 +38,15 @@ module.exports = (robot) => { const taboo = new Map(); + loadList(robot.brain); + // Recall the Taboo list from brain - const taboolist = robot.brain.get("taboo"); - if (taboolist != null) { - for (let topic of taboolist) { - taboo.set(topic, new RegExp(`\\b${topic}\\b`, "i")); + function loadList(brain) { + const taboolist = brain.get("taboo"); + if (taboolist != null) { + for (let topic of taboolist) { + taboo.set(topic, new RegExp(`\\b${topic}\\b`, "i")); + } } } @@ -73,7 +77,7 @@ module.exports = (robot) => { /** * Stores the taboolist in brain. */ - function rememberList(brain) { + function saveList(brain) { const taboolist = []; taboo.forEach((re, topic) => { taboolist.push(topic); @@ -88,7 +92,7 @@ module.exports = (robot) => { const keyTopic = topic.toLowerCase(); if (taboo.delete(keyTopic)) { res.reply(capitalize(`${topic} is no longer taboo`)); - rememberList(res.robot.brain); + saveList(res.robot.brain); } else { res.reply(`Oops, ${topic} is not taboo`); } @@ -101,7 +105,7 @@ module.exports = (robot) => { const keyTopic = topic.toLowerCase(); if (!taboo.has(keyTopic)) { taboo.set(keyTopic, new RegExp(`\\b${topic}\\b`, "i")); - rememberList(res.robot.brain); + saveList(res.robot.brain); res.reply(capitalize(`${topic} is now taboo`)); } else { res.reply(`Oops, ${topic} is already taboo`); @@ -112,6 +116,7 @@ module.exports = (robot) => { * Lists all the taboo topics. */ function listTopics(res) { + loadList(res.robot.brain); // Not necessary, but helps for testing. if (taboo.size == 0) { res.reply("Nothing is taboo here."); } else { diff --git a/test/main.js b/test/main.js index 1684aa5..0e6085f 100644 --- a/test/main.js +++ b/test/main.js @@ -8,6 +8,7 @@ const helper = new Helper('../src/main.js'); // path to file you want to test class NewMockResponse extends Helper.Response { + random(items) { return items[0]; } @@ -17,7 +18,7 @@ describe('hubot', () => { let room; - beforeEach(function() { room = helper.createRoom({response: NewMockResponse})}); + beforeEach(function() { room = helper.createRoom({response: NewMockResponse});}); afterEach(function() { room.destroy()}); it('should respond when making topic taboo', (done) => { @@ -28,7 +29,30 @@ describe('hubot', () => { ]); done(); }); + }); + it('should handle untimely clearing of taboo', (done) => { + room.user.say('alice', 'hubot pimento is no longer taboo').then(() => { + expect(room.messages).to.eql([ + ['alice', 'hubot pimento is no longer taboo'], + ['hubot', '@alice Oops, pimento is not taboo'], + ]); + done(); + }); + }); + + it('should respond to mentions of taboo topics', (done) => { + room.user.say('becky', 'hubot make squirrel taboo').then(() => { + room.user.say('becky', 'I love me some squirrel').then(() => { + expect(room.messages).to.eql([ + ['becky', 'hubot make squirrel taboo'], + ['hubot', '@becky Squirrel is now taboo'], + ['becky', 'I love me some squirrel'], + ['hubot', '@becky Do not speak of squirrel.'] + ]); + done(); + }); + }); }); it('should respond when making topic no longer taboo', (done) => { @@ -44,4 +68,64 @@ describe('hubot', () => { }); }); }); + + it('should allow alternate wording', (done) => { + room.user.say('becky', 'hubot make lettuce taboo').then(() => { + room.user.say('becky', 'hubot lettuce is not taboo').then(() => { + expect(room.messages).to.eql([ + ['becky', 'hubot make lettuce taboo'], + ['hubot', '@becky Lettuce is now taboo'], + ['becky', 'hubot lettuce is not taboo'], + ['hubot', '@becky Lettuce is no longer taboo'], + ]); + done(); + }); + }); + }); + + it('should indicate repeating taboo', (done) => { + room.user.say('becky', 'hubot make tomatoe taboo').then(() => { + room.user.say('becky', 'hubot make tomatoe taboo').then(() => { + expect(room.messages).to.eql([ + ['becky', 'hubot make tomatoe taboo'], + ['hubot', '@becky Tomatoe is now taboo'], + ['becky', 'hubot make tomatoe taboo'], + ['hubot', '@becky Oops, tomatoe is already taboo'], + ]); + done(); + }); + }); + }); + + it('should report an empty taboo list', (done) => { + room.user.say('alice', 'hubot list taboo').then(() => { + expect(room.messages).to.eql([ + ['alice', 'hubot list taboo'], + ['hubot', '@alice Nothing is taboo here.'], + ]); + done(); + }); + }); + + it('should report a taboo list', (done) => { + room.robot.brain.set("taboo", ['synergy', 'actionable']); + room.user.say('alice', 'hubot list taboo').then(() => { + expect(room.messages).to.eql([ + ['alice', 'hubot list taboo'], + ['hubot', '@alice Taboo topics are: synergy, actionable'], + ]); + done(); + }); + }); + + it('should report a taboo list alternate wording', (done) => { + room.robot.brain.set("taboo", ['growth', 'headwinds']); + room.user.say('alice', 'hubot taboo list').then(() => { + expect(room.messages).to.eql([ + ['alice', 'hubot taboo list'], + ['hubot', '@alice Taboo topics are: growth, headwinds'], + ]); + done(); + }); + }); });