| 1 |
// Copyright 2010 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.debug.entryPointRegistryTest'); |
| 16 |
goog.setTestOnly('goog.debug.entryPointRegistryTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug.ErrorHandler'); |
| 19 |
goog.require('goog.debug.entryPointRegistry'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var lastError; |
| 23 |
var errorHandler; |
| 24 |
var errorFn; |
| 25 |
|
| 26 |
function setUp() { |
| 27 |
lastError = null; |
| 28 |
errorFn = function(message) { throw {message: message}; }; |
| 29 |
errorHandler = new goog.debug.ErrorHandler(function(ex) { lastError = ex; }); |
| 30 |
goog.debug.entryPointRegistry.refList_ = []; |
| 31 |
} |
| 32 |
|
| 33 |
function testMonitorAndUnmonitor() { |
| 34 |
goog.debug.entryPointRegistry.register(function(transformer) { |
| 35 |
errorFn = transformer(errorFn); |
| 36 |
}); |
| 37 |
goog.debug.entryPointRegistry.monitorAll(errorHandler); |
| 38 |
|
| 39 |
var e = assertThrows('expected error', goog.partial(errorFn, 'Hello!')); |
| 40 |
assertEquals('Error in protected function: Hello!', e.message); |
| 41 |
assertEquals('Hello!', lastError.message); |
| 42 |
|
| 43 |
goog.debug.entryPointRegistry.unmonitorAllIfPossible(errorHandler); |
| 44 |
|
| 45 |
e = assertThrows('expected error', goog.partial(errorFn, 'Goodbye!')); |
| 46 |
assertEquals('Goodbye!', e.message); |
| 47 |
assertEquals('Hello!', lastError.message); |
| 48 |
} |
| 49 |
|
| 50 |
function testRegisterAfterMonitor() { |
| 51 |
goog.debug.entryPointRegistry.monitorAll(errorHandler); |
| 52 |
goog.debug.entryPointRegistry.register(function(transformer) { |
| 53 |
errorFn = transformer(errorFn); |
| 54 |
}); |
| 55 |
|
| 56 |
var e = assertThrows('expected error', goog.partial(errorFn, 'Hello!')); |
| 57 |
assertEquals('Error in protected function: Hello!', e.message); |
| 58 |
assertEquals('Hello!', lastError.message); |
| 59 |
|
| 60 |
goog.debug.entryPointRegistry.unmonitorAllIfPossible(errorHandler); |
| 61 |
|
| 62 |
e = assertThrows('expected error', goog.partial(errorFn, 'Goodbye!')); |
| 63 |
assertEquals('Goodbye!', e.message); |
| 64 |
assertEquals('Hello!', lastError.message); |
| 65 |
} |
| 66 |
|
| 67 |
function testInvalidUnmonitor() { |
| 68 |
goog.debug.entryPointRegistry.monitorAll(errorHandler); |
| 69 |
var e = assertThrows( |
| 70 |
'expected error', |
| 71 |
goog.partial( |
| 72 |
goog.debug.entryPointRegistry.unmonitorAllIfPossible, |
| 73 |
new goog.debug.ErrorHandler())); |
| 74 |
assertEquals( |
| 75 |
'Assertion failed: Only the most recent monitor can be unwrapped.', |
| 76 |
e.message); |
| 77 |
goog.debug.entryPointRegistry.unmonitorAllIfPossible(errorHandler); |
| 78 |
} |