Skip to content

Commit

Permalink
More tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jul 8, 2016
1 parent c93cc91 commit e40c109
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 86 deletions.
7 changes: 3 additions & 4 deletions src/SheetsTemplater.gs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,16 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
sheet.getRange(endLine + 1, initialColumn + 1).setValue('');
sheet.getRange(endLine + 1, endColumn + 1).setValue('');
properties.values = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).getValues();

for (var i = initialLine + 1, index = 0; i < endLine + 2; i++, index++) {
for (var j = initialColumn + 2; j < endColumn; j++) {
for (var j = initialColumn + 2; j < endColumn + 1; j++) {
properties.i = i - 1;
properties.j = j - 1;
properties.json[entityName] = array[index];
QuickDrive.processCell(properties);

}
if (config.stripeFirst == !(index % 2)) {
sheet.getRange(i, 1, 1, sheet.getMaxColumns()).setBackground(config.stripeColor);
if (_config.stripeFirst == !(index % 2)) {
sheet.getRange(i, 1, 1, sheet.getMaxColumns()).setBackground(_config.stripeColor);
}
}

Expand Down
11 changes: 0 additions & 11 deletions test/mock/Cell.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/mock/DriveApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var File = require('./File.js').File;
var Folder = require('./Folder.js').Folder;
var File = require('./DriveApp/File.js').File;
var Folder = require('./DriveApp/Folder.js').Folder;

var DriveApp = function () {
return {
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 0 additions & 25 deletions test/mock/Range.js

This file was deleted.

34 changes: 0 additions & 34 deletions test/mock/Sheet.js

This file was deleted.

7 changes: 6 additions & 1 deletion test/mock/SpreadsheetApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var Spreadsheet = require('./Spreadsheet.js').Spreadsheet;
var Spreadsheet = require('./SpreadsheetApp/Spreadsheet.js').Spreadsheet;
var SpreadsheetApp = function (mock) {
return {
BorderStyle: {
DOTTED: 'DOTTED',
DASHED: 'DASHED',
SOLID: 'SOLID'
},
open: function (file) {
return new Spreadsheet(mock || file);
}
Expand Down
34 changes: 34 additions & 0 deletions test/mock/SpreadsheetApp/Cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var Cell = function (firstValue) {
var value = firstValue || "",
backgroundColor = 'white',
borderStyle = 'SOLID';

var links = {};

this.setValue = function (newValue) {
value = newValue;
};
this.getValue = function () {
return value;
};
this.setBackground = function(newBackground) {
backgroundColor = newBackground;
};
this._configureLinks = function(newLinks) {
links = newLinks;
};
this._left = function() {
return links.left;
};
this._bottom = function() {
return links.bottom;
};
this._right = function() {
return links.right;
};
this._top = function() {
return links.top;
};
};

exports.Cell = Cell;
49 changes: 49 additions & 0 deletions test/mock/SpreadsheetApp/Range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var Range = function (myMatrix) {
var matrix = myMatrix || [
[]
];
this.getValues = function () {
var values = [];
for (var i = 0; i < matrix.length; i++) {
values[i] = [];
for (var j = 0; j < matrix[i].length; j++) {
values[i][j] = matrix[i][j].getValue();
}
}
return values;
};

this.setValue = function (value) {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix.length; j++) {
matrix[i][j].setValue(value);
}
}
};

this._getCells = function() {
return matrix;
};
this.copyTo = function (anotherRange) {
var firstCell = anotherRange._getCells()[0][0];
var actualLineCell = firstCell;
for (var i = 0; i < matrix.length; i++) {
actualCell = actualLineCell;
for (var j = 0; j < matrix[0].length; j++) {
actualCell.setValue(matrix[i][j].getValue());
actualCell = actualCell._right();
}
actualLineCell = actualLineCell._bottom();
}
};

this.setBackground = function (background) {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[0].length; j++) {
matrix[i][j].setBackground(background);
}
}
};
};

exports.Range = Range;
82 changes: 82 additions & 0 deletions test/mock/SpreadsheetApp/Sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var Range = require('./Range').Range;
var Cell = require('./Cell').Cell;
var Sheet = function (myMatrix) {
var matrix = myMatrix || [
[]
];

var setupMatrixLinks = function () {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[i].length; j++) {
matrix[i][j]._configureLinks({
top: i != 0 ? matrix[i - 1][j] : undefined,
right: j != (matrix[i].length - 1) ? matrix[i][j + 1] : undefined,
bottom: i != (matrix.length - 1) ? matrix[i + 1][j] : undefined,
left: j != 0 ? matrix[i][j - 1] : undefined
});
}
}
};

setupMatrixLinks();
this.getRange = function (row, col, lines, cols) {
lines = lines || 1;
cols = cols || 1;
if (!row || !col || row <= 0 || col <= 0 || lines > matrix.length || cols > matrix[0].length) {
throw new Error('Invalid params to range.');
}
var newMatrix = [
[]
];
for (var i = 0; i < lines; i++) {
newMatrix[i] = [];
for (var j = 0; j < cols; j++) {
newMatrix[i][j] = matrix[i + row - 1][j + col - 1];
}
}
return new Range(newMatrix);
};

var createRow = function (qty) {
var row = [];
for (var i = 0; i < qty; i++) {
row.push(new Cell(''));
}
return row;
};

this.insertRowsBefore = function (row, qty) {
var newMatrix = [];
for (var i = 0; i < row - 1; i++) {
newMatrix.push(matrix[i]);
}
for (var i = 0; i < qty; i++) {
newMatrix.push(createRow(matrix[0].length));
}
for (var i = row - 1; i < matrix.length; i++) {
newMatrix.push(matrix[i]);
}

// for (var i =0; i < newMatrix.length;i++) {
// var a = '';
// for(var j =0;j < newMatrix[i].length;j++) {
// a = a + ' ' + newMatrix[i][j].getValue();
// }
// console.info(a);
// }
// console.info(newMatrix.length);
// console.info(newMatrix[0].length);
matrix = newMatrix;
setupMatrixLinks();
};

this.getMaxRows = function () {
return matrix.length;
};

this.getMaxColumns = function () {
return matrix[0].length;
};
};

