-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.mjs
105 lines (82 loc) · 3.18 KB
/
usage.mjs
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
// -*- coding: utf-8, tab-width: 2 -*-
import test from 'p-tape';
import updateCache from '../cache.js';
function makeGenCounter() {
function gen() {
gen.counter += 1;
return { 'gen#': gen.counter };
};
gen.counter = 0;
return gen;
}
test('Basic example', async (t) => {
const cache = {};
t.plan(33);
(function animalSounds() {
const topic = 'animal sounds';
const gen = makeGenCounter();
const addCat = { cat: 'meow' };
const learnedCat = updateCache(cache, topic, addCat, gen);
t.equal(gen.counter, 1, 'cat gen ran');
t.same(learnedCat, { 'gen#': 1, cat: 'meow' }, 'learned cat sound');
t.notEqual(learnedCat, addCat);
const uncachedCat = updateCache(null, topic, addCat);
t.equal(uncachedCat, addCat);
const uncachedGenCat = updateCache(null, topic, addCat, gen);
t.equal(gen.counter, 2, 'cat gen ran');
t.notEqual(uncachedGenCat, addCat);
t.same(uncachedGenCat, { 'gen#': 2, cat: 'meow' });
const againCat = updateCache(cache, topic, addCat, gen);
t.equal(againCat, learnedCat);
t.equal(gen.counter, 2, 'cat gen skipped');
const addDog = { dog: 'woof' };
const learnedDog = updateCache(cache, topic, addDog, gen);
t.equal(gen.counter, 2, 'dog gen skipped');
t.same(learnedDog, { 'gen#': 1, cat: 'meow', dog: 'woof' },
'learned dog sound');
t.notEqual(learnedDog, addCat);
t.notEqual(learnedDog, addDog);
const uncachedDog = updateCache(null, topic, addDog);
t.equal(uncachedDog, addDog);
const addCow = { cow: 'moo' };
const learnedCow = updateCache(cache, topic, addCow);
t.same(learnedCow, { 'gen#': 1, cat: 'meow', dog: 'woof', cow: 'moo' },
'learned cow sound');
t.notEqual(learnedCow, addCat);
t.notEqual(learnedCow, addDog);
t.notEqual(learnedCow, addCow);
t.notEqual(learnedCow, gen);
const uncachedCow = updateCache(null, topic, addCow);
t.equal(uncachedCow, addCow);
}());
(function flowerColors() {
const topic = 'flower colors';
const addRose = { rose: 'red' };
const learnedRose = updateCache(cache, topic, addRose);
t.same(learnedRose, { rose: 'red' });
t.notEqual(learnedRose, addRose);
const uncachedRose = updateCache(null, topic, addRose);
t.equal(uncachedRose, addRose);
const addViolet = { violet: 'blue' };
const learnedViolet = updateCache(cache, topic, addViolet);
t.same(learnedViolet, { rose: 'red', violet: 'blue' });
t.notEqual(learnedViolet, addRose);
t.notEqual(learnedViolet, addViolet);
const uncachedViolet = updateCache(null, topic, addViolet);
t.equal(uncachedViolet, addViolet);
const addLily = { lily: 'white' };
const learnedLily = updateCache(cache, topic, addLily);
t.same(learnedLily, { rose: 'red', violet: 'blue', lily: 'white' });
t.notEqual(learnedLily, addRose);
t.notEqual(learnedLily, addViolet);
t.notEqual(learnedLily, addLily);
const uncachedLily = updateCache(null, topic, addLily);
t.equal(uncachedLily, addLily);
}());
t.same(cache, {
'animal sounds': { 'gen#': 1, cat: 'meow', dog: 'woof', cow: 'moo' },
'flower colors': { rose: 'red', violet: 'blue', lily: 'white' },
});
t.end();
});
/* scroll */