Skip to content

Commit

Permalink
Improve the test suite.
Browse files Browse the repository at this point in the history
Add more coverage.
  • Loading branch information
HaroldPutman committed Nov 10, 2016
1 parent 0ed6d9a commit 2de23c3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.hubot_history
coverage
dist
node_modules
npm-debug.log
dist
coverage
.hubot_history
scripts
8 changes: 5 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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
19 changes: 12 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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`);
}
Expand All @@ -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`);
Expand All @@ -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 {
Expand Down
86 changes: 85 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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();
});
});
});

0 comments on commit 2de23c3

Please sign in to comment.