Permalink
Browse files

[tests]

  • Loading branch information...
3rd-Eden committed Sep 14, 2012
1 parent 2e7e51b commit cba75b17f1e02680d9342284be5492549882bec8
Showing with 117 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +14 −0 Makefile
  4. +1 −1 index.js
  5. +5 −1 package.json
  6. +93 −0 tests/expirable.test.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.8
View
@@ -0,0 +1,14 @@
+# configuration for the test suite
+ALL_TESTS = $(shell find tests/ -name '*.test.js')
+REPORTER = spec
+UI = exports
+
+test:
+ @./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+ --timeout 4000 \
+ --ui $(UI) \
+ --growl \
+ $(ALL_TESTS)
+
+.PHONY: test
View
@@ -75,7 +75,7 @@ Expire.prototype.set = function set(key, value, expires) {
Expire.prototype.has = function has(key) {
var now = Date.now();
- return key in this.cache && (now - this.cache[key].last) >= this.cache[key].expires;
+ return key in this.cache && (now - this.cache[key].last) <= this.cache[key].expires;
};
/**
View
@@ -24,5 +24,9 @@
"expiree"
],
"author": "Arnout Kazemier",
- "license": "MIT"
+ "license": "MIT",
+ "devDependencies": {
+ "mocha": "1.4.x",
+ "chai": "1.2.x"
+ }
}
View
@@ -0,0 +1,93 @@
+'use strict';
+
+// set up chai, our assertation library
+var chai = require('chai')
+ , expect = chai.expect;
+
+chai.Assertion.includeStack = true;
+
+describe('Expirable', function () {
+ var Expirable = require('../');
+
+ it('should just work without arguments', function () {
+ var cache = new Expirable();
+
+ cache.destroy();
+ });
+
+ it('should set the expiree as first argument', function () {
+ var cache = new Expirable('5 seconds');
+
+ cache.destroy();
+ });
+
+ it('should get and get the value', function () {
+ var cache = new Expirable('5 minutes');
+
+ cache.set('foo', 'bar');
+ expect(cache.get('foo')).to.equal('bar');
+
+ cache.destroy();
+ });
+
+ it('should have the key after setting', function () {
+ var cache = new Expirable('1 second');
+
+ cache.set('foo', 'bar');
+ expect(cache.has('foo')).to.equal(true);
+
+ cache.destroy();
+ });
+
+ it('should expire the cache', function (done) {
+ var cache = new Expirable('5ms');
+
+ cache.set('foo', 'bar');
+
+ setTimeout(function () {
+ expect(cache.has('foo')).to.equal(false);
+ expect(cache.get('foo')).to.equal(undefined);
+
+ cache.destroy();
+ done();
+ }, 10);
+ });
+
+ it('should remove keys', function () {
+ var cache = new Expirable('10000 ms');
+
+ cache.set('foo', 'bar');
+ cache.remove('foo');
+ expect(cache.get('foo')).to.equal(undefined);
+
+ cache.destroy();
+ });
+
+ it('should override default expire time during set', function (done) {
+ var cache = new Expirable('5 ms');
+
+ cache.set('foo', 'bar', '10 minutes');
+
+ setTimeout(function () {
+ expect(cache.get('foo')).to.equal('bar');
+
+ cache.destroy();
+ done();
+ }, 20);
+ });
+
+ it('should stop the scan, but still.. kill item when we get it', function (done) {
+ var cache = new Expirable('15 ms');
+
+ cache.set('foo', 'trololol');
+ cache.stop();
+
+ setTimeout(function () {
+ expect(cache.cache.foo.value).to.equal('trololol');
+ expect(cache.get('foo')).to.equal(undefined);
+
+ cache.destroy();
+ done();
+ }, 50);
+ });
+});

0 comments on commit cba75b1

Please sign in to comment.