error_test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2009 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.ErrorTest');
  15. goog.setTestOnly('goog.debug.ErrorTest');
  16. goog.require('goog.debug.Error');
  17. goog.require('goog.testing.ExpectedFailures');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. goog.require('goog.userAgent.product');
  21. var expectedFailures;
  22. function setUpPage() {
  23. expectedFailures = new goog.testing.ExpectedFailures();
  24. }
  25. function tearDown() {
  26. expectedFailures.handleTearDown();
  27. }
  28. function testError() {
  29. function xxxxx() { yyyyy(); }
  30. function yyyyy() { zzzzz(); }
  31. function zzzzz() { throw new goog.debug.Error('testing'); }
  32. var stack = null, message = null;
  33. try {
  34. xxxxx();
  35. } catch (e) {
  36. message = e.message;
  37. if (e.stack) {
  38. stack = e.stack.split('\n');
  39. }
  40. }
  41. assertEquals('Message property should be set', 'testing', message);
  42. expectedFailures.expectFailureFor(
  43. (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10')) ||
  44. goog.userAgent.product.SAFARI ||
  45. (goog.userAgent.product.CHROME &&
  46. !goog.userAgent.isVersionOrHigher(532)),
  47. 'error.stack is not widely supported');
  48. try {
  49. assertNotNull(stack);
  50. if (goog.userAgent.product.FIREFOX &&
  51. goog.userAgent.isVersionOrHigher('2.0')) {
  52. // Firefox 4 and greater does not have the first line that says
  53. // 'Error'. So we insert a dummy line to simplify the test.
  54. stack.splice(0, 0, 'Error');
  55. }
  56. // If the stack trace came from a synthetic Error object created
  57. // inside the goog.debug.Error constructor, it will have an extra frame
  58. // at stack[1]. If it came from captureStackTrace or was attached
  59. // by IE when the error was caught, it will not.
  60. if (!Error.captureStackTrace && !goog.userAgent.IE) {
  61. stack.splice(1, 1); // Remove stack[1].
  62. }
  63. assertContains('1st line of stack should have "Error"', 'Error', stack[0]);
  64. assertContains('2nd line of stack should have "zzzzz"', 'zzzzz', stack[1]);
  65. assertContains('3rd line of stack should have "yyyyy"', 'yyyyy', stack[2]);
  66. assertContains('4th line of stack should have "xxxxx"', 'xxxxx', stack[3]);
  67. } catch (e) {
  68. expectedFailures.handleException(e);
  69. }
  70. }
  71. function testInheriting() {
  72. function MyError() { goog.debug.Error.call(this); }
  73. goog.inherits(MyError, goog.debug.Error);
  74. MyError.prototype.message = 'My custom error';
  75. var message = null;
  76. try {
  77. throw new MyError();
  78. } catch (e) {
  79. message = e.message;
  80. }
  81. assertEquals('My custom error', message);
  82. }