1 var JS
= this.JS
|| require('jstest');
3 var ystorage
= require('yuu/storage');
5 JS
.Test
.describe('ystorage', function () { with (this) {
6 JS
.Test
.describe("PrefixedStorage", function () { with (this) {
8 this.backend
= new ystorage
.FakeStorage();
9 this.a
= new ystorage
.PrefixedStorage(this.backend
, "a");
10 this.b
= new ystorage
.PrefixedStorage(this.backend
, "b");
11 this.c1
= new ystorage
.PrefixedStorage(this.backend
, "c");
12 this.c2
= new ystorage
.PrefixedStorage(this.backend
, "c");
15 it('starts empty', function () { with (this) {
16 assertEqual(0, this.backend
.length
);
17 assertEqual(0, this.a
.length
);
18 assertEqual(0, this.b
.length
);
19 assertEqual(0, this.c1
.length
);
20 assertEqual(0, this.c2
.length
);
21 assertEqual(null, this.backend
.key(0));
22 assertEqual(null, this.a
.key(0));
23 assertEqual(null, this.b
.key(0));
24 assertEqual(null, this.c1
.key(0));
25 assertEqual(null, this.c2
.key(0));
28 it('stores and retrieves a value', function () { with (this) {
29 this.a
.setItem("key", "value");
30 assertEqual(1, this.backend
.length
);
31 assertEqual(1, this.a
.length
);
32 assertEqual(0, this.b
.length
);
33 assertEqual(0, this.c1
.length
);
34 assertEqual(0, this.c2
.length
);
35 assertEqual("a -- key", this.backend
.key(0));
36 assertEqual("key", this.a
.key(0));
37 assertEqual(null, this.b
.key(0));
38 assertEqual(null, this.c1
.key(0));
39 assertEqual(null, this.c2
.key(0));
42 it('avoids other values when clearing', function () { with (this) {
43 this.a
.setItem("key", "value a");
44 this.b
.setItem("key", "value b");
45 assertEqual(2, this.backend
.length
);
46 assertEqual(1, this.a
.length
);
47 assertEqual(1, this.b
.length
);
49 assertEqual(1, this.backend
.length
);
50 assertEqual(0, this.a
.length
);
51 assertEqual(1, this.b
.length
);
52 assertEqual(null, this.a
.key(0));
53 assertEqual(null, this.a
.getItem("key"));
54 assertEqual("key", this.b
.key(0));
55 assertEqual("value b", this.b
.getItem("key"));
58 it('shares with the same prefix', function () { with (this) {
59 this.c1
.setItem("key", "value");
60 assertEqual("key", this.c1
.key(0));
61 assertEqual("key", this.c2
.key(0));
62 assertEqual("value", this.c1
.getItem("key"));
63 assertEqual("value", this.c2
.getItem("key"));
68 JS
.Test
.describe("PrefixedStorage", function () { with (this) {
69 it('stores objects', function () { with (this) {
70 var storage
= new ystorage
.Storage();
71 assertEqual(undefined, storage
.getObject("key"));
72 storage
.setObject("key", [1, 2, 3]);
73 assertEqual([1, 2, 3], storage
.getObject("key"));
75 it('handles defaults', function () { with (this) {
76 var storage
= new ystorage
.Storage(null, {
77 'should be null': null,
78 'should be undefined': undefined,
79 'should be false': false
81 assertEqual(null, storage
.getObject('should be null'));
82 assertEqual(false, storage
.getObject('should be false'));
83 assertEqual(undefined, storage
.getObject('should be undefined'));
84 assertEqual(undefined, storage
.getObject('should not exist'));
86 it('handles fallbacks', function () { with (this) {
87 var storage
= new ystorage
.Storage();
88 assertEqual(undefined, storage
.getObject('?', undefined));
89 assertEqual(null, storage
.getObject('?', null));
90 assertEqual(false, storage
.getObject('?', false));
92 it('prefers defaults to fallbacks', function () { with (this) {
93 var storage
= new ystorage
.Storage(null, {
94 'should be null': null,
95 'should be undefined': undefined,
96 'should be false': false
98 assertEqual(null, storage
.getObject('should be null', 0));
99 assertEqual(false, storage
.getObject('should be false', 0));
100 assertEqual(undefined, storage
.getObject('should be undefined', 0));
101 assertEqual(0, storage
.getObject('should not exist', 0));
103 it('keeps defaults', function () { with (this) {
104 var storage
= new ystorage
.Storage(null, { a
: 'a' });
105 assertEqual('a', storage
.getObject('a'));
106 storage
.setObject('a', 'b');
107 assertEqual('b', storage
.getObject('a'));
108 storage
.removeObject('a');
109 assertEqual('a', storage
.getObject('a'));