1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.debugTest');
goog.setTestOnly('goog.debugTest');
goog.require('goog.debug');
goog.require('goog.debug.errorcontext');
goog.require('goog.structs.Set');
goog.require('goog.testing.jsunit');
function testMakeWhitespaceVisible() {
assertEquals(
'Hello[_][_]World![r][n]\n' +
'[r][n]\n' +
'[f][f]I[_]am[t][t]here![r][n]\n',
goog.debug.makeWhitespaceVisible(
'Hello World!\r\n\r\n\f\fI am\t\there!\r\n'));
}
function testGetFunctionName() {
// Trivial resolver that matches just a few names: a static function, a
// constructor, and a member function.
var resolver = function(f) {
if (f === goog.debug.getFunctionName) {
return 'goog.debug.getFunctionName';
} else if (f === goog.structs.Set) {
return 'goog.structs.Set';
} else if (f === goog.structs.Set.prototype.getCount) {
return 'goog.structs.Set.getCount';
} else {
return null;
}
};
goog.debug.setFunctionResolver(resolver);
assertEquals(
'goog.debug.getFunctionName',
goog.debug.getFunctionName(goog.debug.getFunctionName));
assertEquals(
'goog.structs.Set', goog.debug.getFunctionName(goog.structs.Set));
var set = new goog.structs.Set();
assertEquals(
'goog.structs.Set.getCount', goog.debug.getFunctionName(set.getCount));
// This function is matched by the fallback heuristic.
assertEquals(
'testGetFunctionName', goog.debug.getFunctionName(testGetFunctionName));
goog.debug.setFunctionResolver(null);
}
/**
* Asserts that a substring can be found in a specified text string.
*
* @param {string} substring The substring to search for.
* @param {string} text The text string to search within.
*/
function assertContainsSubstring(substring, text) {
assertNotEquals(
'Could not find "' + substring + '" in "' + text + '"', -1,
text.search(substring));
}
function testDeepExpose() {
var a = {};
var b = {};
var c = {};
a.ancestor = a;
a.otherObject = b;
a.otherObjectAgain = b;
b.nextLevel = c;
// Add Uid to a before deepExpose.
var aUid = goog.getUid(a);
var deepExpose = goog.debug.deepExpose(a);
assertContainsSubstring(
'ancestor = ... reference loop detected .id=' + aUid + '. ...',
deepExpose);
assertContainsSubstring('otherObjectAgain = {', deepExpose);
// Make sure we've reset Uids after the deepExpose call.
assert(goog.hasUid(a));
assertFalse(goog.hasUid(b));
assertFalse(goog.hasUid(c));
}
function testEnhanceErrorWithContext() {
var err = 'abc';
var context = {firstKey: 'first', secondKey: 'another key'};
var errorWithContext = goog.debug.enhanceErrorWithContext(err, context);
assertObjectEquals(
context, goog.debug.errorcontext.getErrorContext(errorWithContext));
}
function testEnhanceErrorWithContext_combinedContext() {
var err = new Error('abc');
goog.debug.errorcontext.addErrorContext(err, 'a', '123');
var context = {b: '456', c: '789'};
var errorWithContext = goog.debug.enhanceErrorWithContext(err, context);
assertObjectEquals(
{a: '123', b: '456', c: '789'},
goog.debug.errorcontext.getErrorContext(errorWithContext));
}
function testFreeze_nonDebug() {
if (goog.DEBUG && typeof Object.freeze == 'function') return;
var a = {};
assertEquals(a, goog.debug.freeze(a));
a.foo = 42;
assertEquals(42, a.foo);
}
function testFreeze_debug() {
if (goog.DEBUG || typeof Object.freeze != 'function') return;
var a = {};
assertEquals(a, goog.debug.freeze(a));
try {
a.foo = 42;
} catch (expectedInStrictMode) {
}
assertUndefined(a.foo);
}
|