| 1 |
// Copyright 2008 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.debugTest'); |
| 16 |
goog.setTestOnly('goog.debugTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug'); |
| 19 |
goog.require('goog.debug.errorcontext'); |
| 20 |
goog.require('goog.structs.Set'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
function testMakeWhitespaceVisible() { |
| 24 |
assertEquals( |
| 25 |
'Hello[_][_]World![r][n]\n' + |
| 26 |
'[r][n]\n' + |
| 27 |
'[f][f]I[_]am[t][t]here![r][n]\n', |
| 28 |
goog.debug.makeWhitespaceVisible( |
| 29 |
'Hello World!\r\n\r\n\f\fI am\t\there!\r\n')); |
| 30 |
} |
| 31 |
|
| 32 |
function testGetFunctionName() { |
| 33 |
// Trivial resolver that matches just a few names: a static function, a |
| 34 |
// constructor, and a member function. |
| 35 |
var resolver = function(f) { |
| 36 |
if (f === goog.debug.getFunctionName) { |
| 37 |
return 'goog.debug.getFunctionName'; |
| 38 |
} else if (f === goog.structs.Set) { |
| 39 |
return 'goog.structs.Set'; |
| 40 |
} else if (f === goog.structs.Set.prototype.getCount) { |
| 41 |
return 'goog.structs.Set.getCount'; |
| 42 |
} else { |
| 43 |
return null; |
| 44 |
} |
| 45 |
}; |
| 46 |
goog.debug.setFunctionResolver(resolver); |
| 47 |
|
| 48 |
assertEquals( |
| 49 |
'goog.debug.getFunctionName', |
| 50 |
goog.debug.getFunctionName(goog.debug.getFunctionName)); |
| 51 |
assertEquals( |
| 52 |
'goog.structs.Set', goog.debug.getFunctionName(goog.structs.Set)); |
| 53 |
var set = new goog.structs.Set(); |
| 54 |
assertEquals( |
| 55 |
'goog.structs.Set.getCount', goog.debug.getFunctionName(set.getCount)); |
| 56 |
|
| 57 |
// This function is matched by the fallback heuristic. |
| 58 |
assertEquals( |
| 59 |
'testGetFunctionName', goog.debug.getFunctionName(testGetFunctionName)); |
| 60 |
|
| 61 |
goog.debug.setFunctionResolver(null); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Asserts that a substring can be found in a specified text string. |
| 67 |
* |
| 68 |
* @param {string} substring The substring to search for. |
| 69 |
* @param {string} text The text string to search within. |
| 70 |
*/ |
| 71 |
function assertContainsSubstring(substring, text) { |
| 72 |
assertNotEquals( |
| 73 |
'Could not find "' + substring + '" in "' + text + '"', -1, |
| 74 |
text.search(substring)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
function testDeepExpose() { |
| 79 |
var a = {}; |
| 80 |
var b = {}; |
| 81 |
var c = {}; |
| 82 |
a.ancestor = a; |
| 83 |
a.otherObject = b; |
| 84 |
a.otherObjectAgain = b; |
| 85 |
b.nextLevel = c; |
| 86 |
// Add Uid to a before deepExpose. |
| 87 |
var aUid = goog.getUid(a); |
| 88 |
|
| 89 |
var deepExpose = goog.debug.deepExpose(a); |
| 90 |
|
| 91 |
assertContainsSubstring( |
| 92 |
'ancestor = ... reference loop detected .id=' + aUid + '. ...', |
| 93 |
deepExpose); |
| 94 |
|
| 95 |
assertContainsSubstring('otherObjectAgain = {', deepExpose); |
| 96 |
|
| 97 |
// Make sure we've reset Uids after the deepExpose call. |
| 98 |
assert(goog.hasUid(a)); |
| 99 |
assertFalse(goog.hasUid(b)); |
| 100 |
assertFalse(goog.hasUid(c)); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testEnhanceErrorWithContext() { |
| 105 |
var err = 'abc'; |
| 106 |
var context = {firstKey: 'first', secondKey: 'another key'}; |
| 107 |
var errorWithContext = goog.debug.enhanceErrorWithContext(err, context); |
| 108 |
assertObjectEquals( |
| 109 |
context, goog.debug.errorcontext.getErrorContext(errorWithContext)); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
function testEnhanceErrorWithContext_combinedContext() { |
| 114 |
var err = new Error('abc'); |
| 115 |
goog.debug.errorcontext.addErrorContext(err, 'a', '123'); |
| 116 |
var context = {b: '456', c: '789'}; |
| 117 |
var errorWithContext = goog.debug.enhanceErrorWithContext(err, context); |
| 118 |
assertObjectEquals( |
| 119 |
{a: '123', b: '456', c: '789'}, |
| 120 |
goog.debug.errorcontext.getErrorContext(errorWithContext)); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testFreeze_nonDebug() { |
| 125 |
if (goog.DEBUG && typeof Object.freeze == 'function') return; |
| 126 |
var a = {}; |
| 127 |
assertEquals(a, goog.debug.freeze(a)); |
| 128 |
a.foo = 42; |
| 129 |
assertEquals(42, a.foo); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
function testFreeze_debug() { |
| 134 |
if (goog.DEBUG || typeof Object.freeze != 'function') return; |
| 135 |
var a = {}; |
| 136 |
assertEquals(a, goog.debug.freeze(a)); |
| 137 |
try { |
| 138 |
a.foo = 42; |
| 139 |
} catch (expectedInStrictMode) { |
| 140 |
} |
| 141 |
assertUndefined(a.foo); |
| 142 |
} |