Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nam Hoang Le committed Apr 11, 2021
1 parent bf7d80e commit 285f625
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/drawRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

/**
* @param {number[]} columns
* @param {string[]} columns
* @param {drawRow~border} border
* @returns {string}
*/
Expand Down
18 changes: 18 additions & 0 deletions test/drawRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
expect,
} from 'chai';
import drawRow from '../src/drawRow';

describe('drawRow', () => {
it('draws a row using parts', () => {
const parts = {
bodyJoin: '│',
bodyLeft: '║',
bodyRight: '║',
};

expect(drawRow([], parts)).to.equal('║║\n');
expect(drawRow(['a'], parts)).to.equal('║a║\n');
expect(drawRow(['a', ' b '], parts)).to.equal('║a│ b ║\n');
});
});
70 changes: 70 additions & 0 deletions test/padTableData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import makeConfig from '../src/makeConfig';
import padTableData from '../src/padTableData';

describe('padTableData', () => {
context('when no given userConfig', () => {
it('insert 01 whitespace character regardless of string whitespaces', () => {
const rows = [[' a ']];

expect(padTableData(rows, makeConfig(rows, undefined))).to.deep.equal([[' a ']]);
});
});

context('when given paddings in columnDefault', () => {
context('when no given column-specific paddings', () => {
it('use the columnDefault values', () => {
const rows = [['a']];

expect(padTableData(rows, makeConfig(rows, {columnDefault: {
paddingLeft: 2,
paddingRight: 3,
}}))).to.deep.equal([[' a ']]);
});
});

context('when given column-specific padding values', () => {
it('use column-specific padding values', () => {
const rows = [['a']];

expect(padTableData(rows, makeConfig(rows, {
columnDefault: {
paddingLeft: 2,
paddingRight: 3,
},
columns: {
0: {
paddingLeft: 4,
paddingRight: 5,
},
},
}))).to.deep.equal([[' a ']]);
});
});
});

context('when given multiple rows and columns', () => {
it('use corresponding column-specific padding values or fallback to the default padding values', () => {
const rows = [['a', 'b'], ['c', 'd']];

expect(padTableData(rows, makeConfig(rows, {
columnDefault: {
paddingLeft: 2,
paddingRight: 3,
},
columns: {
0: {
paddingLeft: 4,
},
1: {
paddingRight: 5,
},
},
}))).to.deep.equal([[' a ', ' b '], [' c ', ' d ']]);
});
});
});
18 changes: 18 additions & 0 deletions test/stringifyTableData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
expect,
} from 'chai';
import stringifyTableData from '../src/stringifyTableData';

describe('stringifyTableData', () => {
it('convert all cell values to strings', () => {
const rows = [[null, undefined, true, false],
[0, -3.141_59, Number.NaN, Number.POSITIVE_INFINITY],
[['a', 'b'], {cd: 1}]];

expect(stringifyTableData(rows)).to.deep.equal([
['null', 'undefined', 'true', 'false'],
['0', '-3.14159', 'NaN', 'Infinity'],
['a,b', '[object Object]'],
]);
});
});
63 changes: 63 additions & 0 deletions test/truncateTableData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import makeConfig from '../src/makeConfig';
import truncateTableData from '../src/truncateTableData';

describe('truncateTableData', () => {
context('when no given userConfig', () => {
it('not truncate at all', () => {
const rows = [['a'.repeat(100)]];

expect(truncateTableData(rows, makeConfig(rows, undefined))).to.deep.equal([['a'.repeat(100)]]);
});
});

context('when given truncate value in columnDefault', () => {
context('when no given column-specific truncate', () => {
it('use the columnDefault value', () => {
const rows = [['a'.repeat(100)]];

expect(truncateTableData(rows, makeConfig(rows, {columnDefault: {
truncate: 20,
}}))).to.deep.equal([['a'.repeat(17) + '...']]);
});
});

context('when given column-specific truncate value', () => {
it('use column-specific truncate value', () => {
const rows = [['a'.repeat(100)]];

expect(truncateTableData(rows, makeConfig(rows, {
columnDefault: {
truncate: 20,
},
columns: {
0: {
truncate: 30,
},
},
}))).to.deep.equal([['a'.repeat(27) + '...']]);
});
});
});

context('when given multiple rows and columns', () => {
it('use corresponding column-specific truncate values or fallback to the default truncate value', () => {
const rows = [['a'.repeat(100), 'b'.repeat(100)], ['c'.repeat(100), 'd'.repeat(100)]];

expect(truncateTableData(rows, makeConfig(rows, {
columnDefault: {
truncate: 20,
},
columns: {
0: {
truncate: 30,
},
},
}))).to.deep.equal([['a'.repeat(27) + '...', 'b'.repeat(17) + '...'], ['c'.repeat(27) + '...', 'd'.repeat(17) + '...']]);
});
});
});
104 changes: 104 additions & 0 deletions test/wrapCell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import wrapCell from '../src/wrapCell';
import wrapString from '../src/wrapString';
import wrapWord from '../src/wrapWord';

