Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/Bug687669Test.java

raw symbols SCIP
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 /**
6 *
7 */
8 package org.mozilla.javascript.tests;
9
10 import static org.junit.Assert.assertEquals;
11
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mozilla.javascript.CompilerEnvirons;
16 import org.mozilla.javascript.Context;
17 import org.mozilla.javascript.ErrorReporter;
18 import org.mozilla.javascript.Parser;
19 import org.mozilla.javascript.ScriptableObject;
20 import org.mozilla.javascript.Undefined;
21 import org.mozilla.javascript.ast.AstRoot;
22
23 /**
24 * @author André Bargull
25 *
26 */
27 public class Bug687669Test {
28 private Context cx;
29 private ScriptableObject scope;
30
31 @Before
32 public void setUp() {
33 cx = Context.enter();
34 cx.setLanguageVersion(Context.VERSION_1_8);
35 scope = cx.initStandardObjects();
36 }
37
38 @After
39 public void tearDown() {
40 Context.exit();
41 }
42
43 private Object eval(CharSequence cs) {
44 return cx.evaluateString(scope, cs.toString(), "<eval>", 1, null);
45 }
46
47 private AstRoot parse(CharSequence cs) {
48 CompilerEnvirons compilerEnv = new CompilerEnvirons();
49 compilerEnv.initFromContext(cx);
50 ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
51 Parser p = new Parser(compilerEnv, compilationErrorReporter);
52 return p.parse(cs.toString(), "<eval>", 1);
53 }
54
55 private String toSource(CharSequence cs) {
56 return parse(cs).toSource();
57 }
58
59 @Test
60 public void testEval() {
61 // test EmptyStatement node doesn't infer with return values (in
62 // contrast to wrapping EmptyExpression into an ExpressionStatement)
63 assertEquals(1d, eval("1;;;;"));
64 assertEquals(Undefined.instance, eval("(function(){1;;;;})()"));
65 assertEquals(1d, eval("(function(){return 1;;;;})()"));
66 }
67
68 @Test
69 public void testToSource() {
70 assertEquals("L1:\n ;\n", toSource("L1:;"));
71 assertEquals("L1:\n ;\na = 1;\n", toSource("L1:; a=1;"));
72
73 assertEquals("if (1) \n ;\n", toSource("if(1);"));
74 assertEquals("if (1) \n ;\na = 1;\n", toSource("if(1); a=1;"));
75
76 assertEquals("if (1) \n a = 1;\n", toSource("if(1)a=1;"));
77 assertEquals("if (1) \n a = 1;\na = 1;\n", toSource("if(1)a=1; a=1;"));
78
79 assertEquals("if (1) \n ;\n;\n;\n;\n", toSource("if(1);;;;"));
80 }
81
82 }