Debian Codegraph @

closure-library/closure/goog/debug/error_test.js

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