|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * |
|
8 * Date: 22 Jan 2002 |
|
9 * SUMMARY: Testing Error.prototype.toString() |
|
10 * |
|
11 * Revised: 25 Nov 2002 |
|
12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=181909 |
|
13 * |
|
14 * Note that ECMA-262 3rd Edition Final, Section 15.11.4.4 states that |
|
15 * Error.prototype.toString() returns an implementation-dependent string. |
|
16 * Therefore any testcase on this property is somewhat arbitrary. |
|
17 * |
|
18 * However, d-russo@ti.com pointed out that Rhino was returning this: |
|
19 * |
|
20 * js> err = new Error() |
|
21 * undefined: undefined |
|
22 * |
|
23 * js> err = new Error("msg") |
|
24 * undefined: msg |
|
25 * |
|
26 * |
|
27 * We expect Rhino to return what SpiderMonkey currently does: |
|
28 * |
|
29 * js> err = new Error() |
|
30 * Error |
|
31 * |
|
32 * js> err = new Error("msg") |
|
33 * Error: msg |
|
34 * |
|
35 * |
|
36 * i.e. we expect err.toString() === err.name if err.message is not defined; |
|
37 * otherwise, we expect err.toString() === err.name + ': ' + err.message. |
|
38 * |
|
39 * See also ECMA 15.11.4.2, 15.11.4.3 |
|
40 */ |
|
41 //----------------------------------------------------------------------------- |
|
42 var UBound = 0; |
|
43 var BUGNUMBER = '(none)'; |
|
44 var summary = 'Testing Error.prototype.toString()'; |
|
45 var status = ''; |
|
46 var statusitems = []; |
|
47 var actual = ''; |
|
48 var actualvalues = []; |
|
49 var expect= ''; |
|
50 var expectedvalues = []; |
|
51 var EMPTY_STRING = ''; |
|
52 var EXPECTED_FORMAT = 0; |
|
53 |
|
54 |
|
55 status = inSection(1); |
|
56 var err1 = new Error('msg1'); |
|
57 actual = examineThis(err1, 'msg1'); |
|
58 expect = EXPECTED_FORMAT; |
|
59 addThis(); |
|
60 |
|
61 status = inSection(2); |
|
62 var err2 = new Error(err1); |
|
63 actual = examineThis(err2, err1); |
|
64 expect = EXPECTED_FORMAT; |
|
65 addThis(); |
|
66 |
|
67 status = inSection(3); |
|
68 var err3 = new Error(); |
|
69 actual = examineThis(err3, EMPTY_STRING); |
|
70 expect = EXPECTED_FORMAT; |
|
71 addThis(); |
|
72 |
|
73 status = inSection(4); |
|
74 var err4 = new Error(EMPTY_STRING); |
|
75 actual = examineThis(err4, EMPTY_STRING); |
|
76 expect = EXPECTED_FORMAT; |
|
77 addThis(); |
|
78 |
|
79 // now generate a run-time error - |
|
80 status = inSection(5); |
|
81 try |
|
82 { |
|
83 eval('1=2'); |
|
84 } |
|
85 catch(err5) |
|
86 { |
|
87 actual = examineThis(err5, '.*'); |
|
88 } |
|
89 expect = EXPECTED_FORMAT; |
|
90 addThis(); |
|
91 |
|
92 |
|
93 |
|
94 //----------------------------------------------------------------------------- |
|
95 test(); |
|
96 //----------------------------------------------------------------------------- |
|
97 |
|
98 |
|
99 |
|
100 /* |
|
101 * Searches err.toString() for err.name + ':' + err.message, |
|
102 * with possible whitespace on each side of the colon sign. |
|
103 * |
|
104 * We allow for no colon in case err.message was not provided by the user. |
|
105 * In such a case, SpiderMonkey and Rhino currently set err.message = '', |
|
106 * as allowed for by ECMA 15.11.4.3. This makes |pattern| work in this case. |
|
107 * |
|
108 * If this is ever changed to a non-empty string, e.g. 'undefined', |
|
109 * you may have to modify |pattern| to take that into account - |
|
110 * |
|
111 */ |
|
112 function examineThis(err, msg) |
|
113 { |
|
114 var pattern = err.name + '\\s*:?\\s*' + msg; |
|
115 return err.toString().search(RegExp(pattern)); |
|
116 } |
|
117 |
|
118 |
|
119 function addThis() |
|
120 { |
|
121 statusitems[UBound] = status; |
|
122 actualvalues[UBound] = actual; |
|
123 expectedvalues[UBound] = expect; |
|
124 UBound++; |
|
125 } |
|
126 |
|
127 |
|
128 function test() |
|
129 { |
|
130 enterFunc ('test'); |
|
131 printBugNumber(BUGNUMBER); |
|
132 printStatus (summary); |
|
133 |
|
134 for (var i = 0; i < UBound; i++) |
|
135 { |
|
136 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
137 } |
|
138 |
|
139 exitFunc ('test'); |
|
140 } |