iterablemechanismtester.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 the iterable storage mechanism interface.
  16. *
  17. * These tests should be included in tests of any class extending
  18. * goog.storage.mechanism.IterableMechanism.
  19. *
  20. */
  21. goog.provide('goog.storage.mechanism.iterableMechanismTester');
  22. goog.require('goog.iter');
  23. goog.require('goog.iter.StopIteration');
  24. goog.require('goog.testing.asserts');
  25. goog.setTestOnly('iterableMechanismTester');
  26. var mechanism = null;
  27. function testCount() {
  28. if (!mechanism) {
  29. return;
  30. }
  31. assertEquals(0, mechanism.getCount());
  32. mechanism.set('first', 'one');
  33. assertEquals(1, mechanism.getCount());
  34. mechanism.set('second', 'two');
  35. assertEquals(2, mechanism.getCount());
  36. mechanism.set('first', 'three');
  37. assertEquals(2, mechanism.getCount());
  38. }
  39. function testIteratorBasics() {
  40. if (!mechanism) {
  41. return;
  42. }
  43. mechanism.set('first', 'one');
  44. assertEquals('first', mechanism.__iterator__(true).next());
  45. assertEquals('one', mechanism.__iterator__(false).next());
  46. var iterator = mechanism.__iterator__();
  47. assertEquals('one', iterator.next());
  48. assertEquals(goog.iter.StopIteration, assertThrows(iterator.next));
  49. }
  50. function testIteratorWithTwoValues() {
  51. if (!mechanism) {
  52. return;
  53. }
  54. mechanism.set('first', 'one');
  55. mechanism.set('second', 'two');
  56. assertSameElements(['one', 'two'], goog.iter.toArray(mechanism));
  57. assertSameElements(
  58. ['first', 'second'], goog.iter.toArray(mechanism.__iterator__(true)));
  59. }
  60. function testClear() {
  61. if (!mechanism) {
  62. return;
  63. }
  64. mechanism.set('first', 'one');
  65. mechanism.set('second', 'two');
  66. mechanism.clear();
  67. assertNull(mechanism.get('first'));
  68. assertNull(mechanism.get('second'));
  69. assertEquals(0, mechanism.getCount());
  70. assertEquals(
  71. goog.iter.StopIteration, assertThrows(mechanism.__iterator__(true).next));
  72. assertEquals(
  73. goog.iter.StopIteration,
  74. assertThrows(mechanism.__iterator__(false).next));
  75. }
  76. function testClearClear() {
  77. if (!mechanism) {
  78. return;
  79. }
  80. mechanism.clear();
  81. mechanism.clear();
  82. assertEquals(0, mechanism.getCount());
  83. }
  84. function testIteratorWithWeirdKeys() {
  85. if (!mechanism) {
  86. return;
  87. }
  88. mechanism.set(' ', 'space');
  89. mechanism.set('=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`', 'control');
  90. mechanism.set(
  91. '\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341', 'ten');
  92. assertEquals(3, mechanism.getCount());
  93. assertSameElements(
  94. [
  95. ' ', '=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`',
  96. '\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341'
  97. ],
  98. goog.iter.toArray(mechanism.__iterator__(true)));
  99. mechanism.clear();
  100. assertEquals(0, mechanism.getCount());
  101. }