Skip to content

Commit 3bfbaaa

Browse files
mmunrwjblue
authored andcommitted
[BUGFIX beta] Add cache tests and fix undefined value
1 parent aac34ea commit 3bfbaaa

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/ember-metal/lib/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Cache.prototype = {
3737
value = this.set(key, this.func(key));
3838
} else if (value === UNDEFINED) {
3939
this.hits ++;
40-
value = UNDEFINED;
40+
value = undefined;
4141
} else {
4242
this.hits ++;
4343
// nothing to translate
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Cache from "ember-metal/cache";
2+
3+
QUnit.module("Cache");
4+
5+
test("basic", function() {
6+
var cache = new Cache(100, function(key) {
7+
return key.toUpperCase();
8+
});
9+
10+
equal(cache.get("foo"), "FOO");
11+
equal(cache.get("bar"), "BAR");
12+
equal(cache.get("foo"), "FOO");
13+
});
14+
15+
test("caches computation correctly", function() {
16+
var count = 0;
17+
var cache = new Cache(100, function(key) {
18+
count++;
19+
return key.toUpperCase();
20+
});
21+
22+
equal(count, 0);
23+
cache.get("foo");
24+
equal(count, 1);
25+
cache.get("bar");
26+
equal(count, 2);
27+
cache.get("bar");
28+
equal(count, 2);
29+
cache.get("foo");
30+
equal(count, 2);
31+
});
32+
33+
test("handles undefined value correctly", function() {
34+
var cache = new Cache(100, function(key) {});
35+
36+
equal(cache.get("foo"), undefined);
37+
});
38+
39+
test("continues working after reaching cache limit", function() {
40+
var cache = new Cache(3, function(key) {
41+
return key.toUpperCase();
42+
});
43+
44+
cache.get("a");
45+
cache.get("b");
46+
cache.get("c");
47+
48+
equal(cache.get("d"), "D");
49+
equal(cache.get("a"), "A");
50+
equal(cache.get("b"), "B");
51+
equal(cache.get("c"), "C");
52+
});

0 commit comments

Comments
 (0)