mechanismsharingtester.js 2.7 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. /**
  15. * @fileoverview Unit tests for storage mechanism sharing.
  16. *
  17. * These tests should be included in tests of any storage mechanism in which
  18. * separate mechanism instances share the same underlying storage. Most (if
  19. * not all) storage mechanisms should have this property. If the mechanism
  20. * employs namespaces, make sure the same namespace is used for both objects.
  21. *
  22. */
  23. goog.provide('goog.storage.mechanism.mechanismSharingTester');
  24. goog.require('goog.iter.StopIteration');
  25. /** @suppress {extraRequire} */
  26. goog.require('goog.storage.mechanism.mechanismTestDefinition');
  27. goog.require('goog.testing.asserts');
  28. goog.setTestOnly('goog.storage.mechanism.mechanismSharingTester');
  29. function testSharedSet() {
  30. if (!mechanism || !mechanism_shared) {
  31. return;
  32. }
  33. mechanism.set('first', 'one');
  34. assertEquals('one', mechanism_shared.get('first'));
  35. assertEquals(1, mechanism_shared.getCount());
  36. var iterator = mechanism_shared.__iterator__();
  37. assertEquals('one', iterator.next());
  38. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  39. }
  40. function testSharedSetInverse() {
  41. if (!mechanism || !mechanism_shared) {
  42. return;
  43. }
  44. mechanism_shared.set('first', 'two');
  45. assertEquals('two', mechanism.get('first'));
  46. assertEquals(1, mechanism.getCount());
  47. var iterator = mechanism.__iterator__();
  48. assertEquals('two', iterator.next());
  49. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  50. }
  51. function testSharedRemove() {
  52. if (!mechanism || !mechanism_shared) {
  53. return;
  54. }
  55. mechanism_shared.set('first', 'three');
  56. mechanism.remove('first');
  57. assertNull(mechanism_shared.get('first'));
  58. assertEquals(0, mechanism_shared.getCount());
  59. assertEquals(
  60. goog.iter.StopIteration,
  61. assertThrows(mechanism_shared.__iterator__().next));
  62. }
  63. function testSharedClean() {
  64. if (!mechanism || !mechanism_shared) {
  65. return;
  66. }
  67. mechanism.set('first', 'four');
  68. mechanism_shared.clear();
  69. assertEquals(0, mechanism.getCount());
  70. assertEquals(
  71. goog.iter.StopIteration, assertThrows(mechanism.__iterator__().next));
  72. }