Skip to content

Commit 244b2b7

Browse files
tuchidagbrail
authored andcommitted
Test to check the position of syntax errors.
1 parent 2f60a02 commit 244b2b7

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.javascript.tests;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import java.util.ArrayList;
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.mozilla.javascript.Context;
15+
import org.mozilla.javascript.ErrorReporter;
16+
import org.mozilla.javascript.EvaluatorException;
17+
import org.mozilla.javascript.ScriptableObject;
18+
19+
public class ErrorReporterTest {
20+
21+
private Context cx;
22+
private ScriptableObject scope;
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
cx = Context.enter();
27+
cx.setLanguageVersion(Context.VERSION_ES6);
28+
scope = cx.initStandardObjects();
29+
}
30+
31+
@After
32+
public void tearDown() {
33+
Context.exit();
34+
}
35+
36+
@Test
37+
public void errorInfo() {
38+
ErrorCollector errorCollector = new ErrorCollector();
39+
cx.setErrorReporter(errorCollector);
40+
41+
try {
42+
cx.evaluateString(scope, "0xGGGG", "", 1, null);
43+
} catch (EvaluatorException e) {
44+
}
45+
46+
assertTrue(errorCollector.reportedRuntimeError);
47+
assertEquals(1, errorCollector.errors.size());
48+
49+
ErrorInfo errorInfo = errorCollector.errors.get(0);
50+
assertEquals("number format error", errorInfo.message);
51+
assertEquals(1, errorInfo.line);
52+
assertEquals("0xGGGG", errorInfo.lineSource);
53+
}
54+
55+
@Test
56+
public void errorLine() {
57+
ErrorCollector errorCollector = new ErrorCollector();
58+
cx.setErrorReporter(errorCollector);
59+
60+
try {
61+
cx.evaluateString(
62+
scope,
63+
"var a = 'a';\nvar b = true;\nvar c = 0xGGGG;\n var d = 123;",
64+
"",
65+
1,
66+
null);
67+
} catch (EvaluatorException e) {
68+
}
69+
70+
assertTrue(errorCollector.reportedRuntimeError);
71+
assertEquals(2, errorCollector.errors.size());
72+
73+
ErrorInfo error1 = errorCollector.errors.get(0);
74+
assertEquals("number format error", error1.message);
75+
assertEquals(3, error1.line);
76+
assertEquals("var c = 0xGGGG;", error1.lineSource);
77+
78+
ErrorInfo error2 = errorCollector.errors.get(1);
79+
assertEquals("missing ; before statement", error2.message);
80+
assertEquals(3, error2.line);
81+
assertEquals("var c = 0xGGGG;", error2.lineSource);
82+
}
83+
84+
@Test
85+
public void catchParserExceptionInIRFactory() {
86+
ErrorCollector errorCollector = new ErrorCollector();
87+
cx.setErrorReporter(errorCollector);
88+
89+
try {
90+
// The test for errors in re-parse with IRFactory after parsing with Parser.
91+
cx.evaluateString(scope, "var a = 123;\n1=1", "", 1, null);
92+
} catch (EvaluatorException e) {
93+
}
94+
95+
assertTrue(errorCollector.reportedRuntimeError);
96+
assertEquals(1, errorCollector.errors.size());
97+
98+
ErrorInfo errorInfo = errorCollector.errors.get(0);
99+
assertEquals("Invalid assignment left-hand side.", errorInfo.message);
100+
assertEquals(2, errorInfo.line);
101+
assertEquals("1=1", errorInfo.lineSource);
102+
}
103+
104+
@Test
105+
public void errorLineInIRFactory() {
106+
ErrorCollector errorCollector = new ErrorCollector();
107+
cx.setErrorReporter(errorCollector);
108+
109+
cx.evaluateString(scope, "var a = 123;\n\nfor (let [x, x] in {}) {}", "", 1, null);
110+
111+
assertEquals(1, errorCollector.errors.size());
112+
113+
ErrorInfo errorInfo = errorCollector.errors.get(0);
114+
assertEquals("TypeError: redeclaration of variable x.", errorInfo.message);
115+
assertEquals(3, errorInfo.line);
116+
assertEquals("for (let [x, x] in {}) {}", errorInfo.lineSource);
117+
}
118+
119+
private static class ErrorInfo {
120+
String message;
121+
int line;
122+
String lineSource;
123+
124+
ErrorInfo(String message, int line, String lineSource) {
125+
this.message = message;
126+
this.line = line;
127+
this.lineSource = lineSource;
128+
}
129+
}
130+
131+
private static class ErrorCollector implements ErrorReporter {
132+
ArrayList<ErrorInfo> errors;
133+
ArrayList<ErrorInfo> warnings;
134+
boolean reportedRuntimeError = false;
135+
136+
ErrorCollector() {
137+
errors = new ArrayList<>();
138+
warnings = new ArrayList<>();
139+
}
140+
141+
@Override
142+
public void error(
143+
String message, String sourceName, int line, String lineSource, int lineOffset) {
144+
errors.add(new ErrorInfo(message, line, lineSource));
145+
}
146+
147+
@Override
148+
public void warning(
149+
String message, String sourceName, int line, String lineSource, int lineOffset) {
150+
warnings.add(new ErrorInfo(message, line, lineSource));
151+
}
152+
153+
@Override
154+
public EvaluatorException runtimeError(
155+
String message, String sourceName, int line, String lineSource, int lineOffset) {
156+
reportedRuntimeError = true;
157+
return new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)