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