-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.js
335 lines (277 loc) · 9.17 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*!
* cache-base <https://github.com/jonschlinkert/cache-base>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('cache-base', function() {
beforeEach(function() {
app = new Cache();
});
describe('constructor:', function() {
it('should create an instance of Cache', function() {
assert(app instanceof Cache);
});
it('should set values passed on the ctor', function() {
app = new Cache({ one: 1, two: 2 });
assert.equal(app.cache.one, 1);
assert.equal(app.cache.two, 2);
});
});
describe('prime', function() {
it('should prime a property on the cache with the given value', function() {
app.prime('life', 42);
assert.equal(app.cache.life, 42);
});
it('should not prime a property if it already exists', function() {
app.set('life', 51);
app.prime('life', 42);
assert.equal(app.cache.life, 51);
});
it('should prime an object', function() {
app.prime({ life: 42 });
assert.equal(app.cache.life, 42);
});
});
describe('default', function() {
it('should set a default value on cache.defaults', function() {
app.default('life', 42);
assert.equal(app.defaults.life, 42);
});
it('should not set a default value on the cache object', function() {
app.default('life', 42);
assert.equal(app.cache.life, undefined);
});
it('should be overridden when values are set', function() {
app.default('life', 42);
app.set('life', 51);
assert.equal(app.cache.life, 51);
});
it('should not set a default if it already exists', function() {
app.set('life', 51);
app.default('life', 42);
assert.equal(app.cache.life, 51);
});
it('should take an object', function() {
app.default({ life: 41 });
assert.equal(app.defaults.life, 41);
});
it('should take an array of objects', function() {
app.default([{ meaning: 41, life: 42 }]);
assert.equal(app.defaults.meaning, 41);
assert.equal(app.defaults.life, 42);
});
it('should return a value from cache.defaults when only the key is passed', function() {
app.default({ foo: 1, bar: 2 });
assert.equal(app.default('foo'), 1);
});
it('should return the default value with `.get()`', function() {
app.default({ foo: 1, bar: 2 });
app.set('foo', 42);
assert.equal(app.get('foo'), 42);
app.del('foo');
assert.equal(app.get('foo'), 1);
assert.equal(app.get('bar'), 2);
});
});
describe('set', function() {
it('should set a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
});
it('should set properties on `app.cache` when defined as key-value pairs', function() {
app.set('a', 'b');
assert.equal(app.cache.a, 'b');
});
it('should set properties on `app.cache` when defined as as an object', function() {
app.set({x: 'y'});
assert.equal(app.cache.x, 'y');
assert.equal(app.get('x'), 'y');
});
it('should set nested properties on the `app.cache`', function() {
app.set('c', {d: 'e'});
assert.equal(app.get('c.d'), 'e');
});
it('should be chainable', function() {
assert.equal(app.set('a', 'b'), app);
app
.set('aa', 'bb')
.set('bb', 'cc')
.set('cc', 'dd');
assert.equal(app.get('aa'), 'bb');
assert.equal(app.get('bb'), 'cc');
assert.equal(app.get('cc'), 'dd');
});
it('should return undefined when not set', function() {
assert.equal(app.set('a', undefined), app);
});
});
describe('get', function() {
it('should return undefined when no set', function() {
assert(app.get('a') === undefined);
});
it('should get a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
});
it('should get a nested property value', function() {
app.set('a.b.c', 'z');
assert.equal(app.cache.a.b.c, 'z');
assert.deepEqual(app.get('a.b'), {c: 'z'});
});
it('should support passing key as an array', function() {
app.set('a.b.c', 'z');
assert.equal(app.cache.a.b.c, 'z');
assert.deepEqual(app.get(['a', 'b']), {c: 'z'});
});
});
describe('union', function() {
it('should union a string value', function() {
app.union('a', 'b');
assert.deepEqual(app.get('a'), ['b']);
});
it('should union multiple string values', function() {
app.union('a', 'b');
app.union('a', 'c');
app.union('a', 'd');
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
});
it('should union multiple arrays', function() {
app.union('a', ['b']);
app.union('a', ['c']);
app.union('a', ['d']);
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
});
it('should union strings and arrays', function() {
app.union('a', 'a');
app.union('a', ['b']);
app.union('a', ['c', 'd']);
assert.deepEqual(app.get('a'), ['a', 'b', 'c', 'd']);
});
it('should union nested string values', function() {
app.union('a.b', 'b');
app.union('a.b', 'c');
app.union('a.b', 'd');
assert.deepEqual(app.get('a'), {b: ['b', 'c', 'd']});
});
it('should union and uniquify arrays', function() {
app.union('a.b', ['b', 'foo']);
app.union('a.b', ['c', 'foo']);
app.union('a.b', ['d', 'foo']);
assert.deepEqual(app.get('a'), {b: ['b', 'foo', 'c', 'd']});
});
});
describe('has', function() {
it('should return true if cache has a value for the given key', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(app.has('foo'));
assert(!app.has('bar'));
assert(app.has('baz'));
assert(!app.has('qux'));
});
it('should work with escaped keys', function() {
app.set('foo\\.baz', 'bar');
assert(!app.has('foo'));
assert(!app.has('bar'));
assert(app.has('foo.baz'));
});
it('should return true if a nested key value on the cache', function() {
app.set('a.b.c.d', { x: 'zzz' });
app.set('a.b.c.e', { f: null });
app.set('a.b.g.j', { k: undefined });
assert(app.has('a'));
assert(app.has('a.b'));
assert(app.has('a.b.c'));
assert(app.has('a.b.c.d'));
assert(app.has('a.b.c.d.x'));
assert(app.has('a.b.c.e.f'));
assert(!app.has('a.b.g.j.k'));
assert(!app.has('a.b.bar'));
assert(!app.has('a.b.c.d.z'));
assert(!app.has('a.b.c.e.bar'));
assert(!app.has('a.b.g.j.foo'));
});
});
describe('hasOwn', function() {
it('should return true if a cache has own key', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(app.hasOwn('foo'));
assert(!app.hasOwn('bar'));
assert(app.hasOwn('baz'));
assert(!app.hasOwn('qux'));
});
it('should work with escaped keys', function() {
app.set('foo\\.baz', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(!app.hasOwn('foo'));
assert(!app.hasOwn('bar'));
assert(app.hasOwn('foo.baz'));
assert(app.hasOwn('baz'));
assert(!app.hasOwn('qux'));
});
it('should return true if a nested key exists `.hasOwn()` on the cache', function() {
app.set('a.b.c.d', { x: 'zzz' });
app.set('a.b.c.e', { f: null });
app.set('a.b.g.j', { k: undefined });
assert(app.hasOwn('a'));
assert(app.hasOwn('a.b'));
assert(app.hasOwn('a.b.c'));
assert(app.hasOwn('a.b.c.d'));
assert(app.hasOwn('a.b.c.d.x'));
assert(app.hasOwn('a.b.c.e.f'));
assert(app.hasOwn('a.b.g.j.k'));
assert(app.hasOwn('a.b.g.j.k'));
assert(app.hasOwn('a.b.c.e.f'));
assert(!app.hasOwn('a.b.bar'));
assert(!app.hasOwn('a.b.c.d.z'));
assert(!app.hasOwn('a.b.c.e.bar'));
assert(!app.hasOwn('a.b.g.j.foo'));
});
});
describe('del', function() {
it('should delete a property from the cache', function() {
app.set('foo', 42);
app.set('bar', 43);
assert.equal(app.get('foo'), 42);
assert.equal(app.get('bar'), 43);
app.del('foo');
assert.equal(app.get('foo'), undefined);
assert.equal(app.get('bar'), 43);
});
it('should delete all property from the cache when no key is passed', function() {
app.set('foo', 42);
app.set('bar', 43);
assert.equal(app.get('foo'), 42);
assert.equal(app.get('bar'), 43);
app.del();
assert.equal(app.get('foo'), undefined);
assert.equal(app.get('bar'), undefined);
});
});
describe('keys', function() {
it('should return all enumerable property names from the cache', function() {
app.set('foo', 42);
app.set('bar', null);
app.set('baz', undefined);
assert.deepEqual(app.keys, ['foo', 'bar']);
});
});
describe('size', function() {
it('should return the length of cache.keys', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert.equal(app.size, 2);
});
});
});