exports.Sheet = Sheet;
File renamed without changes.
2 changes: 1 addition & 1 deletion test/sheetMockTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var SpreadsheetApp = require('./mock/Spreadsheet.js').Spreadsheet();
var SpreadsheetApp = require('./mock/SpreadsheetApp.js').SpreadsheetApp();
var DriveApp = require('./mock/DriveApp.js').DriveApp();
var assert = require('chai').assert;

Expand Down
55 changes: 47 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var DriveApp = require('./mock/DriveApp.js').DriveApp;
var Cell = require('./mock/Cell.js').Cell;
var Cell = require('./mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('../src/SheetsTemplater.gs').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp());
Expand Down Expand Up @@ -75,21 +75,60 @@ describe('QuickDrive functions', function () {
});
});

var matrixMock = [
[new Cell('asd'), new Cell('koko'), new Cell('{=this.my_header}'), new Cell('my left header')],
[new Cell('asd'), new Cell('koko'), new Cell('{=this.my_header}'), new Cell('my left header')],
[new Cell('asd'), new Cell('koko'), new Cell('{=this.my_header}'), new Cell('my left header')]
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('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 = {
my_header: 'header title'
reportHeader: 'My cool header',
user: {
name: 'Matheus',
lastName: 'Martins do Rego',
age: 20,
address: {
cep: '12345-678',
city: {
name: 'Campinas',
state: {
name: 'São Paulo',
country: {
name: 'Brasil'
}
}
}
}
},
curriculum: {
yearsWorking: 3,
languagesThatLikes: [
'Java',
'JavaScript',
'CSS',
'HTML',
'Python'
]
}
};
describe('QuickDrive functions', function () {
describe('get new sheet', function () {
it('it should return sheet with text replaced', function () {
var QuickDriveMock = QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMock));
var QuickDriveMock = QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var file = QuickDriveMock.processSheet(jsonMock);
console.info(file.sheet.getRange(1,1,3,4).getValues());
assert.equal(JSON.stringify([
['My cool header', 'My random text', 'another random text', ''],
['Matheus', 'Martins do Rego', 20, 'My user'],
['12345-678', 'Campinas', 'São Paulo', 'Brasil'],
['Languages that he likes', '', '', ''],
['', 'Java', '', ''],
['', 'JavaScript', '', ''],
['', 'CSS', '', ''],
['', 'HTML', '', ''],
['', 'Python', '', 'text that will stay on last line']
]), JSON.stringify(file.sheet.getRange(1, 1, file.sheet.getMaxRows(), file.sheet.getMaxColumns()).getValues()));
});
});
});

0 comments on commit e40c109

Please sign in to comment.