cache-manager 3.4.1 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/History.md +12 -0
- package/README.md +1 -3
- package/lib/multi_caching.js +5 -1
- package/lib/stores/memory.js +34 -0
- package/package.json +7 -2
- package/.coveralls.yml +0 -1
- package/.eslintrc +0 -61
- package/.nycrc.json +0 -20
- package/.travis.yml +0 -10
- package/Makefile +0 -32
- package/test/caching.unit.js +0 -1587
- package/test/mocha.opts +0 -5
- package/test/multi_caching.unit.js +0 -2195
- package/test/run.js +0 -73
- package/test/stores/memory.unit.js +0 -276
- package/test/stores/none.unit.js +0 -148
- package/test/support.js +0 -115
- package/test/utils.unit.js +0 -24
package/test/run.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
process.env.NODE_ENV = 'test';
|
|
3
|
-
require('../index');
|
|
4
|
-
|
|
5
|
-
var Mocha = require('mocha');
|
|
6
|
-
var optimist = require('optimist');
|
|
7
|
-
var walkDir = require('./support').walkDir;
|
|
8
|
-
|
|
9
|
-
var argv = optimist
|
|
10
|
-
.usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default'](
|
|
11
|
-
{types: 'unit,functional', reporter: 'spec', timeout: 6000})
|
|
12
|
-
.describe('types', 'The types of tests to run, separated by commas. E.g., unit,functional,acceptance')
|
|
13
|
-
.describe('reporter', 'The mocha test reporter to use.')
|
|
14
|
-
.describe('timeout', 'The mocha timeout to use per test (ms).')
|
|
15
|
-
.boolean('help')
|
|
16
|
-
.alias('types', 'T')
|
|
17
|
-
.alias('timeout', 't')
|
|
18
|
-
.alias('reporter', 'R')
|
|
19
|
-
.alias('help', 'h')
|
|
20
|
-
.argv;
|
|
21
|
-
|
|
22
|
-
var mocha = new Mocha({timeout: argv.timeout, reporter: argv.reporter, ui: 'bdd'});
|
|
23
|
-
|
|
24
|
-
var validTestTypes = ['unit', 'functional', 'acceptance', 'integration'];
|
|
25
|
-
var requestedTypes = argv.types.split(',');
|
|
26
|
-
var typesToUse = [];
|
|
27
|
-
|
|
28
|
-
validTestTypes.forEach(function(validTestType) {
|
|
29
|
-
if (requestedTypes.indexOf(validTestType) !== -1) {
|
|
30
|
-
typesToUse.push(validTestType);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (argv.help || typesToUse.length === 0) {
|
|
35
|
-
console.log('\n' + optimist.help());
|
|
36
|
-
process.exit();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
var isValidFile = function(file) {
|
|
40
|
-
if (file.match(/buster/)) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
for (var i = 0; i < typesToUse.length; i++) {
|
|
45
|
-
var testType = typesToUse[i];
|
|
46
|
-
var ext = testType + ".js";
|
|
47
|
-
|
|
48
|
-
if (file.indexOf(ext) !== -1) {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return false;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
function run(cb) {
|
|
57
|
-
walkDir('test', isValidFile, function(err, files) {
|
|
58
|
-
if (err) { return cb(err); }
|
|
59
|
-
|
|
60
|
-
files.forEach(function(file) {
|
|
61
|
-
mocha.addFile(file);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
cb();
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
run(function(err) {
|
|
69
|
-
if (err) { throw err; }
|
|
70
|
-
mocha.run(function(failures) {
|
|
71
|
-
process.exit(failures);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
var assert = require('assert');
|
|
2
|
-
var support = require('../support');
|
|
3
|
-
var checkErr = support.checkErr;
|
|
4
|
-
var caching = require('../../index').caching;
|
|
5
|
-
var memoryStore = require('../../lib/stores/memory');
|
|
6
|
-
|
|
7
|
-
describe("memory store", function() {
|
|
8
|
-
describe("instantiating", function() {
|
|
9
|
-
it("lets us pass in no args", function(done) {
|
|
10
|
-
var memoryCache = memoryStore.create();
|
|
11
|
-
support.testSetGetDel(memoryCache, done);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe("set()", function() {
|
|
16
|
-
var memoryCache;
|
|
17
|
-
var origPromise;
|
|
18
|
-
|
|
19
|
-
beforeEach(function() {
|
|
20
|
-
origPromise = global.Promise;
|
|
21
|
-
delete global.Promise;
|
|
22
|
-
memoryCache = memoryStore.create({noPromises: true});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(function() {
|
|
26
|
-
global.Promise = origPromise;
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// This test should pass in node v0.10.x:
|
|
30
|
-
it("does not require a callback or use of Promises", function(done) {
|
|
31
|
-
memoryCache.set('foo', 'bar');
|
|
32
|
-
|
|
33
|
-
setTimeout(function() {
|
|
34
|
-
assert.equal(memoryCache.get('foo'), 'bar');
|
|
35
|
-
done();
|
|
36
|
-
}, 10);
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe("when used with wrap() function", function() {
|
|
41
|
-
var cache;
|
|
42
|
-
var key;
|
|
43
|
-
var opts = {};
|
|
44
|
-
|
|
45
|
-
beforeEach(function() {
|
|
46
|
-
key = support.random.string(20);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
context("when cache misses", function() {
|
|
50
|
-
function getCachedObject(name, cb) {
|
|
51
|
-
cache.wrap(key, function(cacheCb) {
|
|
52
|
-
cacheCb(null, {foo: 'bar', arr: [1, 2, 3]});
|
|
53
|
-
}, opts, cb);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function getCachedString(name, cb) {
|
|
57
|
-
cache.wrap(key, function(cacheCb) {
|
|
58
|
-
cacheCb(null, 'bar');
|
|
59
|
-
}, opts, cb);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function getCachedArray(name, cb) {
|
|
63
|
-
cache.wrap(key, function(cacheCb) {
|
|
64
|
-
cacheCb(null, [1, 2, 3]);
|
|
65
|
-
}, opts, cb);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function getCachedNumber(name, cb) {
|
|
69
|
-
cache.wrap(key, function(cacheCb) {
|
|
70
|
-
cacheCb(null, 34);
|
|
71
|
-
}, opts, cb);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function getCachedFunction(name, cb) {
|
|
75
|
-
cache.wrap(key, function(cacheCb) {
|
|
76
|
-
cacheCb(null, function() {
|
|
77
|
-
return 'foo';
|
|
78
|
-
});
|
|
79
|
-
}, opts, cb);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getCachedObjectWithPrototype(name, cb) {
|
|
83
|
-
function Thing() {}
|
|
84
|
-
Thing.prototype.f = function() {
|
|
85
|
-
return 'foo';
|
|
86
|
-
};
|
|
87
|
-
cache.wrap(key, function(cacheCb) {
|
|
88
|
-
cacheCb(null, new Thing());
|
|
89
|
-
}, opts, cb);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function assertCachedObjectWithPrototype(result) {
|
|
93
|
-
assert.equal(typeof result, 'object');
|
|
94
|
-
var prototype = Object.getPrototypeOf(result);
|
|
95
|
-
assert.equal(typeof prototype.f, 'function', 'prototype does not have function f');
|
|
96
|
-
assert.equal(result.f(), 'foo', 'prototype function f does not return expected value');
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// By default, memory store clones values before setting in the set method.
|
|
100
|
-
context("when shouldCloneBeforeSet option is not passed in", () => {
|
|
101
|
-
beforeEach(function() {
|
|
102
|
-
cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("does not allow mutation of objects", function(done) {
|
|
106
|
-
getCachedObject('foo', function(err, result) {
|
|
107
|
-
checkErr(err);
|
|
108
|
-
result.foo = 'buzz';
|
|
109
|
-
|
|
110
|
-
getCachedObject('foo', function(err, result) {
|
|
111
|
-
checkErr(err);
|
|
112
|
-
assert.equal(result.foo, 'bar');
|
|
113
|
-
assert.deepEqual(result.arr, [1, 2, 3]);
|
|
114
|
-
done();
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("does not allow mutation of arrays", function(done) {
|
|
120
|
-
getCachedArray('foo', function(err, result) {
|
|
121
|
-
checkErr(err);
|
|
122
|
-
assert.ok(result);
|
|
123
|
-
result = ['a', 'b', 'c'];
|
|
124
|
-
|
|
125
|
-
getCachedArray('foo', function(err, result) {
|
|
126
|
-
checkErr(err);
|
|
127
|
-
assert.deepEqual(result, [1, 2, 3]);
|
|
128
|
-
done();
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("does not allow mutation of strings", function(done) {
|
|
134
|
-
getCachedString('foo', function(err, result) {
|
|
135
|
-
checkErr(err);
|
|
136
|
-
assert.ok(result);
|
|
137
|
-
result = 'buzz';
|
|
138
|
-
|
|
139
|
-
getCachedString('foo', function(err, result) {
|
|
140
|
-
checkErr(err);
|
|
141
|
-
assert.equal(result, 'bar');
|
|
142
|
-
done();
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("does not allow mutation of numbers", function(done) {
|
|
148
|
-
getCachedNumber('foo', function(err, result) {
|
|
149
|
-
checkErr(err);
|
|
150
|
-
assert.ok(result);
|
|
151
|
-
result = 12;
|
|
152
|
-
|
|
153
|
-
getCachedNumber('foo', function(err, result) {
|
|
154
|
-
checkErr(err);
|
|
155
|
-
assert.equal(result, 34);
|
|
156
|
-
done();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it("preserves functions", function(done) {
|
|
162
|
-
getCachedFunction('foo', function(err, result) {
|
|
163
|
-
checkErr(err);
|
|
164
|
-
assert.equal(typeof result, 'function');
|
|
165
|
-
|
|
166
|
-
getCachedFunction('foo', function(err, result) {
|
|
167
|
-
checkErr(err);
|
|
168
|
-
assert.equal(typeof result, 'function');
|
|
169
|
-
done();
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("preserves object prototype", function(done) {
|
|
175
|
-
getCachedObjectWithPrototype('foo', function(err, result) {
|
|
176
|
-
checkErr(err);
|
|
177
|
-
assertCachedObjectWithPrototype(result);
|
|
178
|
-
|
|
179
|
-
getCachedObjectWithPrototype('foo', function(err, result) {
|
|
180
|
-
checkErr(err);
|
|
181
|
-
assertCachedObjectWithPrototype(result);
|
|
182
|
-
done();
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
context("when shouldCloneBeforeSet=false option is passed in", () => {
|
|
189
|
-
beforeEach(function() {
|
|
190
|
-
cache = caching({store: 'memory', ttl: opts.ttl, shouldCloneBeforeSet: false, ignoreCacheErrors: false});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("*does* allow mutation of objects", function(done) {
|
|
194
|
-
getCachedObject('foo', function(err, result) {
|
|
195
|
-
checkErr(err);
|
|
196
|
-
result.foo = 'buzz';
|
|
197
|
-
|
|
198
|
-
getCachedObject('foo', function(err, result) {
|
|
199
|
-
checkErr(err);
|
|
200
|
-
assert.equal(result.foo, 'buzz');
|
|
201
|
-
done();
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it("does not allow mutation of arrays", function(done) {
|
|
207
|
-
getCachedArray('foo', function(err, result) {
|
|
208
|
-
checkErr(err);
|
|
209
|
-
assert.ok(result);
|
|
210
|
-
result = ['a', 'b', 'c'];
|
|
211
|
-
|
|
212
|
-
getCachedArray('foo', function(err, result) {
|
|
213
|
-
checkErr(err);
|
|
214
|
-
assert.deepEqual(result, [1, 2, 3]);
|
|
215
|
-
done();
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it("does not allow mutation of strings", function(done) {
|
|
221
|
-
getCachedString('foo', function(err, result) {
|
|
222
|
-
checkErr(err);
|
|
223
|
-
assert.ok(result);
|
|
224
|
-
result = 'buzz';
|
|
225
|
-
|
|
226
|
-
getCachedString('foo', function(err, result) {
|
|
227
|
-
checkErr(err);
|
|
228
|
-
assert.equal(result, 'bar');
|
|
229
|
-
done();
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it("does not allow mutation of numbers", function(done) {
|
|
235
|
-
getCachedNumber('foo', function(err, result) {
|
|
236
|
-
checkErr(err);
|
|
237
|
-
assert.ok(result);
|
|
238
|
-
result = 12;
|
|
239
|
-
|
|
240
|
-
getCachedNumber('foo', function(err, result) {
|
|
241
|
-
checkErr(err);
|
|
242
|
-
assert.equal(result, 34);
|
|
243
|
-
done();
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it("preserves functions", function(done) {
|
|
249
|
-
getCachedFunction('foo', function(err, result) {
|
|
250
|
-
checkErr(err);
|
|
251
|
-
assert.equal(typeof result, 'function');
|
|
252
|
-
|
|
253
|
-
getCachedFunction('foo', function(err, result) {
|
|
254
|
-
checkErr(err);
|
|
255
|
-
assert.equal(typeof result, 'function');
|
|
256
|
-
done();
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("preserves object prototype", function(done) {
|
|
262
|
-
getCachedObjectWithPrototype('foo', function(err, result) {
|
|
263
|
-
checkErr(err);
|
|
264
|
-
assertCachedObjectWithPrototype(result);
|
|
265
|
-
|
|
266
|
-
getCachedObjectWithPrototype('foo', function(err, result) {
|
|
267
|
-
checkErr(err);
|
|
268
|
-
assertCachedObjectWithPrototype(result);
|
|
269
|
-
done();
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
});
|
package/test/stores/none.unit.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
var assert = require('assert');
|
|
2
|
-
// var support = require('../support');
|
|
3
|
-
var noneStore = require('../../lib/stores/none');
|
|
4
|
-
|
|
5
|
-
describe('none store', function() {
|
|
6
|
-
var key = 'test-key';
|
|
7
|
-
var value = 'test-value';
|
|
8
|
-
var key2 = 'test-key-2';
|
|
9
|
-
var value2 = 'test-value-2';
|
|
10
|
-
|
|
11
|
-
describe('with callback', function() {
|
|
12
|
-
var origPromise;
|
|
13
|
-
var noneCache;
|
|
14
|
-
|
|
15
|
-
beforeEach(function() {
|
|
16
|
-
origPromise = global.Promise;
|
|
17
|
-
delete global.Promise;
|
|
18
|
-
noneCache = noneStore.create({noPromises: true});
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
afterEach(function() {
|
|
22
|
-
global.Promise = origPromise;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('set()', function(done) {
|
|
26
|
-
assert.strictEqual(noneCache.set(key, value), undefined);
|
|
27
|
-
noneCache.set(key, value, function(err) {
|
|
28
|
-
assert.strictEqual(err, null);
|
|
29
|
-
noneCache.set(key, value, {}, done);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('mset()', function(done) {
|
|
34
|
-
assert.strictEqual(noneCache.mset(key, value), undefined);
|
|
35
|
-
noneCache.mset(key, value, key2, value2, function(err) {
|
|
36
|
-
assert.strictEqual(err, null);
|
|
37
|
-
noneCache.mset(key, value, {}, done);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('get()', function(done) {
|
|
42
|
-
assert.strictEqual(noneCache.get(key), undefined);
|
|
43
|
-
noneCache.get(key2, function(err, result) {
|
|
44
|
-
assert.strictEqual(err, null);
|
|
45
|
-
assert.strictEqual(result, undefined);
|
|
46
|
-
noneCache.get(key2, {}, done);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('mget()', function(done) {
|
|
51
|
-
assert.deepEqual(noneCache.mget(key, key2), [undefined, undefined]);
|
|
52
|
-
noneCache.mget(key, key2, function(err, result) {
|
|
53
|
-
assert.strictEqual(err, null);
|
|
54
|
-
assert.deepEqual(result, [undefined, undefined]);
|
|
55
|
-
noneCache.mget(key, key2, key, {}, function(err, result) {
|
|
56
|
-
assert.strictEqual(err, null);
|
|
57
|
-
assert.deepEqual(result, [undefined, undefined, undefined]);
|
|
58
|
-
done();
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('del()', function(done) {
|
|
64
|
-
assert.strictEqual(noneCache.del(), undefined);
|
|
65
|
-
noneCache.del(function() {
|
|
66
|
-
noneCache.del({}, done);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('reset()', function(done) {
|
|
71
|
-
assert.strictEqual(noneCache.reset(), undefined);
|
|
72
|
-
noneCache.reset(done);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('keys()', function(done) {
|
|
76
|
-
assert.deepEqual(noneCache.keys(), []);
|
|
77
|
-
noneCache.keys(function(err, keys) {
|
|
78
|
-
assert.deepEqual(keys, []);
|
|
79
|
-
done();
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('with promise', function() {
|
|
85
|
-
var noneCache;
|
|
86
|
-
|
|
87
|
-
beforeEach(function() {
|
|
88
|
-
noneCache = noneStore.create({promiseDependency: global.Promise});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('get() global promise', function(done) {
|
|
92
|
-
noneStore.create()
|
|
93
|
-
.get(key)
|
|
94
|
-
.then(function(result) {
|
|
95
|
-
assert.strictEqual(result, undefined);
|
|
96
|
-
done();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('set()', function(done) {
|
|
101
|
-
noneCache.set(key, value)
|
|
102
|
-
.then(function(result) {
|
|
103
|
-
assert.equal(result, value);
|
|
104
|
-
done();
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('mset()', function(done) {
|
|
109
|
-
noneCache.mset(key, value, key2, value2)
|
|
110
|
-
.then(function(result) {
|
|
111
|
-
assert.deepEqual(result, [value, value2]);
|
|
112
|
-
done();
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('get()', function(done) {
|
|
117
|
-
noneCache.get(key)
|
|
118
|
-
.then(function(result) {
|
|
119
|
-
assert.strictEqual(result, undefined);
|
|
120
|
-
done();
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('mget()', function(done) {
|
|
125
|
-
noneCache.mget(key, key2)
|
|
126
|
-
.then(function(result) {
|
|
127
|
-
assert.deepEqual(result, [undefined, undefined]);
|
|
128
|
-
done();
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('del()', function(done) {
|
|
133
|
-
noneCache.del(key).then(done);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('reset()', function(done) {
|
|
137
|
-
noneCache.reset().then(done);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('keys()', function(done) {
|
|
141
|
-
noneCache.keys()
|
|
142
|
-
.then(function(result) {
|
|
143
|
-
assert.deepEqual(result, []);
|
|
144
|
-
done();
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
});
|
package/test/support.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
var fs = require('fs');
|
|
2
|
-
var util = require('util');
|
|
3
|
-
var assert = require('assert');
|
|
4
|
-
|
|
5
|
-
var support = {
|
|
6
|
-
random: {
|
|
7
|
-
string: function(strLen) {
|
|
8
|
-
strLen = strLen || 8;
|
|
9
|
-
var chars = "abcdefghiklmnopqrstuvwxyz";
|
|
10
|
-
var randomStr = '';
|
|
11
|
-
for (var i = 0; i < strLen; i++) {
|
|
12
|
-
var rnum = Math.floor(Math.random() * chars.length);
|
|
13
|
-
randomStr += chars.substring(rnum, rnum + 1);
|
|
14
|
-
}
|
|
15
|
-
return randomStr;
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
number: function(max) {
|
|
19
|
-
max = max || 1000;
|
|
20
|
-
return Math.floor((Math.random() * max));
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
checkErr: function(err) {
|
|
25
|
-
if (err) {
|
|
26
|
-
var msg;
|
|
27
|
-
|
|
28
|
-
if (err instanceof Error) {
|
|
29
|
-
msg = err;
|
|
30
|
-
} else if (err.msg) {
|
|
31
|
-
msg = err.msg;
|
|
32
|
-
} else {
|
|
33
|
-
msg = util.inspect(err);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var error = new Error(msg);
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
assertBetween: function(actual, lower, upper) {
|
|
42
|
-
assert.ok(actual >= lower, "Expected " + actual + " to be >= " + lower);
|
|
43
|
-
assert.ok(actual <= upper, "Expected " + actual + " to be <= " + upper);
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
assertWithin: function(actual, expected, delta) {
|
|
47
|
-
var lower = expected - delta;
|
|
48
|
-
var upper = expected + delta;
|
|
49
|
-
this.assertBetween(actual, lower, upper);
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
walkDir: function(dir, validationFunction, cb) {
|
|
53
|
-
if (arguments.length === 2) {
|
|
54
|
-
cb = validationFunction;
|
|
55
|
-
validationFunction = null;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
var results = [];
|
|
59
|
-
fs.readdir(dir, function(err, list) {
|
|
60
|
-
if (err) { return cb(err); }
|
|
61
|
-
|
|
62
|
-
var pending = list.length;
|
|
63
|
-
|
|
64
|
-
if (!pending) { return cb(null, results); }
|
|
65
|
-
|
|
66
|
-
list.forEach(function(file) {
|
|
67
|
-
file = dir + '/' + file;
|
|
68
|
-
fs.stat(file, function(err, stat) {
|
|
69
|
-
if (stat && stat.isDirectory()) {
|
|
70
|
-
support.walkDir(file, validationFunction, function(err, res) {
|
|
71
|
-
results = results.concat(res);
|
|
72
|
-
if (!--pending) { cb(null, results); }
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
if (typeof validationFunction === 'function') {
|
|
76
|
-
if (validationFunction(file)) {
|
|
77
|
-
results.push(file);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
results.push(file);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!--pending) { cb(null, results); }
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
testSetGetDel: function(cache, cb) {
|
|
91
|
-
var key = 'TEST' + support.random.string();
|
|
92
|
-
var val = support.random.string();
|
|
93
|
-
|
|
94
|
-
cache.set(key, val, function(err) {
|
|
95
|
-
if (err) { return cb(err); }
|
|
96
|
-
|
|
97
|
-
cache.get(key, function(err, result) {
|
|
98
|
-
if (err) { return cb(err); }
|
|
99
|
-
assert.equal(result, val);
|
|
100
|
-
|
|
101
|
-
cache.del(key, function(err) {
|
|
102
|
-
if (err) { return cb(err); }
|
|
103
|
-
|
|
104
|
-
cache.get(key, function(err, result) {
|
|
105
|
-
if (err) { return cb(err); }
|
|
106
|
-
assert.ok(!result);
|
|
107
|
-
cb();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
module.exports = support;
|
package/test/utils.unit.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
var utils = require('../lib/utils');
|
|
2
|
-
var assert = require('assert');
|
|
3
|
-
|
|
4
|
-
var isObject = utils.isObject;
|
|
5
|
-
|
|
6
|
-
describe('utils', function() {
|
|
7
|
-
describe('isObject()', function() {
|
|
8
|
-
it('should return "true" when value passed is an object', function() {
|
|
9
|
-
var result1 = isObject({});
|
|
10
|
-
assert.ok(result1);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should return "false" when value passed is not an object', function() {
|
|
14
|
-
var result1 = isObject('string');
|
|
15
|
-
var result2 = isObject(123);
|
|
16
|
-
var result3 = isObject([]);
|
|
17
|
-
var result4 = isObject(function() {});
|
|
18
|
-
assert.ok(!result1);
|
|
19
|
-
assert.ok(!result2);
|
|
20
|
-
assert.ok(!result3);
|
|
21
|
-
assert.ok(!result4);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
});
|