Skip to content

Commit

Permalink
initial feature implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Boyd committed Jun 10, 2015
1 parent 7f66ee4 commit 5d8c13b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 76 deletions.
28 changes: 0 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)

### Node template
# Logs
logs
*.log

test/results

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules


2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ report-generator encapsulates the task of creating simple html reports.
##quick example
```javascript

var reportGenerator = require('report-generator');

var generator new reportGenerator('report.html', ['col1', 'col2'], function (err) {
assert(err === null);
});
Expand Down
51 changes: 35 additions & 16 deletions lib/report-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,17 @@ var fs = require('fs');
self._reportFileName = reportFileName;
self._columnNames = columnNames;

var headers = '';
self._columnNames.forEach(function (columnName) {
headers = headers.concat('<TD><pre>' + columnName + '</pre></TD>');
});

var block = '<HTML>' +
'<BODY>' +
'<Table align="center" border=.5 cellpadding=3 cellspacing=.5>' +
'<TR align=center>' +
headers +
'</TR>';
'<Table id="reportTable" align="center" border=.5 cellpadding=3 cellspacing=.5>';

self.report = self.report.concat(block);
next(null);

//todo write a proper header row
self.writeRows(columnNames, next);
}

ReportGenerator.prototype.writeRow = function (rows, next) {
ReportGenerator.prototype.writeRows = function (rows, next) {

var self = this;

Expand All @@ -61,9 +55,27 @@ var fs = require('fs');

var cells = '<TR>';
rows.forEach(function (row) {
cells = cells.concat('<TD><pre>' + row + '</pre></TD>');

if (typeof row === 'string' || row instanceof String){
cells = cells.concat('<TD><pre>' + row + '</pre></TD>');
}else {
row.forEach(function(cell){

var backgroundColor = '#FFFFFF';
if(cell.color){
backgroundColor = cell.color;
}

var content = cell;
if(cell.cellContent){
content = cell.cellContent;
}

cells = cells.concat('<TD bgcolor="' + backgroundColor + '"><pre>' + content + '</pre></TD>');
});
}
cells = cells.concat('</TR>');
});
cells = cells.concat('</TR>');

self.report = self.report.concat(cells);
next(null);
Expand All @@ -78,11 +90,18 @@ var fs = require('fs');
'</HTML>';

self.report = self.report.concat(block);
/* jshint camelcase:false */
var prettyHtml = html.prettyPrint(self.report, {indent_size: 2});

// get string size in bytes
var stringByteSize = Buffer.byteLength(self.report, 'utf8');

// pretty if string is less than half a MB
if(stringByteSize < 500000){
/* jshint camelcase:false */
block = html.prettyPrint(self.report, {indent_size: 2});
}

try {
fs.writeFile(self._reportFileName, prettyHtml, next);
fs.writeFile(self._reportFileName, block, next);
}
catch (ex) {
return next(new Error(ex));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "report-generator",
"version": "1.0.2",
"version": "1.0.3",
"description": "generates html reports ",
"main": "./lib/report-generator",
"scripts": {
Expand All @@ -24,6 +24,7 @@
"devDependencies": {
"chai": "^2.3.0",
"chai-as-promised": "^5.0.0",
"cheerio": "^0.19.0",
"coveralls": "^2.11.2",
"grunt": "^0.4.5",
"grunt-cli": "^0.1.13",
Expand Down
150 changes: 119 additions & 31 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

/*global describe, it, before*/

/*global describe, it, before, beforeEach, after*/
var fs = require('fs');
var cheerio = require('cheerio');
var assert = require('assert');
var chai = require('chai');
chai.expect();
Expand All @@ -11,14 +12,21 @@ var reportGenerator = require('../lib/report-generator');

describe('report generator', function () {

var testReportName = 'testReport.html';
var publicFunctionTestReportName = 'publicFunctionTest.html';
var writeRowTestReportName = 'writeRowTestReport.html';

describe('public functions', function () {

// cleanup
after(function () {
//fs.unlinkSync(publicFunctionTestReportName);
//fs.unlinkSync(writeRowTestReportName);
});

describe('ReportGenerator', function () {

it('calls back with no error when properly invoked', function (done) {
new reportGenerator(testReportName, ['foo'], function (err) {
new reportGenerator(publicFunctionTestReportName, ['foo'], function (err) {
assert(err === null);
done();
});
Expand All @@ -44,14 +52,14 @@ describe('report generator', function () {
describe('validates columnNames parameter', function () {

it('calls back with error if columnNames is null', function (done) {
new reportGenerator(testReportName, null, function (err) {
new reportGenerator(publicFunctionTestReportName, null, function (err) {
assert(err !== null);
done();
});
});

it('calls back with error if columnNames is not an array', function (done) {
new reportGenerator(testReportName, 'foo', function (err) {
new reportGenerator(publicFunctionTestReportName, 'foo', function (err) {
assert(err !== null);
done();
});
Expand All @@ -62,14 +70,14 @@ describe('report generator', function () {

it('should not throw when next is invalid', function (done) {
(function () {
new reportGenerator(testReportName, ['foo'], '');
new reportGenerator(publicFunctionTestReportName, ['foo'], '');
done();
}).should.not.throw(Error);
});

it('should treat next as optional', function (done) {
(function () {
new reportGenerator(testReportName, ['foo']);
new reportGenerator(publicFunctionTestReportName, ['foo']);
done();
}).should.not.throw(Error);
});
Expand All @@ -88,65 +96,135 @@ describe('report generator', function () {
});
});

describe('writeRow', function () {
describe('writeRows', function () {

var generator;

before(function () {
generator = new reportGenerator(testReportName, ['foo'], function (err) {
generator = new reportGenerator(publicFunctionTestReportName, ['foo'], function (err) {
assert(err === null);
});
});

describe('validates row parameter', function () {

it('calls back with error if row is null', function (done) {
generator.writeRow(null, function (err) {
generator.writeRows(null, function (err) {
assert(err !== null);
done();
});
});

it('calls back with error if row is not an array', function (done) {
generator.writeRow('foo', function (err) {
generator.writeRows('foo', function (err) {
assert(err !== null);
done();
});
});
});

describe('writes multiple rows', function(){
describe('has an optional callback', function () {

it('writes multiple rows', function(done){
it('should not throw when next is invalid', function (done) {
(function () {
generator.writeRows(['1'], {});
done();
}).should.not.throw(Error);
});

generator.writeRow(['1'], function (err) {
assert(err === null);
});
it('should treat next as optional', function (done) {
(function () {
generator.writeRows(['1']);
done();
}).should.not.throw(Error);
});
});

describe('accepts an array of strings or objects', function () {

it('accepts an array of arrays containting strings', function (done) {

generator.writeRow(['2'], function (err) {
var cells = [
"1"
];

var row = [cells];

generator.writeRows(row, function (err) {
assert(err === null);
done();
});
});

it('accepts an array, containing an array of objects', function (done) {

generator.closeReport(done);
var cells = [
{cellContent: '1', color: "red"},
{cellContent: '2', color: "red"}
];

var row = [cells];

generator.writeRows(row, function (err) {
assert(err === null);
done();
});
});

});

describe('has an optional callback', function () {
describe('adds rows to the report table', function () {

it('should not throw when next is invalid', function (done) {
(function () {
generator.writeRow(['1'], {});
done();
}).should.not.throw(Error);
var colCount = 5;
var rowCount = 1000;

before(function (done) {

this.timeout(20000);

// create headers for the table
var headers = [];
for (var colCounter = 0; colCounter < colCount; colCounter++) {
var content = "header " + colCounter;
headers.push({cellContent: content, color: "red"});
}

// create generator
var writeRowsGenerator;
writeRowsGenerator = new reportGenerator(writeRowTestReportName, [headers], function (err) {
assert(err === null);
});

// add rows to report
for (var rowCounter = 0; rowCounter < rowCount; rowCounter++) {

var row = [];

for (colCounter = 0; colCounter < colCount; colCounter++) {
var cellContent = "row " + rowCounter + " cell " + colCounter;
row.push({cellContent: cellContent});
}

writeRowsGenerator.writeRows([row]);
}

writeRowsGenerator.closeReport(done);
});

it('should treat next as optional', function (done) {
(function () {
generator.writeRow(['1']);
it('adds the correct number of rows', function (done) {

fs.readFile(writeRowTestReportName, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}

var $ = cheerio.load(data);
var tableRows = $('table tr').length;

// todo update, right now the header is just another row
assert(tableRows === rowCount + 1, 'table row count was: ' + tableRows + ' expected: ' + rowCount + 1);
done();
}).should.not.throw(Error);
});
});
});
});
Expand All @@ -155,8 +233,8 @@ describe('report generator', function () {

var generator;

before(function () {
generator = new reportGenerator(testReportName, ['foo'], function (err) {
beforeEach(function () {
generator = new reportGenerator(publicFunctionTestReportName, ['foo'], function (err) {
assert(err === null);
});
});
Expand All @@ -178,6 +256,16 @@ describe('report generator', function () {
});
});

describe('creates the report', function () {

it('creates an html file', function () {
generator.closeReport();

fs.exists(publicFunctionTestReportName, function (exists) {
assert(exists === true);
});
});
});
});
});
});

0 comments on commit 5d8c13b

Please sign in to comment.