storagetester.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2017 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. * @fileoverview Helper for various storage tests.
  16. *
  17. * @author johnlenz@google.com (John Lenz)
  18. */
  19. goog.provide('goog.storage.storageTester');
  20. goog.setTestOnly();
  21. goog.require('goog.storage.Storage');
  22. goog.require('goog.structs.Map');
  23. goog.require('goog.testing.asserts');
  24. /**
  25. * @param {!goog.storage.Storage} storage
  26. */
  27. goog.storage.storageTester.runBasicTests = function(storage) {
  28. // Simple Objects.
  29. storage.set('first', 'Hello world!');
  30. storage.set('second', ['one', 'two', 'three']);
  31. storage.set('third', {'a': 97, 'b': 98});
  32. assertEquals('Hello world!', storage.get('first'));
  33. assertObjectEquals(['one', 'two', 'three'], storage.get('second'));
  34. assertObjectEquals({'a': 97, 'b': 98}, storage.get('third'));
  35. // Some more complex fun with a Map.
  36. var map = new goog.structs.Map();
  37. map.set('Alice', 'Hello world!');
  38. map.set('Bob', ['one', 'two', 'three']);
  39. map.set('Cecile', {'a': 97, 'b': 98});
  40. storage.set('first', map.toObject());
  41. assertObjectEquals(map.toObject(), storage.get('first'));
  42. // Setting weird values.
  43. storage.set('second', null);
  44. assertEquals(null, storage.get('second'));
  45. storage.set('second', undefined);
  46. assertEquals(undefined, storage.get('second'));
  47. storage.set('second', '');
  48. assertEquals('', storage.get('second'));
  49. // Clean up.
  50. storage.remove('first');
  51. storage.remove('second');
  52. storage.remove('third');
  53. assertUndefined(storage.get('first'));
  54. assertUndefined(storage.get('second'));
  55. assertUndefined(storage.get('third'));
  56. };