| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import org.junit.Assert; |
| 8 |
import org.junit.Test; |
| 9 |
import org.mozilla.javascript.CompilerEnvirons; |
| 10 |
import org.mozilla.javascript.Context; |
| 11 |
import org.mozilla.javascript.Parser; |
| 12 |
import org.mozilla.javascript.ast.AstRoot; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class Bug491621Test { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private void assertSource(String source, String expectedOutput) |
| 28 |
{ |
| 29 |
CompilerEnvirons env = new CompilerEnvirons(); |
| 30 |
env.setLanguageVersion(Context.VERSION_1_7); |
| 31 |
Parser parser = new Parser(env); |
| 32 |
AstRoot root = parser.parse(source, null, 0); |
| 33 |
Assert.assertEquals(expectedOutput, root.toSource()); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
@Test |
| 40 |
public void testVarDeclarationToSource() |
| 41 |
{ |
| 42 |
assertSource("var x=0;x++;", |
| 43 |
"var x = 0;\nx++;\n"); |
| 44 |
assertSource("for(var i=0;i<10;i++)x[i]=i;a++;", |
| 45 |
"for (var i = 0; i < 10; i++) \n x[i] = i;\na++;\n"); |
| 46 |
assertSource("var a;if(true)a=1;", |
| 47 |
"var a;\nif (true) \n a = 1;\n"); |
| 48 |
assertSource("switch(x){case 1:var y;z++}", |
| 49 |
"switch (x) {\n case 1:\n var y;\n z++;\n}\n"); |
| 50 |
assertSource("for(var p in o)s+=o[p]", |
| 51 |
"for (var p in o) \n s += o[p];\n"); |
| 52 |
assertSource("if(c)var a=0;else a=1", |
| 53 |
"if (c) \n var a = 0;\nelse \n a = 1;\n"); |
| 54 |
assertSource("for(var i=0;i<10;i++)var x=i;x++;", |
| 55 |
"for (var i = 0; i < 10; i++) \n var x = i;\nx++;\n"); |
| 56 |
assertSource("function f(){var i=2;for(var j=0;j<i;++j)print(j);}", |
| 57 |
"function f() {\n var i = 2;\n for (var j = 0; j < i; ++j) \n print(j);\n}\n"); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@Test |
| 64 |
public void testLetDeclarationToSource() |
| 65 |
{ |
| 66 |
assertSource("let x=0;x++;", |
| 67 |
"let x = 0;\nx++;\n"); |
| 68 |
assertSource("for(let i=0;i<10;i++)x[i]=i;a++;", |
| 69 |
"for (let i = 0; i < 10; i++) \n x[i] = i;\na++;\n"); |
| 70 |
assertSource("let a;if(true)a=1;", |
| 71 |
"let a;\nif (true) \n a = 1;\n"); |
| 72 |
assertSource("switch(x){case 1:let y;z++}", |
| 73 |
"switch (x) {\n case 1:\n let y;\n z++;\n}\n"); |
| 74 |
assertSource("for(let p in o)s+=o[p]", |
| 75 |
"for (let p in o) \n s += o[p];\n"); |
| 76 |
assertSource("if(c)let a=0;else a=1", |
| 77 |
"if (c) \n let a = 0;\nelse \n a = 1;\n"); |
| 78 |
assertSource("for(let i=0;i<10;i++){let x=i;}x++;", |
| 79 |
"for (let i = 0; i < 10; i++) {\n let x = i;\n}\nx++;\n"); |
| 80 |
assertSource("function f(){let i=2;for(let j=0;j<i;++j)print(j);}", |
| 81 |
"function f() {\n let i = 2;\n for (let j = 0; j < i; ++j) \n print(j);\n}\n"); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
@Test |
| 88 |
public void testConstDeclarationToSource() |
| 89 |
{ |
| 90 |
assertSource("const x=0;x++;", |
| 91 |
"const x = 0;\nx++;\n"); |
| 92 |
assertSource("const a;if(true)a=1;", |
| 93 |
"const a;\nif (true) \n a = 1;\n"); |
| 94 |
assertSource("switch(x){case 1:const y;z++}", |
| 95 |
"switch (x) {\n case 1:\n const y;\n z++;\n}\n"); |
| 96 |
assertSource("if(c)const a=0;else a=1", |
| 97 |
"if (c) \n const a = 0;\nelse \n a = 1;\n"); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void testOctLiteralToSource() |
| 102 |
{ |
| 103 |
assertSource("010;", "010;\n"); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void testHexLiteralToSource() |
| 108 |
{ |
| 109 |
assertSource("0xff;", "0xff;\n"); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void testHexOctDecLiteralToSource() |
| 114 |
{ |
| 115 |
assertSource("0xff;\n9;\n07;\n1;", "0xff;\n9;\n07;\n1;\n"); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void testEmptySwitchToSource() |
| 120 |
{ |
| 121 |
assertSource("switch(1){}", "switch (1) {\n}\n"); |
| 122 |
} |
| 123 |
} |