richstorage_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.storage.RichStorageTest');
  15. goog.setTestOnly('goog.storage.RichStorageTest');
  16. goog.require('goog.storage.ErrorCode');
  17. goog.require('goog.storage.RichStorage');
  18. goog.require('goog.storage.storageTester');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.storage.FakeMechanism');
  21. function testBasicOperations() {
  22. var mechanism = new goog.testing.storage.FakeMechanism();
  23. var storage = new goog.storage.RichStorage(mechanism);
  24. goog.storage.storageTester.runBasicTests(storage);
  25. }
  26. function testWrapping() {
  27. var mechanism = new goog.testing.storage.FakeMechanism();
  28. var storage = new goog.storage.RichStorage(mechanism);
  29. // Some metadata.
  30. var object = {'a': 97, 'b': 98};
  31. var wrapper = new goog.storage.RichStorage.Wrapper(object);
  32. wrapper['meta'] = 'info';
  33. storage.set('first', wrapper);
  34. assertObjectEquals(object, storage.get('first'));
  35. assertObjectEquals(wrapper, storage.getWrapper('first'));
  36. assertEquals('info', storage.getWrapper('first')['meta']);
  37. // Multiple wrappings.
  38. var wrapper1 = goog.storage.RichStorage.Wrapper.wrapIfNecessary(object);
  39. wrapper1['some'] = 'meta';
  40. var wrapper2 = goog.storage.RichStorage.Wrapper.wrapIfNecessary(wrapper1);
  41. wrapper2['more'] = 'stuff';
  42. storage.set('second', wrapper2);
  43. assertObjectEquals(object, storage.get('second'));
  44. assertObjectEquals(wrapper2, storage.getWrapper('second'));
  45. assertEquals('meta', storage.getWrapper('second')['some']);
  46. assertEquals('stuff', storage.getWrapper('second')['more']);
  47. // Invalid wrappings.
  48. mechanism.set('third', 'null');
  49. assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() {
  50. storage.get('third');
  51. }));
  52. mechanism.set('third', '{"meta": "data"}');
  53. assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() {
  54. storage.get('third');
  55. }));
  56. // Weird values.
  57. var wrapperA = new goog.storage.RichStorage.Wrapper.wrapIfNecessary(null);
  58. wrapperA['one'] = 1;
  59. storage.set('first', wrapperA);
  60. assertObjectEquals(wrapperA, storage.getWrapper('first'));
  61. var wrapperB = new goog.storage.RichStorage.Wrapper.wrapIfNecessary('');
  62. wrapperA['two'] = [];
  63. storage.set('second', wrapperB);
  64. assertObjectEquals(wrapperB, storage.getWrapper('second'));
  65. // Clean up.
  66. storage.remove('first');
  67. storage.remove('second');
  68. storage.remove('third');
  69. assertUndefined(storage.get('first'));
  70. assertUndefined(storage.get('second'));
  71. assertUndefined(storage.get('third'));
  72. assertNull(mechanism.get('first'));
  73. assertNull(mechanism.get('second'));
  74. assertNull(mechanism.get('third'));
  75. }