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