|
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 * Date: 2001-07-10 |
|
8 * |
|
9 * SUMMARY: Invoking try...catch through Function.call |
|
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=49286 |
|
11 * |
|
12 * 1) Define a function with a try...catch block in it |
|
13 * 2) Invoke the function via the call method of Function |
|
14 * 3) Pass bad syntax to the try...catch block |
|
15 * 4) We should catch the error! |
|
16 */ |
|
17 //----------------------------------------------------------------------------- |
|
18 var UBound = 0; |
|
19 var BUGNUMBER = 49286; |
|
20 var summary = 'Invoking try...catch through Function.call'; |
|
21 var cnErrorCaught = 'Error caught'; |
|
22 var cnErrorNotCaught = 'Error NOT caught'; |
|
23 var cnGoodSyntax = '1==2'; |
|
24 var cnBadSyntax = '1=2'; |
|
25 var status = ''; |
|
26 var statusitems = []; |
|
27 var actual = ''; |
|
28 var actualvalues = []; |
|
29 var expect= ''; |
|
30 var expectedvalues = []; |
|
31 |
|
32 |
|
33 var obj = new testObject(); |
|
34 |
|
35 status = 'Section A of test: direct call of f'; |
|
36 actual = f.call(obj); |
|
37 expect = cnErrorCaught; |
|
38 addThis(); |
|
39 |
|
40 status = 'Section B of test: indirect call of f'; |
|
41 actual = g.call(obj); |
|
42 expect = cnErrorCaught; |
|
43 addThis(); |
|
44 |
|
45 |
|
46 |
|
47 //----------------------------------------- |
|
48 test(); |
|
49 //----------------------------------------- |
|
50 |
|
51 |
|
52 function test() |
|
53 { |
|
54 enterFunc ('test'); |
|
55 printBugNumber(BUGNUMBER); |
|
56 printStatus (summary); |
|
57 |
|
58 for (var i=0; i<UBound; i++) |
|
59 { |
|
60 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
61 } |
|
62 |
|
63 exitFunc ('test'); |
|
64 } |
|
65 |
|
66 |
|
67 // An object storing bad syntax as a property - |
|
68 function testObject() |
|
69 { |
|
70 this.badSyntax = cnBadSyntax; |
|
71 this.goodSyntax = cnGoodSyntax; |
|
72 } |
|
73 |
|
74 |
|
75 // A function wrapping a try...catch block |
|
76 function f() |
|
77 { |
|
78 try |
|
79 { |
|
80 eval(this.badSyntax); |
|
81 } |
|
82 catch(e) |
|
83 { |
|
84 return cnErrorCaught; |
|
85 } |
|
86 return cnErrorNotCaught; |
|
87 } |
|
88 |
|
89 |
|
90 // A function wrapping a call to f - |
|
91 function g() |
|
92 { |
|
93 return f.call(this); |
|
94 } |
|
95 |
|
96 |
|
97 function addThis() |
|
98 { |
|
99 statusitems[UBound] = status; |
|
100 actualvalues[UBound] = actual; |
|
101 expectedvalues[UBound] = expect; |
|
102 UBound++; |
|
103 } |