From fefa198bf26148a2eb3d5e98c4410db2dd0d0789 Mon Sep 17 00:00:00 2001 From: Johan Lorenzo Date: Mon, 25 May 2015 16:17:03 +0200 Subject: [PATCH] Add the ability to report duplicate IDs Fixes #4 --- lib/model/testcase.js | 6 ++++++ test/unit/model/testcase.js | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/model/testcase.js b/lib/model/testcase.js index 5af95e5..bc133bf 100644 --- a/lib/model/testcase.js +++ b/lib/model/testcase.js @@ -19,8 +19,12 @@ function Testcase(id, instructions, state, bug, userStory, variables) { if (Testcase._isOptionalFieldDefined(variables)) { this.variables = variables; } + + Testcase.alreadyDefinedIds.push(this.id); } +Testcase.alreadyDefinedIds = []; + Testcase.isValidState = function(candidate) { return VALID_STATES.indexOf(candidate) !== -1; }; @@ -32,6 +36,8 @@ Testcase._sanitizeId = function(candidate) { } finally { if (!/^[A-Za-z_.]+$/.test(candidate)) { errorHandler.add(new Error('"' + candidate + '" should contains only letters, dots, and underscores')); + } else if (Testcase.alreadyDefinedIds.indexOf(candidate) > -1) { + errorHandler.add(new Error('The id "' + candidate + '" is already used')); } return candidate; } diff --git a/test/unit/model/testcase.js b/test/unit/model/testcase.js index a170d98..14841c2 100644 --- a/test/unit/model/testcase.js +++ b/test/unit/model/testcase.js @@ -23,6 +23,7 @@ describe('Testcase parser', function () { beforeEach(function() { object = {}; + Testcase.alreadyDefinedIds.length = 0; }); it('should accept all attributes', function() { @@ -71,6 +72,11 @@ describe('Testcase parser', function () { var testcase = new Testcase('id', 'instructions'); assert.isUndefined(testcase.variables); }); + + it('should push the id to the array of already defined Ids', function() { + new Testcase('newId', 'instructions'); + assert.include(Testcase.alreadyDefinedIds, 'newId'); + }); }); describe('isValidState()', function() { @@ -113,6 +119,12 @@ describe('Testcase parser', function () { Testcase._sanitizeId('fxos test'); sinon.assert.calledWith(addMock, new Error('"fxos test" should contains only letters, dots, and underscores')); }); + + it('should add an error if the id is already defined', function() { + new Testcase('dupeId', 'instructions'); + Testcase._sanitizeId('dupeId'); + sinon.assert.calledWith(addMock, new Error('The ID "newId" is already used')); + }); }); describe('_sanitizeString()', function() {