entrypointregistry_test.js 2.8 KB

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