From 3afff4b44b79f7191eab47dbbe19acd93e40fb7e Mon Sep 17 00:00:00 2001 From: Matheus Date: Sun, 19 Jun 2016 16:22:31 -0300 Subject: [PATCH] More test coverage --- SheetsTemplater.gs | 31 +++++++++++++++++++++++++++---- test/test.js | 14 ++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/SheetsTemplater.gs b/SheetsTemplater.gs index d38cdd2..a1c4f70 100644 --- a/SheetsTemplater.gs +++ b/SheetsTemplater.gs @@ -4,7 +4,7 @@ var QuickDrive = (function (config, json) { QuickDrive.annotationFunctions = { REPLACE_TEXT: replaceValue, FOR_EACH: processForEach, - NONE: function(properties) { + NONE: function (properties) { return; } }; @@ -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; } @@ -141,5 +164,5 @@ function doGet(e) { }; if (typeof module !== 'undefined' && module.exports != null) { - exports.QuickDrive = QuickDrive; + exports.QuickDrive = QuickDrive; } diff --git a/test/test.js b/test/test.js index 9460c25..2e2d3e1 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () { @@ -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}')); }); - }); });