Use Mocha to make tests more reliable

This commit is contained in:
Paul Melnikow 2017-03-27 22:24:02 -04:00 committed by Thaddee Tyl
parent 2e4721ea1f
commit 5f945d4856
3 changed files with 61 additions and 82 deletions

View File

@ -37,7 +37,7 @@
},
"scripts": {
"lint": "eslint '**/*.js'",
"test:js": "node test/test.js",
"test:js": "mocha test/test.js test/lru-cache.js",
"test": "npm run lint && npm run test:js"
},
"bin": {
@ -55,6 +55,7 @@
"logo"
],
"devDependencies": {
"eslint": "^3.18.0"
"eslint": "^3.18.0",
"mocha": "^3.2.0"
}
}

View File

@ -1,17 +1,21 @@
var assert = require('assert');
var LRU = require('../lib/lru-cache.js');
module.exports = [
["should support being called without new", function(done, assert) {
describe('The LRU cache', function () {
it("should support being called without new", function() {
var cache = LRU(1);
assert(cache instanceof LRU);
done();
}],
["should support a zero capacity", function(done, assert) {
});
it("should support a zero capacity", function() {
var cache = new LRU(0);
cache.set('key', 'value');
assert.equal(cache.cache.size, 0);
done();
}],
["should support a one capacity", function(done, assert) {
});
it("should support a one capacity", function() {
var cache = new LRU(1);
cache.set('key1', 'value1');
assert.equal(cache.cache.size, 1);
@ -23,10 +27,9 @@ module.exports = [
assert.equal(cache.oldest, cache.cache.get('key2'));
assert.equal(cache.get('key1'), undefined);
assert.equal(cache.get('key2'), 'value2');
done();
}],
["should remove the oldest element when reaching capacity",
function(done, assert) {
});
it("should remove the oldest element when reaching capacity", function() {
var cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
@ -45,10 +48,9 @@ module.exports = [
assert.equal(cache.get('key1'), undefined);
assert.equal(cache.get('key2'), 'value2');
assert.equal(cache.get('key3'), 'value3');
done();
}],
["should make sure that resetting a key in cache makes it newest",
function(done, assert) {
});
it("should make sure that resetting a key in cache makes it newest", function() {
var cache = new LRU(2);
cache.set('key', 'value');
cache.set('key2', 'value2');
@ -57,10 +59,9 @@ module.exports = [
cache.set('key', 'value');
assert.equal(cache.oldest, cache.cache.get('key2'));
assert.equal(cache.newest, cache.cache.get('key'));
done();
}],
["should make sure that getting a key in cache makes it newest",
function(done, assert) {
});
it("should make sure that getting a key in cache makes it newest", function() {
var slot1, slot2, slot3;
// When the key is oldest.
@ -134,6 +135,6 @@ module.exports = [
assert.equal(slot3.newer, slot2);
assert.equal(slot2.older, slot3);
assert.equal(slot2.newer, null);
done();
}],
];
});
});

View File

@ -1,45 +1,16 @@
var assertion = require('assert');
var assert = require('assert');
var http = require('http');
var cproc = require('child_process');
var fs = require('fs');
function test(target, tests) {
var wrappedTests = tests.map(function(test) {
return function() {
var desc = test[0];
var f = test[1];
return new Promise(function(resolve, reject) {
var assert = function(pred, msg) { assert.ok(pred, msg); };
['ok', 'equal', 'deepEqual', 'strictEqual', 'deepStrictEqual',
'notEqual', 'notDeepEqual', 'notStrictEqual', 'notDeepStrictEqual',
'fail', 'doesNotThrow', 'throws',
].forEach(function(k) {
assert[k] = function(...args) {
try {
assertion[k].apply(null, args);
} catch(e) { reject(e); }
};
});
f(resolve, assert);
}).catch(function(e) {
console.error('Failed:', target + ' ' + desc + '\n', e.stack);
});
};
});
var prom = wrappedTests[0]();
for (var i = 1; i < wrappedTests.length; i++) {
prom = prom.then(wrappedTests[i]);
}
return prom;
}
// Test parameters
var port = '1111';
var url = 'http://127.0.0.1:' + port + '/';
var server;
test('The CLI', [
['should provide a help message', function(done, assert) {
describe('The CLI', function () {
it('should provide a help message', function(done) {
var child = cproc.spawn('node', ['test/cli-test.js']);
var buffer = '';
child.stdout.on('data', function(chunk) {
@ -49,8 +20,9 @@ test('The CLI', [
assert(buffer.startsWith('Usage'));
done();
});
}],
['should produce default badges', function(done, assert) {
});
it('should produce default badges', function(done) {
var child = cproc.spawn('node',
['test/cli-test.js', 'cactus', 'grown']);
child.stdout.on('data', function(chunk) {
@ -60,8 +32,9 @@ test('The CLI', [
assert(buffer.includes('grown'), 'grown');
done();
});
}],
['should produce colorschemed badges', function(done, assert) {
});
it('should produce colorschemed badges', function(done) {
var child = cproc.spawn('node',
['test/cli-test.js', 'cactus', 'grown', ':green']);
child.stdout.on('data', function(chunk) {
@ -69,17 +42,19 @@ test('The CLI', [
assert(buffer.startsWith('<svg'), '<svg');
done();
});
}],
['should produce right-color badges', function(done, assert) {
var child = cproc.spawn('node',
});
it('should produce right-color badges', function(done) {
child = cproc.spawn('node',
['test/cli-test.js', 'cactus', 'grown', '#abcdef']);
child.stdout.on('data', function(chunk) {
var buffer = ''+chunk;
assert(buffer.includes('#abcdef'), '#abcdef');
done();
});
}],
['should produce PNG badges', function(done, assert) {
});
it('should produce PNG badges', function(done) {
var child = cproc.spawn('node',
['test/cli-test.js', 'cactus', 'grown', '.png']);
child.stdout.on('data', function(chunk) {
@ -94,21 +69,22 @@ test('The CLI', [
assert.equal(chunk[7], 0x0a);
done();
});
}],
])
});
.then(function() {
test('The server', [
// Start running the server.
['should start', function(done, assert) {
});
describe('The server', function () {
before('Start running the server', function(done) {
server = cproc.spawn('node', ['test/server-test.js', port]);
var isDone = false;
server.stdout.on('data', function(data) {
if (data.toString().indexOf('ready') >= 0 && !isDone) { done(); isDone = true; }
});
server.stderr.on('data', function(data) { console.log(''+data); });
}],
['should produce colorscheme badges', function(done, assert) {
});
it('should produce colorscheme badges', function(done) {
http.get(url + ':fruit-apple-green.svg',
function(res) {
var buffer = '';
@ -120,8 +96,9 @@ test('The server', [
done();
});
});
}],
['should produce colorscheme PNG badges', function(done, assert) {
});
it('should produce colorscheme PNG badges', function(done) {
http.get(url + ':fruit-apple-green.png',
function(res) {
res.on('data', function(chunk) {
@ -137,11 +114,11 @@ test('The server', [
done();
});
});
}],
['should shut down', function(done, assert) {
});
after('Shut down the server', function(done) {
server.kill();
server.on('exit', function() { done(); });
}],
]);})
});
.then(function() { test('The LRU cache', require('./lru-cache.js')); });
});