mechanismseparationtester.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 separation.
  16. *
  17. * These tests should be included by tests of any mechanism which natively
  18. * implements namespaces. There is no need to include those tests for mechanisms
  19. * extending goog.storage.mechanism.PrefixedMechanism. Make sure a different
  20. * namespace is used for each object.
  21. *
  22. */
  23. goog.provide('goog.storage.mechanism.mechanismSeparationTester');
  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.mechanismSeparationTester');
  29. function testSeparateSet() {
  30. if (!mechanism || !mechanism_separate) {
  31. return;
  32. }
  33. mechanism.set('first', 'one');
  34. assertNull(mechanism_separate.get('first'));
  35. assertEquals(0, mechanism_separate.getCount());
  36. assertEquals(
  37. goog.iter.StopIteration,
  38. assertThrows(mechanism_separate.__iterator__().next));
  39. }
  40. function testSeparateSetInverse() {
  41. if (!mechanism || !mechanism_separate) {
  42. return;
  43. }
  44. mechanism.set('first', 'one');
  45. mechanism_separate.set('first', 'two');
  46. assertEquals('one', mechanism.get('first'));
  47. assertEquals(1, mechanism.getCount());
  48. var iterator = mechanism.__iterator__();
  49. assertEquals('one', iterator.next());
  50. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  51. }
  52. function testSeparateRemove() {
  53. if (!mechanism || !mechanism_separate) {
  54. return;
  55. }
  56. mechanism.set('first', 'one');
  57. mechanism_separate.remove('first');
  58. assertEquals('one', mechanism.get('first'));
  59. assertEquals(1, mechanism.getCount());
  60. var iterator = mechanism.__iterator__();
  61. assertEquals('one', iterator.next());
  62. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  63. }
  64. function testSeparateClean() {
  65. if (!mechanism || !mechanism_separate) {
  66. return;
  67. }
  68. mechanism_separate.set('first', 'two');
  69. mechanism.clear();
  70. assertEquals('two', mechanism_separate.get('first'));
  71. assertEquals(1, mechanism_separate.getCount());
  72. var iterator = mechanism_separate.__iterator__();
  73. assertEquals('two', iterator.next());
  74. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  75. }