Skip to content

Commit

Permalink
superdooper refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Aug 18, 2016
1 parent e2c82a2 commit dda9c2e
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 153 deletions.
11 changes: 11 additions & 0 deletions src/QuickDrive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var SheetTemplater = require('./SheetTemplater.js').SheetTemplater;

var QuickDrive = function (DriveApp, SpreadsheetApp) {
this.SheetTemplater = function(config) {
return new SheetTemplater(DriveApp, SpreadsheetApp, config);
};
};

if (typeof module !== 'undefined' && module.exports != null) {
exports.QuickDrive = QuickDrive;
}
7 changes: 7 additions & 0 deletions src/Router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function doPost(e) {
var json = e ? JSON.parse(e.parameters.data[0]) : {};
var config = e ? (e.parameters.config ? JSON.parse(e.parameters.config[0]) : {}) : {};
var QuickDriveObj = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newFile = QuickDriveObj.processSheet(json);
return ContentService.createTextOutput(newFile.fileId);
};
17 changes: 5 additions & 12 deletions src/SheetsTemplater.js → src/SheetTemplater.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
var SheetTemplater = function (DriveApp, SpreadsheetApp, newConfig) {
var annotationFunctions = {
REPLACE_TEXT: replaceValue,
FOR_EACH: processForEach,
Expand Down Expand Up @@ -31,11 +31,11 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
};
this._config = _config;
var validateConfig = function (config) {
if (config.folderId && (typeof config.folderId != 'string' || config.folderId.length != 18)) {
if (config.folderId && (typeof config.folderId != 'string' || config.folderId.length != 28)) {
throw new Error('invalid-folder-id');
}

if (config.templateId && (typeof config.templateId != 'string' || config.templateId.length != 45)) {
if (config.templateId && (typeof config.templateId != 'string' || config.templateId.length != 44)) {
throw new Error('invalid-file-id');
}

Expand All @@ -50,6 +50,7 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
throw new Error('invalid-stripe-color');
}
};
this.validateConfig = validateConfig;

if (newConfig) {
validateConfig(newConfig);
Expand Down Expand Up @@ -263,14 +264,6 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
};
};

function doPost(e) {
var json = e ? JSON.parse(e.parameters.data[0]) : {};
var config = e ? (e.parameters.config ? JSON.parse(e.parameters.config[0]) : {}) : {};
var QuickDriveObj = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newFile = QuickDriveObj.processSheet(json);
return ContentService.createTextOutput(newFile.fileId);
};

if (typeof module !== 'undefined' && module.exports != null) {
exports.QuickDrive = QuickDrive;
exports.SheetTemplater = SheetTemplater;
}
6 changes: 5 additions & 1 deletion test/mock/SpreadsheetApp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var Spreadsheet = require('./SpreadsheetApp/Spreadsheet.js').Spreadsheet;
var SpreadsheetApp = function (mock) {
var _mock = mock;
return {
_setupMock: function(mock) {
_mock = mock;
},
BorderStyle: {
DOTTED: 'DOTTED',
DASHED: 'DASHED',
SOLID: 'SOLID'
},
open: function (file) {
return new Spreadsheet(mock || file);
return new Spreadsheet(_mock || file);
}
}
};
Expand Down
24 changes: 8 additions & 16 deletions test/spreadsheetTemplater/annotations/allProperties.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
var DriveApp = require('./../../mock/DriveApp.js').DriveApp;
var Cell = require('./../../mock/SpreadsheetApp/Cell.js').Cell;
var Range = require('./../../mock/SpreadsheetApp/Range.js').Range;
var SpreadsheetApp = require('./../../mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('../../../src/SheetsTemplater.js').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp(), new Range());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

var matrixMockWithReplaceAnnotations = [
[new Cell('{*this.propertyCell}'), new Cell('{*this.anotherPropertyCell}')],
[new Cell('{*this.long.obj.reference.cell}'), new Cell('')]
];
var matrixMockWithReplaceAnnotations = function() {
return [
[new Cell('{*this.propertyCell}'), new Cell('{*this.anotherPropertyCell}')],
[new Cell('{*this.long.obj.reference.cell}'), new Cell('')]
];
};

var jsonMock = {
propertyCell: {
Expand Down Expand Up @@ -42,8 +34,8 @@ var jsonMock = {

describe('test specific annotations', function () {
it('all properties annotation', function () {
var QuickDriveMock = new QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var file = QuickDriveMock.processSheet(jsonMock);
SpreadsheetApp._setupMock(matrixMockWithReplaceAnnotations());
var file = SheetTemplater.processSheet(jsonMock);
var range = file.sheet._processFormulas().getRange(1,1,2,2).getCells();
var firstCell = range[0][0];
var secondCell = range[0][1];
Expand Down
25 changes: 9 additions & 16 deletions test/spreadsheetTemplater/annotations/forEach.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
var DriveApp = require('./../../mock/DriveApp.js').DriveApp;
var Cell = require('./../../mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./../../mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('../../../src/SheetsTemplater.js').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

var matrixMockWithReplaceAnnotations = [
[new Cell('Languages that he likes'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('{~this.curriculum.languagesThatLikes : languages}'), new Cell('{=languages}'), new Cell('{~}'), new Cell('text that will stay on last line')],
[new Cell('-'), new Cell('-'), new Cell('-'), new Cell('-')],
];
var matrixMockWithReplaceAnnotations = function() {
return [
[new Cell('Languages that he likes'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('{~this.curriculum.languagesThatLikes : languages}'), new Cell('{=languages}'), new Cell('{~}'), new Cell('text that will stay on last line')],
[new Cell('-'), new Cell('-'), new Cell('-'), new Cell('-')],
];
};

var jsonMock = {
curriculum: {
Expand All @@ -26,8 +19,8 @@ var jsonMock = {
};
describe('forEach annotation', function () {
it('it should return sheet forEach replaced', function () {
var QuickDriveMock = new QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var file = QuickDriveMock.processSheet(jsonMock);
SpreadsheetApp._setupMock(matrixMockWithReplaceAnnotations());
var file = SheetTemplater.processSheet(jsonMock);
assert.equal(JSON.stringify([
['Languages that he likes', '', '', ''],
['', 'Java', '', ''],
Expand Down
55 changes: 23 additions & 32 deletions test/spreadsheetTemplater/annotations/mainTest.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
var DriveApp = require('./../../mock/DriveApp.js').DriveApp;
var Cell = require('./../../mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./../../mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('./../../../src/SheetsTemplater.js').QuickDrive;
var QuickDrive = new QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

describe('getAnnotationType', function () {
it('it should return none annotation, just simple text', function () {
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('==simple text with no annotation=='));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{another simple'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('another one}'));
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('{with no action}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('==simple text with no annotation=='));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{another simple'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('another one}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.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}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=@foo.bar}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=@bar}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=foo..bar}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=.foo.bar}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=.foobar}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.getAnnotationType('{=foobar.}'));
assert.equal(SheetTemplater.annotationFunctions.NONE, SheetTemplater.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}'));
assert.equal(SheetTemplater.annotationFunctions.REPLACE_TEXT, SheetTemplater.getAnnotationType('{=foo.bar}'));
assert.equal(SheetTemplater.annotationFunctions.REPLACE_TEXT, SheetTemplater.getAnnotationType('{=foo}'));
assert.equal(SheetTemplater.annotationFunctions.REPLACE_TEXT, SheetTemplater.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}'));
assert.equal(SheetTemplater.annotationFunctions.FOR_EACH, SheetTemplater.getAnnotationType('{~mylist}'));
assert.equal(SheetTemplater.annotationFunctions.FOR_EACH, SheetTemplater.getAnnotationType('{~foo.myList}'));
assert.equal(SheetTemplater.annotationFunctions.FOR_EACH, SheetTemplater.getAnnotationType('{~foo.bar.with.many.properties.myList}'));
});
it('it should insert formula annotation', function () {
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#myFormula}'));
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#my.formula}'));
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#foo.bar.with.many.properties.myList}'));
assert.equal(SheetTemplater.annotationFunctions.INSERT_FORMULA, SheetTemplater.getAnnotationType('{#myFormula}'));
assert.equal(SheetTemplater.annotationFunctions.INSERT_FORMULA, SheetTemplater.getAnnotationType('{#my.formula}'));
assert.equal(SheetTemplater.annotationFunctions.INSERT_FORMULA, SheetTemplater.getAnnotationType('{#foo.bar.with.many.properties.myList}'));
});
it('it should set matrix annotation', function () {
assert.equal(QuickDrive.annotationFunctions.SET_MATRIX, QuickDrive.getAnnotationType('{+myMatrix}'));
assert.equal(QuickDrive.annotationFunctions.SET_MATRIX, QuickDrive.getAnnotationType('{+my.matrix}'));
assert.equal(QuickDrive.annotationFunctions.SET_MATRIX, QuickDrive.getAnnotationType('{+foo.bar.with.many.properties.myMatrix}'));
assert.equal(SheetTemplater.annotationFunctions.SET_MATRIX, SheetTemplater.getAnnotationType('{+myMatrix}'));
assert.equal(SheetTemplater.annotationFunctions.SET_MATRIX, SheetTemplater.getAnnotationType('{+my.matrix}'));
assert.equal(SheetTemplater.annotationFunctions.SET_MATRIX, SheetTemplater.getAnnotationType('{+foo.bar.with.many.properties.myMatrix}'));
});
});

Expand Down
40 changes: 40 additions & 0 deletions test/spreadsheetTemplater/basicFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
describe('QuickDrive functions', function () {
describe('validateConfig', function () {
var createQuickDriveWithConfig = function (propertie, value) {
var obj = {};
obj[propertie] = value;
return function () {
SheetTemplater.validateConfig(obj);
};
};
it('it should have default config with no error', function () {
SheetTemplater.validateConfig(SheetTemplater._config);
});
it('it should validate fileId', function () {
assert.throws(createQuickDriveWithConfig('templateId', 'myshortid'), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', 2313123), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', true), Error, 'invalid-file-id');
createQuickDriveWithConfig('fileId', '123456789012345678901234567890123456789012345')();
});
it('it should validate folderId', function () {
assert.throws(createQuickDriveWithConfig('folderId', 'myshortid'), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', 2313123), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', true), Error, 'invalid-folder-id');
createQuickDriveWithConfig('folderId', '1234567890123456789012345678')();
});
it('it should validate file name', function () {
assert.throws(createQuickDriveWithConfig('newDocumentName', 2313123), Error, 'invalid-file-name');
assert.throws(createQuickDriveWithConfig('newDocumentName', true), Error, 'invalid-file-name');
createQuickDriveWithConfig('newDocumentName', 'my new name')();
});
it('it should validate stripe color', function () {
assert.throws(createQuickDriveWithConfig('stripeColor', 2313123), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', true), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '#3333'), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '3333'), Error, 'invalid-stripe-color');
createQuickDriveWithConfig('stripeColor', '#333333')();
createQuickDriveWithConfig('stripeColor', '#333')();
createQuickDriveWithConfig('stripeColor', 'rgb(123,123,123)')();
});
});
});
31 changes: 12 additions & 19 deletions test/spreadsheetTemplater/macro/mainTest.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
var DriveApp = require('./../../mock/DriveApp.js').DriveApp;
var Cell = require('./../../mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./../../mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('../../../src/SheetsTemplater.js').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

var matrixMockWithReplaceAnnotations = [
[new Cell('{=this.reportHeader}'), new Cell('My random text'), new Cell('another random text'), new Cell('')],
[new Cell('{=this.user.name}'), new Cell('{=this.user.lastName}'), new Cell('{=this.user.age}'), new Cell('My user')],
[new Cell('{=this.user.address.cep}'), new Cell('{=this.user.address.city.name}'), new Cell('{=this.user.address.city.state.name}'), new Cell('{=this.user.address.city.state.country.name}')],
[new Cell('{#this.myFormula}'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('Languages that he likes'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('{~this.curriculum.languagesThatLikes : languages}'), new Cell('{=languages}'), new Cell('{~}'), new Cell('text that will stay on last line')]
];
var matrixMockWithReplaceAnnotations = function() {
return [
[new Cell('{=this.reportHeader}'), new Cell('My random text'), new Cell('another random text'), new Cell('')],
[new Cell('{=this.user.name}'), new Cell('{=this.user.lastName}'), new Cell('{=this.user.age}'), new Cell('My user')],
[new Cell('{=this.user.address.cep}'), new Cell('{=this.user.address.city.name}'), new Cell('{=this.user.address.city.state.name}'), new Cell('{=this.user.address.city.state.country.name}')],
[new Cell('{#this.myFormula}'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('Languages that he likes'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('{~this.curriculum.languagesThatLikes : languages}'), new Cell('{=languages}'), new Cell('{~}'), new Cell('text that will stay on last line')]
];
};

var jsonMock = {
reportHeader: 'My cool header',
Expand Down Expand Up @@ -50,8 +43,8 @@ var jsonMock = {
describe('QuickDrive functions', function () {
describe('get new sheet', function () {
it('it should return sheet with text replaced', function () {
var QuickDriveMock = new QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var file = QuickDriveMock.processSheet(jsonMock);
SpreadsheetApp._setupMock(matrixMockWithReplaceAnnotations());
var file = SheetTemplater.processSheet(jsonMock);
assert.equal(JSON.stringify([
['My cool header', 'My random text', 'another random text', ''],
['Matheus', 'Martins do Rego', '20', 'My user'],
Expand Down
55 changes: 3 additions & 52 deletions test/spreadsheetTemplater/mainTest.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,4 @@
var DriveApp = require('./../mock/DriveApp.js').DriveApp;
var Cell = require('./../mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./../mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('./../../src/SheetsTemplater.js').QuickDrive;
var QuickDrive = new QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

describe('QuickDrive functions', function () {
describe('validateConfig', function () {
var createQuickDriveWithConfig = function (propertie, value) {
var obj = {};
obj[propertie] = value;
return function () {
QuickDriveConstructor(DriveApp(), SpreadsheetApp(), obj);
};
};
it('it should have default config with no error', function () {
QuickDriveConstructor(DriveApp(), QuickDrive._config);
});
it('it should validate fileId', function () {
assert.throws(createQuickDriveWithConfig('templateId', 'myshortid'), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', 2313123), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', true), Error, 'invalid-file-id');
createQuickDriveWithConfig('fileId', '123456789012345678901234567890123456789012345')();
});
it('it should validate folderId', function () {
assert.throws(createQuickDriveWithConfig('folderId', 'myshortid'), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', 2313123), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', true), Error, 'invalid-folder-id');
createQuickDriveWithConfig('folderId', '123456789012345678')();
});
it('it should validate file name', function () {
assert.throws(createQuickDriveWithConfig('newDocumentName', 2313123), Error, 'invalid-file-name');
assert.throws(createQuickDriveWithConfig('newDocumentName', true), Error, 'invalid-file-name');
createQuickDriveWithConfig('newDocumentName', 'my new name')();
});
it('it should validate stripe color', function () {
assert.throws(createQuickDriveWithConfig('stripeColor', 2313123), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', true), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '#3333'), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '3333'), Error, 'invalid-stripe-color');
createQuickDriveWithConfig('stripeColor', '#333333')();
createQuickDriveWithConfig('stripeColor', '#333')();
createQuickDriveWithConfig('stripeColor', 'rgb(123,123,123)')();
});
});
});

require('./basicFunctions.js');
require('./sheetMockTest.js');
require('./annotations/mainTest.js');
require('./macro/mainTest.js');
require('./sheetMockTest.js');
require('./macro/mainTest.js');
Loading

0 comments on commit dda9c2e

Please sign in to comment.