| 1 |
// Copyright 2012 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 |
goog.provide('goog.labs.structs.MapTest'); |
| 16 |
goog.setTestOnly('goog.labs.structs.MapTest'); |
| 17 |
|
| 18 |
goog.require('goog.labs.structs.Map'); |
| 19 |
goog.require('goog.testing.PropertyReplacer'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var map; |
| 23 |
var stubs; |
| 24 |
|
| 25 |
function setUpPage() { |
| 26 |
stubs = new goog.testing.PropertyReplacer(); |
| 27 |
} |
| 28 |
|
| 29 |
function setUp() { |
| 30 |
map = new goog.labs.structs.Map(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
function testSet() { |
| 35 |
var key = 'test'; |
| 36 |
var value = 'value'; |
| 37 |
map.set(key, value); |
| 38 |
assertEquals(value, map.get(key)); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
function testSetsWithSameKey() { |
| 43 |
var key = 'test'; |
| 44 |
var value = 'value'; |
| 45 |
var value2 = 'value2'; |
| 46 |
map.set(key, value); |
| 47 |
map.set(key, value2); |
| 48 |
assertEquals(value2, map.get(key)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function testSetWithUndefinedValue() { |
| 53 |
var key = 'test'; |
| 54 |
map.set(key, undefined); |
| 55 |
assertUndefined(map.get(key)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
function testSetWithUnderUnderProtoUnderUnder() { |
| 60 |
var key = '__proto__'; |
| 61 |
var value = 'value'; |
| 62 |
var value2 = 'value2'; |
| 63 |
|
| 64 |
map.set(key, value); |
| 65 |
assertEquals(value, map.get(key)); |
| 66 |
|
| 67 |
map.set(key, value2); |
| 68 |
assertEquals(value2, map.get(key)); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
function testSetWithBuiltInPropertyShadows() { |
| 73 |
var key = 'toString'; |
| 74 |
var value = 'value'; |
| 75 |
var key2 = 'hasOwnProperty'; |
| 76 |
var value2 = 'value2'; |
| 77 |
|
| 78 |
map.set(key, value); |
| 79 |
map.set(key2, value2); |
| 80 |
assertEquals(value, map.get(key)); |
| 81 |
assertEquals(value2, map.get(key2)); |
| 82 |
|
| 83 |
map.set(key, value2); |
| 84 |
map.set(key2, value); |
| 85 |
assertEquals(value2, map.get(key)); |
| 86 |
assertEquals(value, map.get(key2)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
function testGetBeforeSetOfUnderUnderProtoUnderUnder() { |
| 91 |
assertUndefined(map.get('__proto__')); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
function testContainsKey() { |
| 96 |
assertFalse(map.containsKey('key')); |
| 97 |
assertFalse(map.containsKey('__proto__')); |
| 98 |
assertFalse(map.containsKey('toString')); |
| 99 |
assertFalse(map.containsKey('hasOwnProperty')); |
| 100 |
assertFalse(map.containsKey('key2')); |
| 101 |
assertFalse(map.containsKey('key3')); |
| 102 |
assertFalse(map.containsKey('key4')); |
| 103 |
|
| 104 |
map.set('key', 'v'); |
| 105 |
map.set('__proto__', 'v'); |
| 106 |
map.set('toString', 'v'); |
| 107 |
map.set('hasOwnProperty', 'v'); |
| 108 |
map.set('key2', undefined); |
| 109 |
map.set('key3', null); |
| 110 |
map.set('key4', ''); |
| 111 |
|
| 112 |
assertTrue(map.containsKey('key')); |
| 113 |
assertTrue(map.containsKey('__proto__')); |
| 114 |
assertTrue(map.containsKey('toString')); |
| 115 |
assertTrue(map.containsKey('hasOwnProperty')); |
| 116 |
assertTrue(map.containsKey('key2')); |
| 117 |
assertTrue(map.containsKey('key3')); |
| 118 |
assertTrue(map.containsKey('key4')); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function testContainsValueWithShadowKeys() { |
| 123 |
assertFalse(map.containsValue('v2')); |
| 124 |
assertFalse(map.containsValue('v3')); |
| 125 |
assertFalse(map.containsValue('v4')); |
| 126 |
|
| 127 |
map.set('__proto__', 'v2'); |
| 128 |
map.set('toString', 'v3'); |
| 129 |
map.set('hasOwnProperty', 'v4'); |
| 130 |
|
| 131 |
assertTrue(map.containsValue('v2')); |
| 132 |
assertTrue(map.containsValue('v3')); |
| 133 |
assertTrue(map.containsValue('v4')); |
| 134 |
|
| 135 |
assertFalse(map.containsValue(Object.prototype.toString)); |
| 136 |
assertFalse(map.containsValue(Object.prototype.hasOwnProperty)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
function testContainsValueWithNullAndUndefined() { |
| 141 |
assertFalse(map.containsValue(undefined)); |
| 142 |
assertFalse(map.containsValue(null)); |
| 143 |
|
| 144 |
map.set('key2', undefined); |
| 145 |
map.set('key3', null); |
| 146 |
|
| 147 |
assertTrue(map.containsValue(undefined)); |
| 148 |
assertTrue(map.containsValue(null)); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
function testContainsValueWithNumber() { |
| 153 |
assertFalse(map.containsValue(-1)); |
| 154 |
assertFalse(map.containsValue(0)); |
| 155 |
assertFalse(map.containsValue(1)); |
| 156 |
map.set('key', -1); |
| 157 |
map.set('key2', 0); |
| 158 |
map.set('key3', 1); |
| 159 |
assertTrue(map.containsValue(-1)); |
| 160 |
assertTrue(map.containsValue(0)); |
| 161 |
assertTrue(map.containsValue(1)); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
function testContainsValueWithNaN() { |
| 166 |
assertFalse(map.containsValue(NaN)); |
| 167 |
map.set('key', NaN); |
| 168 |
assertTrue(map.containsValue(NaN)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
function testContainsValueWithNegativeZero() { |
| 173 |
assertFalse(map.containsValue(-0)); |
| 174 |
map.set('key', -0); |
| 175 |
assertTrue(map.containsValue(-0)); |
| 176 |
assertFalse(map.containsValue(0)); |
| 177 |
|
| 178 |
map.set('key', 0); |
| 179 |
assertFalse(map.containsValue(-0)); |
| 180 |
assertTrue(map.containsValue(0)); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
function testContainsValueWithStrings() { |
| 185 |
assertFalse(map.containsValue('')); |
| 186 |
assertFalse(map.containsValue('v')); |
| 187 |
map.set('key', ''); |
| 188 |
map.set('key2', 'v'); |
| 189 |
assertTrue(map.containsValue('')); |
| 190 |
assertTrue(map.containsValue('v')); |
| 191 |
} |
| 192 |
|
| 193 |
function testRemove() { |
| 194 |
map.set('key', 'v'); |
| 195 |
map.set('__proto__', 'v2'); |
| 196 |
map.set('toString', 'v3'); |
| 197 |
map.set('hasOwnProperty', 'v4'); |
| 198 |
map.set('key2', undefined); |
| 199 |
map.set('key3', null); |
| 200 |
map.set('key4', ''); |
| 201 |
|
| 202 |
assertFalse(map.remove('key do not exist')); |
| 203 |
|
| 204 |
assertTrue(map.remove('key')); |
| 205 |
assertFalse(map.containsKey('key')); |
| 206 |
assertFalse(map.remove('key')); |
| 207 |
|
| 208 |
assertTrue(map.remove('__proto__')); |
| 209 |
assertFalse(map.containsKey('__proto__')); |
| 210 |
assertFalse(map.remove('__proto__')); |
| 211 |
|
| 212 |
assertTrue(map.remove('toString')); |
| 213 |
assertFalse(map.containsKey('toString')); |
| 214 |
assertFalse(map.remove('toString')); |
| 215 |
|
| 216 |
assertTrue(map.remove('hasOwnProperty')); |
| 217 |
assertFalse(map.containsKey('hasOwnProperty')); |
| 218 |
assertFalse(map.remove('hasOwnProperty')); |
| 219 |
|
| 220 |
assertTrue(map.remove('key2')); |
| 221 |
assertFalse(map.containsKey('key2')); |
| 222 |
assertFalse(map.remove('key2')); |
| 223 |
|
| 224 |
assertTrue(map.remove('key3')); |
| 225 |
assertFalse(map.containsKey('key3')); |
| 226 |
assertFalse(map.remove('key3')); |
| 227 |
|
| 228 |
assertTrue('', map.remove('key4')); |
| 229 |
assertFalse(map.containsKey('key4')); |
| 230 |
assertFalse(map.remove('key4')); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
function testGetCountAndIsEmpty() { |
| 235 |
assertEquals(0, map.getCount()); |
| 236 |
assertTrue(map.isEmpty()); |
| 237 |
|
| 238 |
map.set('key', 'v'); |
| 239 |
assertEquals(1, map.getCount()); |
| 240 |
map.set('__proto__', 'v2'); |
| 241 |
assertEquals(2, map.getCount()); |
| 242 |
map.set('toString', 'v3'); |
| 243 |
assertEquals(3, map.getCount()); |
| 244 |
map.set('hasOwnProperty', 'v4'); |
| 245 |
assertEquals(4, map.getCount()); |
| 246 |
|
| 247 |
map.set('key', 'a'); |
| 248 |
assertEquals(4, map.getCount()); |
| 249 |
map.set('__proto__', 'a2'); |
| 250 |
assertEquals(4, map.getCount()); |
| 251 |
map.set('toString', 'a3'); |
| 252 |
assertEquals(4, map.getCount()); |
| 253 |
map.set('hasOwnProperty', 'a4'); |
| 254 |
assertEquals(4, map.getCount()); |
| 255 |
|
| 256 |
map.remove('key'); |
| 257 |
assertEquals(3, map.getCount()); |
| 258 |
map.remove('__proto__'); |
| 259 |
assertEquals(2, map.getCount()); |
| 260 |
map.remove('toString'); |
| 261 |
assertEquals(1, map.getCount()); |
| 262 |
map.remove('hasOwnProperty'); |
| 263 |
assertEquals(0, map.getCount()); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
function testClear() { |
| 268 |
map.set('key', 'v'); |
| 269 |
map.set('__proto__', 'v'); |
| 270 |
map.set('toString', 'v'); |
| 271 |
map.set('hasOwnProperty', 'v'); |
| 272 |
map.set('key2', undefined); |
| 273 |
map.set('key3', null); |
| 274 |
map.set('key4', ''); |
| 275 |
|
| 276 |
map.clear(); |
| 277 |
|
| 278 |
assertFalse(map.containsKey('key')); |
| 279 |
assertFalse(map.containsKey('__proto__')); |
| 280 |
assertFalse(map.containsKey('toString')); |
| 281 |
assertFalse(map.containsKey('hasOwnProperty')); |
| 282 |
assertFalse(map.containsKey('key2')); |
| 283 |
assertFalse(map.containsKey('key3')); |
| 284 |
assertFalse(map.containsKey('key4')); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
function testGetEntries() { |
| 289 |
map.set('key', 'v'); |
| 290 |
map.set('__proto__', 'v'); |
| 291 |
map.set('toString', 'v'); |
| 292 |
map.set('hasOwnProperty', 'v'); |
| 293 |
map.set('key2', undefined); |
| 294 |
map.set('key3', null); |
| 295 |
map.set('key4', ''); |
| 296 |
|
| 297 |
var entries = map.getEntries(); |
| 298 |
assertEquals(7, entries.length); |
| 299 |
assertContainsEntry(['key', 'v'], entries); |
| 300 |
assertContainsEntry(['__proto__', 'v'], entries); |
| 301 |
assertContainsEntry(['toString', 'v'], entries); |
| 302 |
assertContainsEntry(['hasOwnProperty', 'v'], entries); |
| 303 |
assertContainsEntry(['key2', undefined], entries); |
| 304 |
assertContainsEntry(['key3', null], entries); |
| 305 |
assertContainsEntry(['key4', ''], entries); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
function testGetKeys() { |
| 310 |
map.set('key', 'v'); |
| 311 |
map.set('__proto__', 'v'); |
| 312 |
map.set('toString', 'v'); |
| 313 |
map.set('hasOwnProperty', 'v'); |
| 314 |
map.set('key2', undefined); |
| 315 |
map.set('key3', null); |
| 316 |
map.set('k4', ''); |
| 317 |
|
| 318 |
var values = map.getKeys(); |
| 319 |
assertSameElements( |
| 320 |
['key', '__proto__', 'toString', 'hasOwnProperty', 'key2', 'key3', 'k4'], |
| 321 |
values); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
function testGetValues() { |
| 326 |
map.set('key', 'v'); |
| 327 |
map.set('__proto__', 'v'); |
| 328 |
map.set('toString', 'v'); |
| 329 |
map.set('hasOwnProperty', 'v'); |
| 330 |
map.set('key2', undefined); |
| 331 |
map.set('key3', null); |
| 332 |
map.set('key4', ''); |
| 333 |
|
| 334 |
var values = map.getValues(); |
| 335 |
assertSameElements(['v', 'v', 'v', 'v', undefined, null, ''], values); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
function testAddAllToEmptyMap() { |
| 340 |
map.set('key', 'v'); |
| 341 |
map.set('key2', 'v2'); |
| 342 |
map.set('key3', 'v3'); |
| 343 |
map.set('key4', 'v4'); |
| 344 |
|
| 345 |
var map2 = new goog.labs.structs.Map(); |
| 346 |
map2.addAll(map); |
| 347 |
|
| 348 |
assertEquals(4, map2.getCount()); |
| 349 |
assertEquals('v', map2.get('key')); |
| 350 |
assertEquals('v2', map2.get('key2')); |
| 351 |
assertEquals('v3', map2.get('key3')); |
| 352 |
assertEquals('v4', map2.get('key4')); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
function testAddAllToNonEmptyMap() { |
| 357 |
map.set('key', 'v'); |
| 358 |
map.set('key2', 'v2'); |
| 359 |
map.set('key3', 'v3'); |
| 360 |
map.set('key4', 'v4'); |
| 361 |
|
| 362 |
var map2 = new goog.labs.structs.Map(); |
| 363 |
map2.set('key0', 'o'); |
| 364 |
map2.set('key', 'o'); |
| 365 |
map2.set('key2', 'o2'); |
| 366 |
map2.set('key3', 'o3'); |
| 367 |
map2.addAll(map); |
| 368 |
|
| 369 |
assertEquals(5, map2.getCount()); |
| 370 |
assertEquals('o', map2.get('key0')); |
| 371 |
assertEquals('v', map2.get('key')); |
| 372 |
assertEquals('v2', map2.get('key2')); |
| 373 |
assertEquals('v3', map2.get('key3')); |
| 374 |
assertEquals('v4', map2.get('key4')); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
function testClone() { |
| 379 |
map.set('key', 'v'); |
| 380 |
map.set('key2', 'v2'); |
| 381 |
map.set('key3', 'v3'); |
| 382 |
map.set('key4', 'v4'); |
| 383 |
|
| 384 |
var map2 = map.clone(); |
| 385 |
|
| 386 |
assertEquals(4, map2.getCount()); |
| 387 |
assertEquals('v', map2.get('key')); |
| 388 |
assertEquals('v2', map2.get('key2')); |
| 389 |
assertEquals('v3', map2.get('key3')); |
| 390 |
assertEquals('v4', map2.get('key4')); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
function testMapWithModifiedObjectPrototype() { |
| 395 |
stubs.set(Object.prototype, 'toString', function() {}); |
| 396 |
stubs.set(Object.prototype, 'foo', function() {}); |
| 397 |
stubs.set(Object.prototype, 'field', 100); |
| 398 |
stubs.set(Object.prototype, 'fooKey', function() {}); |
| 399 |
|
| 400 |
map = new goog.labs.structs.Map(); |
| 401 |
map.set('key', 'v'); |
| 402 |
map.set('key2', 'v2'); |
| 403 |
map.set('fooKey', 'v3'); |
| 404 |
|
| 405 |
assertTrue(map.containsKey('key')); |
| 406 |
assertTrue(map.containsKey('key2')); |
| 407 |
assertTrue(map.containsKey('fooKey')); |
| 408 |
assertFalse(map.containsKey('toString')); |
| 409 |
assertFalse(map.containsKey('foo')); |
| 410 |
assertFalse(map.containsKey('field')); |
| 411 |
|
| 412 |
assertTrue(map.containsValue('v')); |
| 413 |
assertTrue(map.containsValue('v2')); |
| 414 |
assertTrue(map.containsValue('v3')); |
| 415 |
assertFalse(map.containsValue(100)); |
| 416 |
|
| 417 |
var entries = map.getEntries(); |
| 418 |
assertEquals(3, entries.length); |
| 419 |
assertContainsEntry(['key', 'v'], entries); |
| 420 |
assertContainsEntry(['key2', 'v2'], entries); |
| 421 |
assertContainsEntry(['fooKey', 'v3'], entries); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
function assertContainsEntry(entry, entryList) { |
| 426 |
for (var i = 0; i < entryList.length; ++i) { |
| 427 |
if (entry[0] == entryList[i][0] && entry[1] === entryList[i][1]) { |
| 428 |
return; |
| 429 |
} |
| 430 |
} |
| 431 |
fail('Did not find entry: ' + entry + ' in: ' + entryList); |
| 432 |
} |