5 ms·
Write a damn unit test for your padding function. We should share unit tests, not one-line libs. Libs with unit tests can be one-liners and be as fine-grained a
by kislotnik 10y ago
Write a damn unit test for your padding function. We should share unit tests, not one-line libs.
Libs with unit tests can be one-liners and be as fine-grained as we like, since it's something your users don't have to download
- nailer 10y ago> We should share unit tests, not one-line libs. Most of the small libs I've shared are mainly sharing unit tests. Here's a function I've shared on npm: var kind = function(item) { var getPrototype = function(item) { return Object.prototype.toString.call(item).slice(8, -1); }; var kind, Undefined; if (item === null ) { kind = 'null'; } else { if ( item === Undefined ) { kind = 'undefined'; } else { var prototype = getPrototype(item); if ( ( prototype === 'Number' ) && isNaN(item) ) { kind = 'NaN'; } else { kind = prototype; } } } return kind; }; The tests: suite('kind', function(){ test('shows number-like things as numbers', function(){ assert(avkind(37) === 'Number'); assert(avkind(3.14) === 'Number'); assert(avkind(Math.LN2) === 'Number'); assert(avkind(Infinity) === 'Number'); assert(avkind(Number(1)) === 'Number'); assert(avkind(new Number(1)) === 'Number'); }); test('shows NaN as NaN', function(){ assert(avkind(NaN) === 'NaN'); }); test('Shows strings as strings', function(){ assert(avkind('') === 'String'); assert(avkind('bla') === 'String'); assert(avkind(String("abc")) === 'String'); assert(avkind(new String("abc")) === 'String'); }); test('shows strings accurately', function(){ assert(avkind(true) === 'Boolean'); assert(avkind(false) === 'Boolean'); assert(avkind(new Boolean(true)) === 'Boolean'); }); test('shows arrays accurately', function(){ assert(avkind([1, 2, 4]) === 'Array'); assert(avkind(new Array(1, 2, 3)) === 'Array'); }); test('shows objects accurately', function(){ assert(avkind({a:1}) === 'Object'); assert(avkind(new Object()) === 'Object'); }); test('shows dates accurately', function(){ assert(avkind(new Date()) === 'Date'); }); test('loves Functions too', function(){ assert(avkind(function(){}) === 'Function'); assert(avkind(new Function("console.log(arguments)")) === 'Function'); assert(avkind(Math.sin) === 'Function'); }); test('shows undefined accurately', function(){ assert(avkind(undefined) === 'undefined'); }); test('shows null accurately', function(){ assert(avkind(null) === 'null'); }); });