Debian Codegraph @

closure-library/closure/goog/testing/mockstorage_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.MockStorageTest');
16 goog.setTestOnly('goog.testing.MockStorageTest');
17
18 goog.require('goog.testing.MockStorage');
19 goog.require('goog.testing.jsunit');
20
21 var instance;
22
23 function setUp() {
24 instance = new goog.testing.MockStorage();
25 }
26
27
28 /**
29 * Tests the MockStorage interface.
30 */
31 function testMockStorage() {
32 assertEquals(0, instance.length);
33
34 instance.setItem('foo', 'bar');
35 assertEquals(1, instance.length);
36 assertEquals('bar', instance.getItem('foo'));
37 assertEquals('foo', instance.key(0));
38
39 instance.setItem('foo', 'baz');
40 assertEquals('baz', instance.getItem('foo'));
41
42 instance.setItem('goo', 'gl');
43 assertEquals(2, instance.length);
44 assertEquals('gl', instance.getItem('goo'));
45 assertEquals('goo', instance.key(1));
46
47 assertNull(instance.getItem('poogle'));
48
49 instance.removeItem('foo');
50 assertEquals(1, instance.length);
51 assertEquals('goo', instance.key(0));
52
53 instance.setItem('a', 12);
54 assertEquals('12', instance.getItem('a'));
55 instance.setItem('b', false);
56 assertEquals('false', instance.getItem('b'));
57 instance.setItem('c', {a: 1, b: 12});
58 assertEquals('[object Object]', instance.getItem('c'));
59
60 instance.clear();
61 assertEquals(0, instance.length);
62
63 // Test some special cases.
64 instance.setItem('emptyString', '');
65 assertEquals('', instance.getItem('emptyString'));
66 instance.setItem('isNull', null);
67 assertEquals('null', instance.getItem('isNull'));
68 instance.setItem('isUndefined', undefined);
69 assertEquals('undefined', instance.getItem('isUndefined'));
70 instance.setItem('', 'empty key');
71 assertEquals('empty key', instance.getItem(''));
72 assertEquals(4, instance.length);
73 }