|
| 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