Skip to content

Commit f9ab208

Browse files
committed
Tests.
1 parent c241d91 commit f9ab208

10 files changed

Lines changed: 2079 additions & 2 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/interpreterv2/Compiler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,15 +1740,14 @@ private void addLineNumber(Node node, List<Integer> lines) {
17401740

17411741
private void updateLineNumber(Node node) {
17421742
int lineno = node.getLineno();
1743-
if (lineno < 0) return;
17441743
SourceMapper mapper = compilerEnv.getSourceMapper();
17451744
if (mapper != null) {
17461745
Position mapped = mapper.mapPosition(lineno, node.getColumn());
17471746
if (mapped == null) return;
17481747
lineno = mapped.getLine();
17491748
}
17501749

1751-
// Token.printColumns and SourceMapper not in open-source
1750+
if (lineno < 0) return;
17521751

17531752
updateLineNumber(lineno);
17541753
}
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
package org.mozilla.javascript.interpreterv2;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.mozilla.javascript.testutils.Utils;
5+
6+
/**
7+
* Test suite for conditional catch blocks functionality in InterpreterV2. Tests the Mozilla/Rhino
8+
* extension: catch (e if condition) { ... }
9+
*/
10+
class ConditionalCatchTest {
11+
12+
@Test
13+
void testBasicConditionalCatch() {
14+
String script =
15+
"var result = 'none';\n"
16+
+ "try {\n"
17+
+ " throw new TypeError('test error');\n"
18+
+ "} catch (e if e instanceof TypeError) {\n"
19+
+ " result = 'caught TypeError';\n"
20+
+ "} catch (e) {\n"
21+
+ " result = 'caught other';\n"
22+
+ "}\n"
23+
+ "result;";
24+
25+
Utils.assertWithAllModes_ES6("caught TypeError", script);
26+
}
27+
28+
@Test
29+
void testMultipleConditionalCatches() {
30+
String script =
31+
"var result = 'none';\n"
32+
+ "try {\n"
33+
+ " throw new ReferenceError('reference error');\n"
34+
+ "} catch (e if e instanceof TypeError) {\n"
35+
+ " result = 'caught TypeError';\n"
36+
+ "} catch (e if e instanceof ReferenceError) {\n"
37+
+ " result = 'caught ReferenceError';\n"
38+
+ "} catch (e) {\n"
39+
+ " result = 'caught other';\n"
40+
+ "}\n"
41+
+ "result;";
42+
43+
Utils.assertWithAllModes_ES6("caught ReferenceError", script);
44+
}
45+
46+
@Test
47+
void testComplexCondition() {
48+
String script =
49+
"var result = 'none';\n"
50+
+ "try {\n"
51+
+ " throw new TypeError('special error');\n"
52+
+ "} catch (e if e instanceof TypeError && e.message.indexOf('special') >= 0) {\n"
53+
+ " result = 'caught special TypeError';\n"
54+
+ "} catch (e if e instanceof TypeError) {\n"
55+
+ " result = 'caught regular TypeError';\n"
56+
+ "} catch (e) {\n"
57+
+ " result = 'caught other';\n"
58+
+ "}\n"
59+
+ "result;";
60+
61+
Utils.assertWithAllModes_ES6("caught special TypeError", script);
62+
}
63+
64+
@Test
65+
void testFallthroughToDefault() {
66+
String script =
67+
"var result = 'none';\n"
68+
+ "try {\n"
69+
+ " throw new Error('generic error');\n"
70+
+ "} catch (e if e instanceof TypeError) {\n"
71+
+ " result = 'caught TypeError';\n"
72+
+ "} catch (e if e instanceof ReferenceError) {\n"
73+
+ " result = 'caught ReferenceError';\n"
74+
+ "} catch (e) {\n"
75+
+ " result = 'caught default';\n"
76+
+ "}\n"
77+
+ "result;";
78+
79+
Utils.assertWithAllModes_ES6("caught default", script);
80+
}
81+
82+
@Test
83+
void testVariableBinding() {
84+
String script =
85+
"var result = 'none';\n"
86+
+ "var outerVar = 'outer';\n"
87+
+ "try {\n"
88+
+ " throw new TypeError('test');\n"
89+
+ "} catch (e if outerVar === 'outer' && e instanceof TypeError) {\n"
90+
+ " result = 'caught with outer variable: ' + outerVar;\n"
91+
+ "} catch (e) {\n"
92+
+ " result = 'caught other';\n"
93+
+ "}\n"
94+
+ "result;";
95+
96+
Utils.assertWithAllModes_ES6("caught with outer variable: outer", script);
97+
}
98+
99+
@Test
100+
void testScopeIsolation() {
101+
String script =
102+
"var e = 'outer';\n"
103+
+ "var result = 'none';\n"
104+
+ "try {\n"
105+
+ " throw new TypeError('test');\n"
106+
+ "} catch (e if e instanceof TypeError) {\n"
107+
+ " result = 'inner e is TypeError: ' + (e instanceof TypeError);\n"
108+
+ "}\n"
109+
+ "result + ', outer e: ' + e;";
110+
111+
Utils.assertWithAllModes_ES6("inner e is TypeError: true, outer e: outer", script);
112+
}
113+
114+
@Test
115+
void testNestedConditionalCatch() {
116+
String script =
117+
"var result = 'none';\n"
118+
+ "try {\n"
119+
+ " try {\n"
120+
+ " throw new TypeError('inner error');\n"
121+
+ " } catch (e if e instanceof ReferenceError) {\n"
122+
+ " result = 'caught inner ReferenceError';\n"
123+
+ " } catch (e if e instanceof TypeError) {\n"
124+
+ " result = 'caught inner TypeError';\n"
125+
+ " throw new ReferenceError('outer error');\n"
126+
+ " }\n"
127+
+ "} catch (e if e instanceof ReferenceError) {\n"
128+
+ " result = 'caught outer ReferenceError';\n"
129+
+ "} catch (e) {\n"
130+
+ " result = 'caught outer other';\n"
131+
+ "}\n"
132+
+ "result;";
133+
134+
Utils.assertWithAllModes_ES6("caught outer ReferenceError", script);
135+
}
136+
137+
@Test
138+
void testConditionalCatchWithFinally() {
139+
String script =
140+
"var result = 'none';\n"
141+
+ "var finallyExecuted = false;\n"
142+
+ "try {\n"
143+
+ " throw new TypeError('test');\n"
144+
+ "} catch (e if e instanceof TypeError) {\n"
145+
+ " result = 'caught TypeError';\n"
146+
+ "} catch (e) {\n"
147+
+ " result = 'caught other';\n"
148+
+ "} finally {\n"
149+
+ " finallyExecuted = true;\n"
150+
+ "}\n"
151+
+ "result + ',' + finallyExecuted;";
152+
153+
Utils.assertWithAllModes_ES6("caught TypeError,true", script);
154+
}
155+
156+
@Test
157+
void testStringException() {
158+
String script =
159+
"var result = 'none';\n"
160+
+ "try {\n"
161+
+ " throw 'string exception';\n"
162+
+ "} catch (e if typeof e === 'string') {\n"
163+
+ " result = 'caught string: ' + e;\n"
164+
+ "} catch (e) {\n"
165+
+ " result = 'caught other';\n"
166+
+ "}\n"
167+
+ "result;";
168+
169+
Utils.assertWithAllModes_ES6("caught string: string exception", script);
170+
}
171+
172+
@Test
173+
void testNumberException() {
174+
String script =
175+
"var result = 'none';\n"
176+
+ "try {\n"
177+
+ " throw 42;\n"
178+
+ "} catch (e if typeof e === 'number') {\n"
179+
+ " result = 'caught number: ' + e;\n"
180+
+ "} catch (e) {\n"
181+
+ " result = 'caught other';\n"
182+
+ "}\n"
183+
+ "result;";
184+
185+
Utils.assertWithAllModes_ES6("caught number: 42", script);
186+
}
187+
188+
@Test
189+
void testFunctionCallInCondition() {
190+
String script =
191+
"function isSpecialError(e) {\n"
192+
+ " return e instanceof TypeError && e.message === 'special';\n"
193+
+ "}\n"
194+
+ "\n"
195+
+ "var result = 'none';\n"
196+
+ "try {\n"
197+
+ " throw new TypeError('special');\n"
198+
+ "} catch (e if isSpecialError(e)) {\n"
199+
+ " result = 'caught special error';\n"
200+
+ "} catch (e) {\n"
201+
+ " result = 'caught other';\n"
202+
+ "}\n"
203+
+ "result;";
204+
205+
Utils.assertWithAllModes_ES6("caught special error", script);
206+
}
207+
208+
@Test
209+
void testConditionEvaluationOrder() {
210+
String script =
211+
"var evaluationOrder = [];\n"
212+
+ "\n"
213+
+ "function condition1(e) {\n"
214+
+ " evaluationOrder.push('condition1');\n"
215+
+ " return false;\n"
216+
+ "}\n"
217+
+ "\n"
218+
+ "function condition2(e) {\n"
219+
+ " evaluationOrder.push('condition2');\n"
220+
+ " return true;\n"
221+
+ "}\n"
222+
+ "\n"
223+
+ "try {\n"
224+
+ " throw new Error('test');\n"
225+
+ "} catch (e if condition1(e)) {\n"
226+
+ " // Should not reach here\n"
227+
+ "} catch (e if condition2(e)) {\n"
228+
+ " // Should reach here\n"
229+
+ "} catch (e) {\n"
230+
+ " // Should not reach here\n"
231+
+ "}\n"
232+
+ "\n"
233+
+ "evaluationOrder.join(',');";
234+
235+
Utils.assertWithAllModes_ES6("condition1,condition2", script);
236+
}
237+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.mozilla.javascript.interpreterv2;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.mozilla.javascript.testutils.Utils;
5+
6+
/**
7+
* Regression test for conditional string add operations in InterpreterV2. Tests the fix for
8+
* conditional string concatenation that previously broke.
9+
*/
10+
class ConditionalStringAddTest {
11+
12+
@Test
13+
void testConditionalStringAdd() {
14+
String script =
15+
"var args = \"\";\n"
16+
+ "var i = 2;\n"
17+
+ "args += ( i == 2 ) ? i : i + ', ';\n"
18+
+ "args;";
19+
20+
Utils.assertWithAllModes_ES6("2", script);
21+
}
22+
23+
@Test
24+
void testConditionalStringAddWithElse() {
25+
String script =
26+
"var args = \"\";\n"
27+
+ "var i = 3;\n"
28+
+ "args += ( i == 2 ) ? i : i + ', ';\n"
29+
+ "args;";
30+
31+
Utils.assertWithAllModes_ES6("3, ", script);
32+
}
33+
34+
@Test
35+
void testConditionalStringAddChained() {
36+
String script =
37+
"var args = \"start\";\n"
38+
+ "for (var i = 1; i <= 3; i++) {\n"
39+
+ " args += ( i == 1 ) ? i : ', ' + i;\n"
40+
+ "}\n"
41+
+ "args;";
42+
43+
Utils.assertWithAllModes_ES6("start1, 2, 3", script);
44+
}
45+
46+
@Test
47+
void testConditionalStringAddWithComplexExpression() {
48+
String script =
49+
"var result = \"\";\n"
50+
+ "var x = 5;\n"
51+
+ "result += (x > 3) ? \"big: \" + x : \"small: \" + x;\n"
52+
+ "result;";
53+
54+
Utils.assertWithAllModes_ES6("big: 5", script);
55+
}
56+
}

0 commit comments

Comments
 (0)