github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/cache_spec.js (about) 1 var Cache = require('src/cache') 2 3 /** 4 * Debug function to assert cache state 5 * 6 * @param {Cache} cache 7 */ 8 9 function toString (cache) { 10 var s = '' 11 var entry = cache.head 12 while (entry) { 13 s += String(entry.key) + ':' + entry.value 14 entry = entry.newer 15 if (entry) { 16 s += ' < ' 17 } 18 } 19 return s 20 } 21 22 describe('Cache', function () { 23 var c = new Cache(4) 24 25 it('put', function () { 26 c.put('adam', 29) 27 c.put('john', 26) 28 c.put('angela', 24) 29 c.put('bob', 48) 30 expect(c.size).toBe(4) 31 expect(toString(c)).toBe('adam:29 < john:26 < angela:24 < bob:48') 32 }) 33 34 it('put with same key', function () { 35 var same = new Cache(4) 36 same.put('john', 29) 37 same.put('john', 26) 38 same.put('john', 24) 39 same.put('john', 48) 40 expect(same.size).toBe(1) 41 expect(toString(same)).toBe('john:48') 42 }) 43 44 it('get', function () { 45 expect(c.get('adam')).toBe(29) 46 expect(c.get('john')).toBe(26) 47 expect(c.get('angela')).toBe(24) 48 expect(c.get('bob')).toBe(48) 49 expect(toString(c)).toBe('adam:29 < john:26 < angela:24 < bob:48') 50 51 expect(c.get('angela')).toBe(24) 52 // angela should now be the tail 53 expect(toString(c)).toBe('adam:29 < john:26 < bob:48 < angela:24') 54 }) 55 56 it('expire', function () { 57 c.put('ygwie', 81) 58 expect(c.size).toBe(4) 59 expect(toString(c)).toBe('john:26 < bob:48 < angela:24 < ygwie:81') 60 expect(c.get('adam')).toBeUndefined() 61 }) 62 63 it('shift', function () { 64 var shift = new Cache(4) 65 shift.put('adam', 29) 66 shift.put('john', 26) 67 shift.put('angela', 24) 68 shift.put('bob', 48) 69 70 shift.shift() 71 shift.shift() 72 shift.shift() 73 expect(shift.size).toBe(1) 74 expect(toString(shift)).toBe('bob:48') 75 }) 76 })