/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.javascript.tests; import org.junit.Assert; import org.junit.Test; import org.mozilla.javascript.CompilerEnvirons; import org.mozilla.javascript.Context; import org.mozilla.javascript.Parser; import org.mozilla.javascript.ast.AstRoot; /** * @author Hannes Wallnoefer */ public class Bug491621Test { /** * Asserts that the value returned by {@link AstRoot#toSource()} after * the given input source was parsed equals the specified expected output source. * * @param source the JavaScript source to be parsed * @param expectedOutput the JavaScript source that is expected to be * returned by {@link AstRoot#toSource()} */ private void assertSource(String source, String expectedOutput) { CompilerEnvirons env = new CompilerEnvirons(); env.setLanguageVersion(Context.VERSION_1_7); Parser parser = new Parser(env); AstRoot root = parser.parse(source, null, 0); Assert.assertEquals(expectedOutput, root.toSource()); } /** * Tests that var declaration AST nodes is properly decompiled. */ @Test public void testVarDeclarationToSource() { assertSource("var x=0;x++;", "var x = 0;\nx++;\n"); assertSource("for(var i=0;i<10;i++)x[i]=i;a++;", "for (var i = 0; i < 10; i++) \n x[i] = i;\na++;\n"); assertSource("var a;if(true)a=1;", "var a;\nif (true) \n a = 1;\n"); assertSource("switch(x){case 1:var y;z++}", "switch (x) {\n case 1:\n var y;\n z++;\n}\n"); assertSource("for(var p in o)s+=o[p]", "for (var p in o) \n s += o[p];\n"); assertSource("if(c)var a=0;else a=1", "if (c) \n var a = 0;\nelse \n a = 1;\n"); assertSource("for(var i=0;i<10;i++)var x=i;x++;", "for (var i = 0; i < 10; i++) \n var x = i;\nx++;\n"); assertSource("function f(){var i=2;for(var j=0;j