Skip to content
Browse files

Add tests

  • Loading branch information...
1 parent cf9f2e1 commit 8f28e9bc4263a5861c6d6d5aac6e49da9ef8de59 @eldargab committed Mar 11, 2012
Showing with 100 additions and 4 deletions.
  1. +12 −2 lib/table.js
  2. +4 −2 package.json
  3. +84 −0 test/table.js
View
14 lib/table.js
@@ -22,21 +22,31 @@ Table.prototype.cell = function (col, obj, printer, length) {
var s = c.printer(obj);
c.length = s.length > c.length ? s.length : c.length;
}
+ return this;
}
Table.prototype.newLine = function () {
this.lines.push(this.line);
this.line = {};
+ return this;
}
Table.prototype.toString = function () {
var self = this;
var pad = Table.RightPadder();
var padWithDashs = Table.RightPadder('-');
+
+ function printCell (s, length) {
+ if (s.length <= length) return pad(s, length);
+ s = s.slice(0, length);
+ if (length > 3) s = s.slice(0, -3).concat('...');
+ return s;
+ }
+
var s = '';
s += this._printLine(function (key, col) {
- return pad(key, col.length);
+ return printCell(key, col.length);
});
s += this._printLine(function (key, col) {
@@ -45,7 +55,7 @@ Table.prototype.toString = function () {
this.lines.forEach(function (line) {
s += self._printLine(function (key, col) {
- return pad(col.printer(line[key], col.length), col.length);
+ return printCell(col.printer(line[key], col.length), col.length);
});
});
View
6 package.json
@@ -8,6 +8,8 @@
"url": "git://github.com:eldargab/easy-table.git"
},
"main": "lib/table.js",
- "dependencies": {},
- "devDependencies": {}
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ }
}
View
84 test/table.js
@@ -0,0 +1,84 @@
+var should = require('should');
+var Table = require('../lib/table');
+
+describe('Easy table', function () {
+ var t;
+
+ beforeEach(function () {
+ t = new Table;
+ });
+
+ function expectLine (line) {
+ line--;
+ return t.toString().split('\n')[line].should;
+ }
+
+ it('test', function () {
+ t.cell('First column', '11');
+ t.cell('Second column', '12');
+ t.newLine();
+
+ t.cell('First column', '21');
+ t.cell('Second column', '22');
+ t.newLine();
+
+ t.toString().should.equal(
+ 'First column' + t.shift + 'Second column' + '\n' +
+ '------------' + t.shift + '-------------' + '\n' +
+ '11 ' + t.shift + '12 ' + '\n' +
+ '21 ' + t.shift + '22 ' + '\n'
+ );
+ });
+
+ it('Should adjust column width to fit all contents', function () {
+ t.cell('col', '').newLine();
+ expectLine(1).be.equal('col');
+
+ t.cell('col', 'value').newLine();
+ expectLine(1).be.equal('col ');
+ });
+
+ describe('Should accept print function as third parameter to .cell() method and call it two times', function () {
+ it('First time to determine minimal width', function () {
+ var callCount = 0;
+ function print (obj) {
+ obj.should.equal(10);
+ if (callCount == 0) arguments.length.should.equal(1);
+ callCount++;
+ return obj.toString();
+ }
+ t.cell('col', 10, print);
+ });
+
+ it('Second time asking to render actual value passing additional length parameter', function () {
+ var callCount = 0;
+ function print (obj, length) {
+ obj.should.equal(10);
+ if (callCount == 1) length.should.equal(4);
+ callCount++;
+
+ if (arguments.length == 1) return '10 ';
+ return ' 10 ';
+ }
+ t.cell('col', 10, print).newLine();
+ expectLine(3).be.equal(' 10 ');
+ });
+ });
+
+ describe('Should accept column length as 4-th parameter to .cell() method. In such case:', function () {
+ it('Column length should be fixed', function () {
+ t.cell('col', 'value', null, 10).newLine();
+ expectLine(3).be.equal('value ');
+ });
+
+ it('If cell value doesn`t fit column width it should be trancated', function () {
+ t.cell('col', 'A very long value', null, 14).newLine();
+ expectLine(3).be.equal('A very long...');
+ });
+ });
+
+ it('test Table.padLeft()', function () {
+ Table.padLeft('a', 2).should.equal(' a');
+ });
+});
+

0 comments on commit 8f28e9b

Please sign in to comment.
Something went wrong with that request. Please try again.