reflect_test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2016 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. goog.provide('goog.reflectTest');
  15. goog.setTestOnly('goog.reflectTest');
  16. goog.require('goog.object');
  17. goog.require('goog.reflect');
  18. goog.require('goog.testing.jsunit');
  19. /**
  20. * @param {number} key
  21. * @return {number}
  22. */
  23. var doubleFn = function(key) {
  24. return key * 2;
  25. };
  26. /**
  27. * @param {number} key
  28. * @return {number}
  29. */
  30. var tripleFn = function(key) {
  31. return key * 3;
  32. };
  33. /**
  34. * @param {number} key
  35. */
  36. var throwsFn = function(key) {
  37. throw new Error("Shouldn't be called.");
  38. };
  39. /**
  40. * @param {string} key
  41. * @return {string}
  42. */
  43. var passthroughFn = function(key) {
  44. return key;
  45. };
  46. function testCache() {
  47. var cacheObj = {};
  48. assertEquals(2, goog.reflect.cache(cacheObj, 1, doubleFn));
  49. assertEquals(2, cacheObj[1]);
  50. assertEquals(1, goog.object.getCount(cacheObj));
  51. // Ensure we get the same value with a different valueFn.
  52. assertEquals(2, goog.reflect.cache(cacheObj, 1, throwsFn));
  53. assertEquals(2, cacheObj[1]);
  54. assertEquals(1, goog.object.getCount(cacheObj));
  55. // Ensure cache works with all string keys.
  56. assertEquals(
  57. 'toString', goog.reflect.cache(cacheObj, 'toString', passthroughFn));
  58. assertEquals('toString', goog.reflect.cache(cacheObj, 'toString', throwsFn));
  59. // Not checking count as it is not correct for IE8 (doesn't enumerate
  60. // toString).
  61. }
  62. function testCache_keyFn() {
  63. var cacheObj = {};
  64. assertEquals(3, goog.reflect.cache(cacheObj, 1, tripleFn, doubleFn));
  65. assertEquals(3, cacheObj[2]);
  66. assertEquals(1, goog.object.getCount(cacheObj));
  67. // Ensure we get the same value with a different valueFn.
  68. assertEquals(3, goog.reflect.cache(cacheObj, 1, throwsFn, doubleFn));
  69. assertEquals(3, cacheObj[2]);
  70. assertEquals(1, goog.object.getCount(cacheObj));
  71. // Ensure we set a new value if we provide a different keyFn.
  72. assertEquals(2, goog.reflect.cache(cacheObj, 1, doubleFn, tripleFn));
  73. assertEquals(2, cacheObj[3]);
  74. assertEquals(2, goog.object.getCount(cacheObj));
  75. // Ensure the keyFn is always called.
  76. assertThrows(function() {
  77. goog.reflect.cache(cacheObj, 1, tripleFn, throwsFn);
  78. });
  79. }