Skip to content

Commit

Permalink
Added more-legitimate tests + code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Jul 31, 2014
1 parent 745bd36 commit 1c70278
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 82 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
Thumbs.db
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
script:
- make test
- make test-ci-coverage
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.PHONY: test test-ci-coverage

MOCHA=node_modules/.bin/mocha
COVERALLS=node_modules/.bin/coveralls
_MOCHA=node_modules/.bin/_mocha
ISTANBUL=node_modules/.bin/istanbul

test:
npm test

test-ci-coverage:
npm install coveralls
npm install istanbul
@rm -rf coverage
$(ISTANBUL) cover $(_MOCHA) --report lcovonly -- -R tap

@echo
@echo Sending report to coveralls.io...
@cat ./coverage/lcov.info | $(COVERALLS)
@rm -rf ./coverage
@echo Done
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# tinycache
[![NPM version](https://badge.fury.io/js/tinycache.png)](http://badge.fury.io/js/tinycache)
[![Build Status](https://travis-ci.org/andyburke/tinycache.png?branch=master)](https://travis-ci.org/andyburke/tinycache)
[![Coverage Status](https://coveralls.io/repos/andyburke/tinycache/badge.png)](https://coveralls.io/r/andyburke/tinycache)

A simple, small (~100 lines) in-memory cache for node.js or the browser (~1.5KB minified).

Expand Down
29 changes: 18 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
{
"name" : "tinycache",
"description" : "A simple in-memory cache in ~100 lines.",
"author" : "Andy Burke <aburke@bitflood.org>",
"url" : "https://github.com/andyburke/tinycache",
"name": "tinycache",
"description": "A simple in-memory cache in ~100 lines.",
"author": "Andy Burke <aburke@bitflood.org>",
"url": "https://github.com/andyburke/tinycache",
"contributors": [
{
"name": "Paul Tarjan",
"email": "npm@paulisageek.com",
"web": "https://github.com/ptarjan"
"web": "https://github.com/ptarjan"
}
],
"keywords" : [
"keywords": [
"cache",
"in-memory",
"storage"
],
"main" : "./tinycache.js",
"version" : "0.1.1",
"repository" : {
"type" : "git",
"url" : "git://github.com/andyburke/tinycache.git"
"main": "./tinycache.js",
"version": "0.1.1",
"scripts": {
"test": "mocha -R list"
},
"repository": {
"type": "git",
"url": "git://github.com/andyburke/tinycache.git"
},
"devDependencies": {
"mocha": "^1.21.3",
"chai": "^1.9.1"
}
}
60 changes: 0 additions & 60 deletions test.js

This file was deleted.

134 changes: 134 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
var assert = require('chai').assert;
var TinyCache = require('../tinycache.js');
var i = 0, new_key = function() { return 'key_' + (++i); };

var suite = function(cache) {
describe('size()', function() {
it('should return 0 when empty', function() {
cache.clear();
assert.equal(cache.size(), 0);
});
it('should return size', function() {
cache.clear();
cache.put(new_key(), 1, 10);
assert.equal(cache.size(), 1);
});
it('should return correct value after object falls out of cache', function(done) {
var start = cache.size();
cache.put(new_key(), 1, 2);
assert.equal(cache.size(), start + 1);
setTimeout(function() {
assert.equal(cache.size(), start);
done();
}, 4);
});
});

describe('clear()', function() {
it('should clear all objects', function() {
var size_start = cache.size();
var keys = [new_key(), new_key(), new_key()];
cache.put(keys[0], 1, 10);
cache.put(keys[1], 2, 10);
cache.put(keys[2], 3, 10);
assert.equal(cache.size(), size_start + 3);

cache.clear();
assert.equal(cache.size(), 0);
assert.isNull(cache.get(keys[0]));
assert.isNull(cache.get(keys[1]));
assert.isNull(cache.get(keys[2]));
});
});

describe('get()', function() {
it('should return null if key doesn\'t exist', function() {
assert.isNull(cache.get('awf1n1a'));
assert.isNull(cache.get(null));
assert.isNull(cache.get());
assert.isNull(cache.get(1));
assert.isNull(cache.get(true));
assert.isNull(cache.get(false));
});
it('should return value', function() {
var complicated = ['a',{'b':'c','d':['e',3]},'@'];
var key = new_key();
cache.put(key, complicated, 100);
assert.deepEqual(cache.get(key), complicated);
});
});

describe('put()', function() {
it('should overwrite existing object if exists', function(done) {
var ttl1 = 20;
var ttl2 = 20;
var value1 = {a:1};
var value2 = {a:2};
var key = new_key();

cache.put(key, value1, ttl1);
assert.deepEqual(cache.get(key), value1);
setTimeout(function() {
cache.put(key, value2, ttl2);
assert.deepEqual(cache.get(key), value2);
}, 10);

// test that the value isn't wiped out by the first
// put()'s timeout
setTimeout(function() {
assert.deepEqual(cache.get(key), value2);
done();
}, 25);
});
describe('stored object', function() {
it('should exist during ttl (ms)', function(done) {
var ttl = 20;
var value = {a:1};
var key = new_key();
cache.put(key, value, ttl);
setTimeout(function() {
assert.deepEqual(cache.get(key), value);
done();
}, 10);
});
it('should expire after ttl (ms)', function(done) {
var ttl = 20;
var value = {a:1};
var key = new_key();
cache.put(key, value, ttl)
setTimeout(function() {
assert.isNull(cache.get(key));
done();
}, 30);
});
});
});

describe('hits()', function() {
it('should return number of cache hits', function() {
var key = new_key();
cache.put(key, 1, 1);
var start = cache.hits();
cache.get('missing');
cache.get(key);
cache.get(key);
cache.get(key);
assert.equal(cache.hits(), start + 3);
});
});

describe('misses()', function() {
it('should return number of cache misses', function() {
var key = new_key();
cache.put(key, 1, 1);
var start = cache.misses();
cache.get('missing');
cache.get('missing');
cache.get(key);
assert.equal(cache.misses(), start + 2);
});
});
};

describe('tinycache (instance)', function() { suite(new TinyCache()); });
describe('tinycache (shared)', function() { suite(TinyCache.shared); });
18 changes: 8 additions & 10 deletions tinycache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var TinyCache = function() {
self.debug = false;
self.hitCount = 0;
self.missCount = 0;

return self;
}

Expand Down Expand Up @@ -43,14 +43,14 @@ TinyCache.prototype.put = function( key, value, time ) {
TinyCache.prototype.del = function(key) {
var self = this;
var record = self.cache[ key ];

if ( !record )
{
return false;
}

clearTimeout( record.timeout );

var isExpired = expired( record );
delete self.cache[ key ];
return !isExpired;
Expand All @@ -62,7 +62,7 @@ TinyCache.prototype.clear = function() {
for( var key in self.cache ) {
clearTimeout( self.cache[ key ].timeout );
}

self.cache = {};
}

Expand All @@ -73,15 +73,13 @@ TinyCache.prototype.get = function(key) {
{
if ( !expired( record ) )
{
self.debug && ++self.hitCount;
++self.hitCount;
return record.value;
}
else
{
self.debug && ++self.missCount;
} else {
self.del( key );
}
}
++self.missCount;
return null;
}

Expand Down
1 change: 0 additions & 1 deletion tinycache.min.js

This file was deleted.

0 comments on commit 1c70278

Please sign in to comment.