describe('wrapCell', () => {
const strings = ['aa bb cc', 'a a bb cccc', 'aaabbcc', 'a\\bb', 'a_bb', 'a-bb', 'a.bb', 'a,bb', 'a;bb'];

context('useWrapWord is enabled', () => {
context('the string does not contain the newline character', () => {
it('should return the same output as wrapWord\'s', () => {
for (const string of strings) {
expect(wrapCell(string, 3, true)).to.deep.equal(wrapWord(string, 3));
}
});
});

context('the string does contains the newline character', () => {
context('the length of lineChunk is smaller than the length of container', () => {
it('splits by the newlines', () => {
expect(wrapCell('\n', 5, true)).to.deep.equal(['', '']);
expect(wrapCell('a\n', 5, true)).to.deep.equal(['a', '']);
expect(wrapCell('\na', 5, true)).to.deep.equal(['', 'a']);
expect(wrapCell('\na\n', 5, true)).to.deep.equal(['', 'a', '']);
expect(wrapCell('a\na', 5, true)).to.deep.equal(['a', 'a']);
expect(wrapCell('a \na', 5, true)).to.deep.equal(['a', 'a']);

expect(wrapCell('\n\n', 5, true)).to.deep.equal(['', '', '']);
expect(wrapCell('a\n\n', 5, true)).to.deep.equal(['a', '', '']);
expect(wrapCell('\n\na', 5, true)).to.deep.equal(['', '', 'a']);
expect(wrapCell('a\n\nb', 5, true)).to.deep.equal(['a', '', 'b']);
expect(wrapCell('a\n\n\nb', 5, true)).to.deep.equal(['a', '', '', 'b']);
});
});

context('the length of lineChunk is longer than the length of container', () => {
it('continue cuts the word by wrapWord function', () => {
expect(wrapCell('aaa bbb\nc', 3, true)).to.deep.equal(['aaa', 'bbb', 'c']);
expect(wrapCell('a b c\nd', 3, true)).to.deep.equal(['a b', 'c', 'd']);

expect(wrapCell('aaaa\nbbbb', 3, true)).to.deep.equal(['aaa', 'a', 'bbb', 'b']);

expect(wrapCell('a\\bb\nc', 3, true)).to.deep.equal(['a\\', 'bb', 'c']);
expect(wrapCell('a/bb\nc', 3, true)).to.deep.equal(['a/', 'bb', 'c']);
expect(wrapCell('a_bb\nc', 3, true)).to.deep.equal(['a_', 'bb', 'c']);
expect(wrapCell('a-bb\nc', 3, true)).to.deep.equal(['a-', 'bb', 'c']);
expect(wrapCell('a.bb\nc', 3, true)).to.deep.equal(['a.', 'bb', 'c']);
expect(wrapCell('a,bb\nc', 3, true)).to.deep.equal(['a,', 'bb', 'c']);
expect(wrapCell('a;bb\nc', 3, true)).to.deep.equal(['a;', 'bb', 'c']);

expect(wrapCell('aaa-b\nc', 3, true)).to.deep.equal(['aaa', '-b', 'c']);
});
});
});
});

context('useWrapWord is disable', () => {
context('the string does not contain the newline character', () => {
it('should return the same output as wrapString\'s', () => {
for (const string of strings) {
expect(wrapCell(string, 3, false)).to.deep.equal(wrapString(string, 3));
}
});
});

context('the string contains the newline character', () => {
context('the length of lineChunk is smaller than the length of container', () => {
it('splits by the newlines and does not trim the chunks', () => {
expect(wrapCell('\n', 5, false)).to.deep.equal(['', '']);
expect(wrapCell(' a \n', 5, false)).to.deep.equal([' a ', '']);
expect(wrapCell('\n a ', 5, false)).to.deep.equal(['', ' a ']);
expect(wrapCell('\n a \n', 5, false)).to.deep.equal(['', ' a ', '']);
expect(wrapCell(' a \n b ', 5, false)).to.deep.equal([' a ', ' b ']);

expect(wrapCell('\n\n', 5, false)).to.deep.equal(['', '', '']);
expect(wrapCell(' a \n\n', 5, false)).to.deep.equal([' a ', '', '']);
expect(wrapCell('\n\n a ', 5, false)).to.deep.equal(['', '', ' a ']);
expect(wrapCell(' a \n\n b ', 5, false)).to.deep.equal([' a ', '', ' b ']);
expect(wrapCell(' a \n\n\n b ', 5, false)).to.deep.equal([' a ', '', '', ' b ']);
});
});

context('the length of lineChunk is longer than the length of container', () => {
it('cuts each chunkLine by wrapString and trim the small chunk if it starts with whitespace', () => {
expect(wrapCell(' \nb', 3, false)).to.deep.equal([' ', 'b']);
expect(wrapCell('a \nb', 3, false)).to.deep.equal(['a ', 'b']);
expect(wrapCell('aa \nb', 3, false)).to.deep.equal(['aa ', 'b']);
expect(wrapCell('aaa \nb', 3, false)).to.deep.equal(['aaa', 'b']);
expect(wrapCell('aaaa\nb', 3, false)).to.deep.equal(['aaa', 'a', 'b']);

expect(wrapCell('a b\nc', 3, false)).to.deep.equal(['a ', 'b', 'c']);
expect(wrapCell('a b\nc', 3, false)).to.deep.equal(['a ', 'b', 'c']);
expect(wrapCell('a b\nc', 3, false)).to.deep.equal(['a ', 'b', 'c']);

expect(wrapCell('aaa b\n c', 3, false)).to.deep.equal(['aaa', 'b', ' c']);
});
});
});
});
});

0 comments on commit 285f625

Please sign in to comment.