weak_test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2014 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 Tests for {@code goog.structs.weak}. set, has, and remove are
  16. * exercised in {@code goog.structs.weak.MapTest} and
  17. * {@code goog.structs.weak.SetTest}.
  18. *
  19. */
  20. goog.provide('goog.structs.weakTest');
  21. goog.setTestOnly('goog.structs.weakTest');
  22. goog.require('goog.array');
  23. goog.require('goog.structs.weak');
  24. goog.require('goog.testing.jsunit');
  25. function shouldRunTests() {
  26. return goog.structs.weak.SUPPORTED_BROWSER;
  27. }
  28. function testGenerateId() {
  29. assertNotEquals(
  30. goog.structs.weak.generateId(), goog.structs.weak.generateId());
  31. }
  32. function testCheckKeyTypeValidObject() {
  33. var validKeys = [{}, [], document.body, /RegExp/, goog.nullFunction];
  34. goog.array.forEach(validKeys, function(key) {
  35. // no error
  36. goog.structs.weak.checkKeyType(key);
  37. });
  38. }
  39. function testCheckKeyTypePrimitive() {
  40. var primitiveKeys = ['test', 1, true, null, undefined];
  41. goog.array.forEach(primitiveKeys, function(key) {
  42. assertThrows(function() { goog.structs.weak.checkKeyType(key); });
  43. });
  44. }
  45. function testCheckKeyTypeNonExtensibleObject() {
  46. var sealedObj = {}, frozenObj = {}, preventExtensionsObj = {};
  47. Object.seal(sealedObj);
  48. Object.freeze(frozenObj);
  49. Object.preventExtensions(preventExtensionsObj);
  50. assertThrows(function() { goog.structs.weak.checkKeyType(sealedObj); });
  51. assertThrows(function() { goog.structs.weak.checkKeyType(frozenObj); });
  52. assertThrows(function() {
  53. goog.structs.weak.checkKeyType(preventExtensionsObj);
  54. });
  55. }