From c93cc915700c2b55949866c0c1464318b11cc490 Mon Sep 17 00:00:00 2001 From: Matheus Martins Date: Thu, 7 Jul 2016 01:12:52 -0300 Subject: [PATCH] Test changes and gapp --- gapps.config.json | 4 + package.json | 6 +- src/SheetsTemplater.gs | 1 - test/mock/Cell.js | 11 +++ test/mock/DriveApp.js | 28 +++++++ test/mock/DriveAppMock.js | 150 ------------------------------------ test/mock/File.js | 31 ++++++++ test/mock/Folder.js | 15 ++++ test/mock/Range.js | 25 ++++++ test/mock/Sheet.js | 34 ++++++++ test/mock/Spreadsheet.js | 9 +++ test/mock/SpreadsheetApp.js | 10 +++ test/mock/docsMock.js | 1 - test/sheetMockTest.js | 5 +- test/test.js | 17 ++-- 15 files changed, 181 insertions(+), 166 deletions(-) create mode 100644 gapps.config.json create mode 100644 test/mock/Cell.js create mode 100644 test/mock/DriveApp.js delete mode 100644 test/mock/DriveAppMock.js create mode 100644 test/mock/File.js create mode 100644 test/mock/Folder.js create mode 100644 test/mock/Range.js create mode 100644 test/mock/Sheet.js create mode 100644 test/mock/Spreadsheet.js create mode 100644 test/mock/SpreadsheetApp.js delete mode 100644 test/mock/docsMock.js diff --git a/gapps.config.json b/gapps.config.json new file mode 100644 index 0000000..b2cf90f --- /dev/null +++ b/gapps.config.json @@ -0,0 +1,4 @@ +{ + "path": "src", + "fileId": "1tVx2IimBlEy63KKegPL_-a7vd0OLyfCfyz6GIi15Q96XIlgN6Yhcpxdm" +} \ No newline at end of file diff --git a/package.json b/package.json index b8f33c1..0095c00 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,13 @@ "url": "git@github.com:matheusmr13/quickdrive.git" }, "scripts": { - "test": "mocha" + "test": "mocha", + "update": "gapps upload" }, "dependencies": { "chai": "^3.5.0", - "mocha": "^2.5.3" + "mocha": "^2.5.3", + "node-google-apps-script": "^1.1.5" }, "devDependencies": {} } diff --git a/src/SheetsTemplater.gs b/src/SheetsTemplater.gs index 31e4b39..7319431 100644 --- a/src/SheetsTemplater.gs +++ b/src/SheetsTemplater.gs @@ -1,6 +1,5 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) { var QuickDrive = {}; - QuickDrive.annotationFunctions = { REPLACE_TEXT: replaceValue, FOR_EACH: processForEach, diff --git a/test/mock/Cell.js b/test/mock/Cell.js new file mode 100644 index 0000000..c1bf50c --- /dev/null +++ b/test/mock/Cell.js @@ -0,0 +1,11 @@ +var Cell = function (firstValue) { + var value = firstValue || ""; + this.setValue = function (newValue) { + value = newValue; + }; + this.getValue = function () { + return value; + }; +}; + +exports.Cell = Cell; diff --git a/test/mock/DriveApp.js b/test/mock/DriveApp.js new file mode 100644 index 0000000..fb0743e --- /dev/null +++ b/test/mock/DriveApp.js @@ -0,0 +1,28 @@ +var File = require('./File.js').File; +var Folder = require('./Folder.js').Folder; + +var DriveApp = function () { + return { + Access: { + ANYONE: 'ANYONE', + ANYONE_WITH_LINK: 'ANYONE_WITH_LINK', + DOMAIN: 'DOMAIN', + DOMAIN_WITH_LINK: 'DOMAIN_WITH_LINK', + PRIVATE: 'PRIVATE' + }, + Permission: { + COMMENT: 'COMMENT', + EDIT: 'EDIT', + NONE: 'NONE', + OWNER: 'OWNER', + VIEW: 'VIEW' + }, + getFileById: function (id) { + return new File(id); + }, + getFolderById: function (id) { + return new Folder(id); + } + }; +}; +exports.DriveApp = DriveApp; diff --git a/test/mock/DriveAppMock.js b/test/mock/DriveAppMock.js deleted file mode 100644 index 2c60ec6..0000000 --- a/test/mock/DriveAppMock.js +++ /dev/null @@ -1,150 +0,0 @@ -var DriveApp = function () { - var DriveFolder = function (newId) { - var files = [], - id = newId || ''; - this.getId = function () { - return id; - }; - this.addFile = function (file) { - files.push(file); - }; - this.getFiles = function () { - return files; - }; - }; - - var DriveFile = function (newId) { - var properties = { - id: newId || '', - name: '' - }; - - this.setSharing = function () {}; - this.getId = function () { - return properties.id; - }; - this.makeCopy = function (name, folder) { - var newFile = new DriveFile(); - newFile._setProperties(properties); - newFile.setName(name); - folder.addFile(newFile); - return newFile; - }; - this.getName = function () { - return properties.name; - }; - this.setName = function (newName) { - properties.name = newName; - }; - this._setProperties = function (newProperties) { - for (var propertie in newProperties) { - properties[propertie] = newProperties[propertie]; - } - }; - }; - - return { - Access: { - ANYONE: 'ANYONE', - ANYONE_WITH_LINK: 'ANYONE_WITH_LINK', - DOMAIN: 'DOMAIN', - DOMAIN_WITH_LINK: 'DOMAIN_WITH_LINK', - PRIVATE: 'PRIVATE' - }, - Permission: { - COMMENT: 'COMMENT', - EDIT: 'EDIT', - NONE: 'NONE', - OWNER: 'OWNER', - VIEW: 'VIEW' - }, - getFileById: function (id) { - return new DriveFile(id); - }, - getFolderById: function (id) { - return new DriveFolder(id); - } - }; -}; - -var SpreadsheetApp = function (mock) { - var SpreadSheetMock = function (mock) { - var SheetMock = function (myMatrix) { - var matrix = myMatrix; - - var CellMock = function () { - var value = ""; - this.setValue = function (newValue) { - this.value = newValue; - }; - - this.getValue = function () { - return value; - }; - }; - - var CellGroupMock = function (matrix) { - var matrix = matrix || [ - [] - ]; - - this.getValues = function (row, col, lines, cols) { - var values = []; - for (var i = row; i < lines; i++) { - values[i] = []; - for (var j = col; j < cols; j++) { - values[i][j] = matrix[i][j]; - } - } - 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] = value; - } - } - }; - }; - - this.getRange = function (row, col, lines, cols) { - var newMatrix = [ - [] - ]; - for (var i = row; i < lines; i++) { - newMatrix[i] = []; - for (var j = col; j < cols; j++) { - newMatrix[i][j]; - } - } - return new CellGroupMock(newMatrix); - }; - - this.getMaxRows = function () { - return matrix.length; - }; - - this.getMaxColumns = function () { - return matrix[0].length; - }; - }; - - var sheets = [new SheetMock(mock)]; - - this.getSheets = function () { - return sheets; - }; - }; - - return { - open: function (file) { - return new SpreadSheetMock(mock); - } - } -}; - -exports.DriveAppMock = { - DriveApp: DriveApp, - SpreadsheetApp: SpreadsheetApp -}; diff --git a/test/mock/File.js b/test/mock/File.js new file mode 100644 index 0000000..1a1b354 --- /dev/null +++ b/test/mock/File.js @@ -0,0 +1,31 @@ +var File = function (newId) { + var properties = { + id: newId || '', + name: '' + }; + + this.setSharing = function () {}; + this.getId = function () { + return properties.id; + }; + this.makeCopy = function (name, folder) { + var newFile = new File(); + newFile._setProperties(properties); + newFile.setName(name); + folder.addFile(newFile); + return newFile; + }; + this.getName = function () { + return properties.name; + }; + this.setName = function (newName) { + properties.name = newName; + }; + this._setProperties = function (newProperties) { + for (var propertie in newProperties) { + properties[propertie] = newProperties[propertie]; + } + }; +}; + +exports.File = File; diff --git a/test/mock/Folder.js b/test/mock/Folder.js new file mode 100644 index 0000000..5f59ffa --- /dev/null +++ b/test/mock/Folder.js @@ -0,0 +1,15 @@ +var Folder = function (newId) { + var files = [], + id = newId || ''; + this.getId = function () { + return id; + }; + this.addFile = function (file) { + files.push(file); + }; + this.getFiles = function () { + return files; + }; +}; + +exports.Folder = Folder; diff --git a/test/mock/Range.js b/test/mock/Range.js new file mode 100644 index 0000000..1f9aafa --- /dev/null +++ b/test/mock/Range.js @@ -0,0 +1,25 @@ +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); + } + } + }; +}; + +exports.Range = Range; diff --git a/test/mock/Sheet.js b/test/mock/Sheet.js new file mode 100644 index 0000000..e1e7e5b --- /dev/null +++ b/test/mock/Sheet.js @@ -0,0 +1,34 @@ +var Range = require('./Range').Range; +var Sheet = function (myMatrix) { + var matrix = myMatrix || [ + [] + ]; + + 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); + }; + + this.getMaxRows = function () { + return matrix.length; + }; + + this.getMaxColumns = function () { + return matrix[0].length; + }; +}; + +exports.Sheet = Sheet; diff --git a/test/mock/Spreadsheet.js b/test/mock/Spreadsheet.js new file mode 100644 index 0000000..1a5d5b5 --- /dev/null +++ b/test/mock/Spreadsheet.js @@ -0,0 +1,9 @@ +var Sheet = require('./Sheet.js').Sheet +var Spreadsheet = function (mock) { + var sheets = [new Sheet(mock)]; + + this.getSheets = function () { + return sheets; + }; +}; +exports.Spreadsheet = Spreadsheet; diff --git a/test/mock/SpreadsheetApp.js b/test/mock/SpreadsheetApp.js new file mode 100644 index 0000000..19fcd6d --- /dev/null +++ b/test/mock/SpreadsheetApp.js @@ -0,0 +1,10 @@ +var Spreadsheet = require('./Spreadsheet.js').Spreadsheet; +var SpreadsheetApp = function (mock) { + return { + open: function (file) { + return new Spreadsheet(mock || file); + } + } +}; + +exports.SpreadsheetApp = SpreadsheetApp; diff --git a/test/mock/docsMock.js b/test/mock/docsMock.js deleted file mode 100644 index 420ed93..0000000 --- a/test/mock/docsMock.js +++ /dev/null @@ -1 +0,0 @@ -var DocApp = {} diff --git a/test/sheetMockTest.js b/test/sheetMockTest.js index b36037a..e555a8f 100644 --- a/test/sheetMockTest.js +++ b/test/sheetMockTest.js @@ -1,6 +1,5 @@ -var mock = require('./mock/DriveAppMock.js').DriveAppMock; -var SpreadsheetApp = mock.SpreadsheetApp(); -var DriveApp = mock.DriveApp(); +var SpreadsheetApp = require('./mock/Spreadsheet.js').Spreadsheet(); +var DriveApp = require('./mock/DriveApp.js').DriveApp(); var assert = require('chai').assert; describe('DriveApp sheet mock test', function () { diff --git a/test/test.js b/test/test.js index 70cad3d..fb8ed84 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ -evar DriveAppMock = require('./mock/DriveAppMock.js').DriveAppMock; -var DriveApp = DriveAppMock.DriveApp; -var SpreadsheetApp = DriveAppMock.SpreadsheetApp; +var DriveApp = require('./mock/DriveApp.js').DriveApp; +var Cell = require('./mock/Cell.js').Cell; +var SpreadsheetApp = require('./mock/SpreadsheetApp.js').SpreadsheetApp; var QuickDriveConstructor = require('../src/SheetsTemplater.gs').QuickDrive; var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp()); var chai = require('chai'); @@ -76,21 +76,20 @@ describe('QuickDrive functions', function () { }); var matrixMock = [ - ['asd', 'koko', '{=this.my_header}', 'my left header'], - ['asd', 'koko', '{=this.my_header}', 'my left header'], - ['asd', 'koko', '{=this.my_header}', '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')], + [new Cell('asd'), new Cell('koko'), new Cell('{=this.my_header}'), new Cell('my left header')] ]; var jsonMock = { - this: { - my_header: 'header title' - } + my_header: 'header title' }; 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 file = QuickDriveMock.processSheet(jsonMock); + console.info(file.sheet.getRange(1,1,3,4).getValues()); }); }); });