diff --git a/package.json b/package.json index 9dd0bff..facc191 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/template.js b/src/utils/template.js new file mode 100644 index 0000000..9ca6cb1 --- /dev/null +++ b/src/utils/template.js @@ -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(), + }; +} diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..4a52320 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +--recursive diff --git a/test/utils/template.spec.js b/test/utils/template.spec.js new file mode 100644 index 0000000..bea9d79 --- /dev/null +++ b/test/utils/template.spec.js @@ -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; + }); + } + }); +});