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 Bug689308Test { 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 testToSourceArray()52 public void testToSourceArray() { 53 assertEquals("[];\n", toSource("[]")); 54 assertEquals("[,];\n", toSource("[,]")); 55 assertEquals("[, ,];\n", toSource("[,,]")); 56 assertEquals("[, , ,];\n", toSource("[,,,]")); 57 58 assertEquals("[1];\n", toSource("[1]")); 59 assertEquals("[1];\n", toSource("[1,]")); 60 assertEquals("[, 1];\n", toSource("[,1]")); 61 assertEquals("[1, 1];\n", toSource("[1,1]")); 62 } 63 } 64