mockstorage_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. goog.provide('goog.testing.MockStorageTest');
  15. goog.setTestOnly('goog.testing.MockStorageTest');
  16. goog.require('goog.testing.MockStorage');
  17. goog.require('goog.testing.jsunit');
  18. var instance;
  19. function setUp() {
  20. instance = new goog.testing.MockStorage();
  21. }
  22. /**
  23. * Tests the MockStorage interface.
  24. */
  25. function testMockStorage() {
  26. assertEquals(0, instance.length);
  27. instance.setItem('foo', 'bar');
  28. assertEquals(1, instance.length);
  29. assertEquals('bar', instance.getItem('foo'));
  30. assertEquals('foo', instance.key(0));
  31. instance.setItem('foo', 'baz');
  32. assertEquals('baz', instance.getItem('foo'));
  33. instance.setItem('goo', 'gl');
  34. assertEquals(2, instance.length);
  35. assertEquals('gl', instance.getItem('goo'));
  36. assertEquals('goo', instance.key(1));
  37. assertNull(instance.getItem('poogle'));
  38. instance.removeItem('foo');
  39. assertEquals(1, instance.length);
  40. assertEquals('goo', instance.key(0));
  41. instance.setItem('a', 12);
  42. assertEquals('12', instance.getItem('a'));
  43. instance.setItem('b', false);
  44. assertEquals('false', instance.getItem('b'));
  45. instance.setItem('c', {a: 1, b: 12});
  46. assertEquals('[object Object]', instance.getItem('c'));
  47. instance.clear();
  48. assertEquals(0, instance.length);
  49. // Test some special cases.
  50. instance.setItem('emptyString', '');
  51. assertEquals('', instance.getItem('emptyString'));
  52. instance.setItem('isNull', null);
  53. assertEquals('null', instance.getItem('isNull'));
  54. instance.setItem('isUndefined', undefined);
  55. assertEquals('undefined', instance.getItem('isUndefined'));
  56. instance.setItem('', 'empty key');
  57. assertEquals('empty key', instance.getItem(''));
  58. assertEquals(4, instance.length);
  59. }