Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "a command line interface to manage harvest and target-process in one place",
"main": "src/index.js",
"scripts": {
"test": "mocha --recursive"
"test": "mocha"
},
"repository": {
"type": "git",
Expand Down
37 changes: 37 additions & 0 deletions src/utils/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports.buildTemplate = buildTemplate;
module.exports.extractData = extractData;



function buildTemplate(data){

if(data.projectId == null || data.userStoryId == null || data.taskId == null){
return null;
}

var string = '#' + data.projectId + ' - #' + data.userStoryId + ' - #' + data.taskId;

if(data.notes){
string += ' ' + data.notes;
}

return string;
}


var extractRegex = /#([0-9]+)\s*-\s*#([0-9]+)\s*-\s*#([0-9]+)\s*((.|\s)*)/;

function extractData(template){
var matches = template.match(extractRegex);

if(!matches){
return null;
}

return {
projectId: +matches[1],
userStoryId: +matches[2],
taskId: +matches[3],
notes: matches[4].trim(),
};
}
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--recursive
90 changes: 90 additions & 0 deletions test/utils/template.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var expect = require("chai").expect;
var template = require('../../src/utils/template');

describe('template', function(){

describe('extractData', function(){

testExtractData('#1000 - #999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000 - #999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000 - #999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000 - #999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000 - #999 - #998 notes blah blah blah 000', 1000, 999, 998, 'notes blah blah blah 000');
testExtractData('#1000 - #999 - #998 notes blah blah blah 000 ', 1000, 999, 998, 'notes blah blah blah 000');
testExtractData('#1000 - #999 - #998', 1000, 999, 998, '');
testExtractData('#1000 - #999 - #998 ', 1000, 999, 998, '');
testExtractData('#1000-#999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000- #999 - #998 notes', 1000, 999, 998, 'notes');
testExtractData('#1000 -#999 - #998 notes', 1000, 999, 998, 'notes');


failExtractData('#1000 #999 notes');


function testExtractData(text, expectedProjectId, expectedUserStoryId, expectedTaskId, expectedNotes){
describe('extract data from: \'' + text + '\'', function(){
var data = template.extractData(text);

it('should get the project id', function(){
expect(data).to.have.property('projectId', expectedProjectId);
});
it('should get the user story id', function(){
expect(data).to.have.property('userStoryId', expectedUserStoryId);
});
it('should get the task id', function(){
expect(data).to.have.property('taskId', expectedTaskId);
});
it('should get the notes', function(){
expect(data).to.have.property('notes', expectedNotes);
});
});
}

function failExtractData(text){
it('should fail extracting data from: \'' + text + '\'', function(){
expect(template.extractData(text)).to.be.null;
});
}

});


describe('buildTemplate', function(){

testBuildTemplate(1000, 999, 888, null, '#1000 - #999 - #888');
testBuildTemplate(1000, 999, 888, 'notes', '#1000 - #999 - #888 notes');
testBuildTemplate(1000, 999, 888, 'notes blah blah', '#1000 - #999 - #888 notes blah blah');
testBuildTemplate(1000, 999, 888, 8, '#1000 - #999 - #888 8');


failBuildTemplate(1000, 999, null, null);
failBuildTemplate(1000, null, 888, null);
failBuildTemplate(null, 999, 888, null);

function testBuildTemplate(projectId, userStoryId, taskId, notes, expectedText){

describe('build template using' + projectId + ', ' + userStoryId + ', ' + taskId,function(){

it('should return the template', function(){
expect(template.buildTemplate({
projectId: projectId,
userStoryId: userStoryId,
taskId: taskId,
notes: notes,
})).to.be.equal(expectedText);
});
});
}

function failBuildTemplate(projectId, userStoryId, taskId, notes){
it('should fail building the template', function(){
expect(template.buildTemplate({
projectId: projectId,
userStoryId: userStoryId,
taskId: taskId,
notes: notes,
})).to.be.null;
});
}
});
});