| 1 |
/*jslint nomen: true */ |
| 2 |
var Store = require('../../lib/store'), |
| 3 |
path = require('path'), |
| 4 |
fs = require('fs'), |
| 5 |
util = require('util'), |
| 6 |
foo = path.resolve(__dirname, 'data', 'foo.js'), |
| 7 |
baz = path.resolve(__dirname, 'data', 'baz.js'), |
| 8 |
anon = path.resolve(__dirname, 'data', 'does_not_exist.js'), |
| 9 |
fooContents = fs.readFileSync(foo, 'utf8'), |
| 10 |
bazContents = fs.readFileSync(baz, 'utf8'); |
| 11 |
|
| 12 |
module.exports = { |
| 13 |
"fslookup store should work correctly": function (test) { |
| 14 |
var store = Store.create('fslookup'); |
| 15 |
|
| 16 |
store.set(foo, 'content'); |
| 17 |
test.ok(store.hasKey(foo)); |
| 18 |
test.ok(store.hasKey(baz)); |
| 19 |
test.ok(!store.hasKey(anon)); |
| 20 |
test.equal(0, store.keys().length); |
| 21 |
|
| 22 |
test.equals(fooContents, store.get(foo)); |
| 23 |
test.equals(bazContents, store.get(baz)); |
| 24 |
test.throws(function () { |
| 25 |
store.get(anon); |
| 26 |
}); |
| 27 |
test.throws(function () { |
| 28 |
store.set(anon, 'foo'); |
| 29 |
}, Error, /non-existent/); |
| 30 |
test.done(); |
| 31 |
}, |
| 32 |
|
| 33 |
"tmp store should work as expected": function (test) { |
| 34 |
var store = Store.create('tmp'); |
| 35 |
|
| 36 |
test.equal(0, store.keys().length); |
| 37 |
store.set(foo, 'content'); |
| 38 |
test.ok(store.hasKey(foo)); |
| 39 |
test.ok(!store.hasKey(baz)); |
| 40 |
test.equal(1, store.keys().length); |
| 41 |
store.set(baz, 'baz content'); |
| 42 |
test.equal(2, store.keys().length); |
| 43 |
|
| 44 |
test.equals('content', store.get(foo)); |
| 45 |
test.equals('baz content', store.get(baz)); |
| 46 |
test.throws(function () { |
| 47 |
store.get(anon); |
| 48 |
}); |
| 49 |
store.dispose(); |
| 50 |
test.ok(!store.hasKey(foo)); |
| 51 |
test.equal(0, store.keys().length); |
| 52 |
test.done(); |
| 53 |
}, |
| 54 |
|
| 55 |
"memory store should work as expected": function (test) { |
| 56 |
var store = Store.create('memory'); |
| 57 |
|
| 58 |
test.equal(0, store.keys().length); |
| 59 |
store.set(foo, 'content'); |
| 60 |
test.ok(store.hasKey(foo)); |
| 61 |
test.ok(!store.hasKey(baz)); |
| 62 |
test.equal(1, store.keys().length); |
| 63 |
store.set(baz, 'baz content'); |
| 64 |
test.equal(2, store.keys().length); |
| 65 |
|
| 66 |
test.equals('content', store.get(foo)); |
| 67 |
test.equals('baz content', store.get(baz)); |
| 68 |
test.throws(function () { |
| 69 |
store.get(anon); |
| 70 |
}); |
| 71 |
store.dispose(); |
| 72 |
test.ok(!store.hasKey(foo)); |
| 73 |
test.equal(0, store.keys().length); |
| 74 |
test.done(); |
| 75 |
}, |
| 76 |
|
| 77 |
"should be able to register a new store": function (test) { |
| 78 |
function NStore() { |
| 79 |
} |
| 80 |
NStore.prototype = { |
| 81 |
set: function (/* file, content */) { return 'x'; } |
| 82 |
}; |
| 83 |
NStore.TYPE = 'nstore'; |
| 84 |
|
| 85 |
Store.register(NStore); |
| 86 |
var store = Store.create('nstore'); |
| 87 |
test.ok(store); |
| 88 |
test.equals('x', store.set('foo', 'bar')); |
| 89 |
test.done(); |
| 90 |
}, |
| 91 |
|
| 92 |
"should not be able to register an invalid store": function (test) { |
| 93 |
function NStore() {} |
| 94 |
test.throws(function () { |
| 95 |
Store.register(NStore); |
| 96 |
}, |
| 97 |
/TYPE/); |
| 98 |
test.done(); |
| 99 |
}, |
| 100 |
|
| 101 |
"invalid store should not be created": function (test) { |
| 102 |
test.throws( |
| 103 |
function () { Store.create('foo'); }, |
| 104 |
/Invalid store \[foo\], allowed values are / |
| 105 |
); |
| 106 |
test.done(); |
| 107 |
}, |
| 108 |
|
| 109 |
"should require overriding of all overrideables": function (test) { |
| 110 |
function NStore() {} |
| 111 |
NStore.TYPE = 'nstore'; |
| 112 |
util.inherits(NStore, Store); |
| 113 |
Store.register(NStore); |
| 114 |
|
| 115 |
var store = Store.create('nstore'), |
| 116 |
asserter = function (fn) { |
| 117 |
test.throws(function () { |
| 118 |
fn(); |
| 119 |
}, |
| 120 |
/must be overridden/); |
| 121 |
}; |
| 122 |
asserter(function () { store.set('foo', 'bar'); }); |
| 123 |
asserter(function () { store.get('foo'); }); |
| 124 |
asserter(function () { store.hasKey('foo'); }); |
| 125 |
asserter(function () { store.keys(); }); |
| 126 |
test.doesNotThrow(function () { store.dispose(); }); |
| 127 |
test.done(); |
| 128 |
} |
| 129 |
}; |