Skip to content

Commit

Permalink
More test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jun 19, 2016
1 parent 01d07d2 commit 3afff4b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
31 changes: 27 additions & 4 deletions SheetsTemplater.gs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var QuickDrive = (function (config, json) {
QuickDrive.annotationFunctions = {
REPLACE_TEXT: replaceValue,
FOR_EACH: processForEach,
NONE: function(properties) {
NONE: function (properties) {
return;
}
};
Expand Down Expand Up @@ -41,9 +41,32 @@ var QuickDrive = (function (config, json) {
return text[0] == '{' && text[text.length - 1] == '}';
};

QuickDrive.getAnnotationType = function(text) {
var isValidAnnotation = function (text) {
if (text.length < 4) {
return false;
}
var insideText = text.substring(2, text.length - 1),
textParts = insideText.split('.'),
validRegex = /^\w+$/;

for (var i = 0; i < textParts.length; i++) {
if (!textParts[i]) {
return false;
}
if (!validRegex.test(textParts[i])) {
return false;
}
}
return true;
};

QuickDrive.getAnnotationType = function (text) {
if (isAnottation(text)) {
return annotationType[text[1]] || QuickDrive.annotationFunctions.NONE;
if (isValidAnnotation(text)) {
return annotationType[text[1]] || QuickDrive.annotationFunctions.NONE;
} else {
return QuickDrive.annotationFunctions.NONE;
}
} else {
return QuickDrive.annotationFunctions.NONE;
}
Expand Down Expand Up @@ -141,5 +164,5 @@ function doGet(e) {
};

if (typeof module !== 'undefined' && module.exports != null) {
exports.QuickDrive = QuickDrive;
exports.QuickDrive = QuickDrive;
}
14 changes: 12 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ DriveApp.Access = {};
DriveApp.Permission = {};

var QuickDrive = require('../SheetsTemplater.gs').QuickDrive;
console.info(QuickDrive);
var assert = require('chai').assert;

describe('QuickDrive', function () {
describe('getAnnotationType', function () {
it('it should return none annotation, just simple text', function () {
Expand All @@ -65,14 +65,24 @@ describe('QuickDrive', function () {
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('another one}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{with no action}'));
});
it('it should return none annotation (invalid annotation present)', function () {
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=@foo.bar}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=@bar}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=foo..bar}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=.foo.bar}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=.foobar}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=foobar.}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{=foo@bar}'));
});
it('it should return simple replace text', function () {
assert.equal(QuickDrive.annotationFunctions.REPLACE_TEXT, QuickDrive.getAnnotationType('{=foo.bar}'));
assert.equal(QuickDrive.annotationFunctions.REPLACE_TEXT, QuickDrive.getAnnotationType('{=foo}'));
assert.equal(QuickDrive.annotationFunctions.REPLACE_TEXT, QuickDrive.getAnnotationType('{=foo.bar.text.with.many.properties}'));
});
it('it should return for each annotation', function () {
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~mylist}'));
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.myList}'));
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.bar.with.many.properties.myList}'));
});

});
});

0 comments on commit 3afff4b

Please sign in to comment.