diff --git a/package.json b/package.json index 1b6694e..d25b91e 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/lru-cache.js b/test/lru-cache.js index e23c7d0..efbd549 100644 --- a/test/lru-cache.js +++ b/test/lru-cache.js @@ -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(); - }], -]; + }); + +}); diff --git a/test/test.js b/test/test.js index 2cdc095..f9e0310 100644 --- a/test/test.js +++ b/test/test.js @@ -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('= 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')); }); +});