|
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-11 |
|
8 * |
|
9 * SUMMARY: Testing eval scope inside a function. |
|
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=77578 |
|
11 */ |
|
12 //----------------------------------------------------------------------------- |
|
13 var UBound = 0; |
|
14 var BUGNUMBER = 77578; |
|
15 var summary = 'Testing eval scope inside a function'; |
|
16 var cnEquals = '='; |
|
17 var status = ''; |
|
18 var statusitems = []; |
|
19 var actual = ''; |
|
20 var actualvalues = []; |
|
21 var expect= ''; |
|
22 var expectedvalues = []; |
|
23 |
|
24 |
|
25 // various versions of JavaScript - |
|
26 var JS_VER = [100, 110, 120, 130, 140, 150]; |
|
27 |
|
28 // Note contrast with local variables i,j,k defined below - |
|
29 var i = 999; |
|
30 var j = 999; |
|
31 var k = 999; |
|
32 |
|
33 |
|
34 //-------------------------------------------------- |
|
35 test(); |
|
36 //-------------------------------------------------- |
|
37 |
|
38 |
|
39 function test() |
|
40 { |
|
41 enterFunc ('test'); |
|
42 printBugNumber(BUGNUMBER); |
|
43 printStatus (summary); |
|
44 |
|
45 // Run tests A,B,C on each version of JS and store results |
|
46 for (var n=0; n!=JS_VER.length; n++) |
|
47 { |
|
48 testA(JS_VER[n]); |
|
49 } |
|
50 for (var n=0; n!=JS_VER.length; n++) |
|
51 { |
|
52 testB(JS_VER[n]); |
|
53 } |
|
54 for (var n=0; n!=JS_VER.length; n++) |
|
55 { |
|
56 testC(JS_VER[n]); |
|
57 } |
|
58 |
|
59 |
|
60 // Compare actual values to expected values - |
|
61 for (var i=0; i<UBound; i++) |
|
62 { |
|
63 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
64 } |
|
65 |
|
66 exitFunc ('test'); |
|
67 } |
|
68 |
|
69 |
|
70 function testA(ver) |
|
71 { |
|
72 // Set the version of JS to test - |
|
73 if (typeof version == 'function') |
|
74 { |
|
75 version(ver); |
|
76 } |
|
77 |
|
78 // eval the test, so it compiles AFTER version() has executed - |
|
79 var sTestScript = ""; |
|
80 |
|
81 // Define a local variable i |
|
82 sTestScript += "status = 'Section A of test; JS ' + ver/100;"; |
|
83 sTestScript += "var i=1;"; |
|
84 sTestScript += "actual = eval('i');"; |
|
85 sTestScript += "expect = 1;"; |
|
86 sTestScript += "captureThis('i');"; |
|
87 |
|
88 eval(sTestScript); |
|
89 } |
|
90 |
|
91 |
|
92 function testB(ver) |
|
93 { |
|
94 // Set the version of JS to test - |
|
95 if (typeof version == 'function') |
|
96 { |
|
97 version(ver); |
|
98 } |
|
99 |
|
100 // eval the test, so it compiles AFTER version() has executed - |
|
101 var sTestScript = ""; |
|
102 |
|
103 // Define a local for-loop iterator j |
|
104 sTestScript += "status = 'Section B of test; JS ' + ver/100;"; |
|
105 sTestScript += "for(var j=1; j<2; j++)"; |
|
106 sTestScript += "{"; |
|
107 sTestScript += " actual = eval('j');"; |
|
108 sTestScript += "};"; |
|
109 sTestScript += "expect = 1;"; |
|
110 sTestScript += "captureThis('j');"; |
|
111 |
|
112 eval(sTestScript); |
|
113 } |
|
114 |
|
115 |
|
116 function testC(ver) |
|
117 { |
|
118 // Set the version of JS to test - |
|
119 if (typeof version == 'function') |
|
120 { |
|
121 version(ver); |
|
122 } |
|
123 |
|
124 // eval the test, so it compiles AFTER version() has executed - |
|
125 var sTestScript = ""; |
|
126 |
|
127 // Define a local variable k in a try-catch block - |
|
128 sTestScript += "status = 'Section C of test; JS ' + ver/100;"; |
|
129 sTestScript += "try"; |
|
130 sTestScript += "{"; |
|
131 sTestScript += " var k=1;"; |
|
132 sTestScript += " actual = eval('k');"; |
|
133 sTestScript += "}"; |
|
134 sTestScript += "catch(e)"; |
|
135 sTestScript += "{"; |
|
136 sTestScript += "};"; |
|
137 sTestScript += "expect = 1;"; |
|
138 sTestScript += "captureThis('k');"; |
|
139 |
|
140 eval(sTestScript); |
|
141 } |
|
142 |
|
143 |
|
144 function captureThis(varName) |
|
145 { |
|
146 statusitems[UBound] = status; |
|
147 actualvalues[UBound] = varName + cnEquals + actual; |
|
148 expectedvalues[UBound] = varName + cnEquals + expect; |
|
149 UBound++; |
|
150 } |