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.ast.AstRoot;
20 
21 /**
22  * @author André Bargull
23  *
24  */
25 public class Bug689314Test {
26     private Context cx;
27 
28     @Before
setUp()29     public void setUp() {
30         cx = Context.enter();
31         cx.setLanguageVersion(Context.VERSION_1_8);
32     }
33 
34     @After
tearDown()35     public void tearDown() {
36         Context.exit();
37     }
38 
parse(CharSequence cs)39     private AstRoot parse(CharSequence cs) {
40         CompilerEnvirons compilerEnv = new CompilerEnvirons();
41         compilerEnv.initFromContext(cx);
42         ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
43         Parser p = new Parser(compilerEnv, compilationErrorReporter);
44         return p.parse(cs.toString(), "<eval>", 1);
45     }
46 
toSource(CharSequence cs)47     private String toSource(CharSequence cs) {
48         return parse(cs).toSource();
49     }
50 
51     @Test
testToSourceFunctionStatement()52     public void testToSourceFunctionStatement() {
53         assertEquals("function F() 1 + 2;\n", toSource("function F() 1+2"));
54         assertEquals("function F() {\n  return 1 + 2;\n}\n",
55                 toSource("function F() {return 1+2}"));
56     }
57 
58     @Test
testToSourceFunctionExpression()59     public void testToSourceFunctionExpression() {
60         assertEquals("var x = function() 1 + 2;\n",
61                 toSource("var x = function () 1+2"));
62         assertEquals("var x = function() {\n  return 1 + 2;\n};\n",
63                 toSource("var x = function () {return 1+2}"));
64         assertEquals("var x = function F() 1 + 2;\n",
65                 toSource("var x = function F() 1+2"));
66         assertEquals("var x = function F() {\n  return 1 + 2;\n};\n",
67                 toSource("var x = function F() {return 1+2}"));
68     }
69 }
70