| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import static org.junit.Assert.*; |
| 8 |
|
| 9 |
import org.junit.After; |
| 10 |
import org.junit.Before; |
| 11 |
import org.junit.Test; |
| 12 |
import org.mozilla.javascript.CompilerEnvirons; |
| 13 |
import org.mozilla.javascript.Context; |
| 14 |
import org.mozilla.javascript.ErrorReporter; |
| 15 |
import org.mozilla.javascript.Parser; |
| 16 |
import org.mozilla.javascript.ast.AstRoot; |
| 17 |
|
| 18 |
|
| 19 |
l |
| 20 |
|
| 21 |
|
| 22 |
public class Bug688023Test { |
| 23 |
private Context cx; |
| 24 |
|
| 25 |
@Before |
| 26 |
public void setUp() { |
| 27 |
cx = Context.enter(); |
| 28 |
} |
| 29 |
|
| 30 |
@After |
| 31 |
public void tearDown() { |
| 32 |
Context.exit(); |
| 33 |
} |
| 34 |
|
| 35 |
private AstRoot parse(CharSequence cs) { |
| 36 |
CompilerEnvirons compilerEnv = new CompilerEnvirons(); |
| 37 |
compilerEnv.initFromContext(cx); |
| 38 |
ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter(); |
| 39 |
Parser p = new Parser(compilerEnv, compilationErrorReporter); |
| 40 |
return p.parse(cs.toString(), "<eval>", 1); |
| 41 |
} |
| 42 |
|
| 43 |
private String toSource(CharSequence cs) { |
| 44 |
return parse(cs).toSource(); |
| 45 |
} |
| 46 |
|
| 47 |
private static String lines(String... lines) { |
| 48 |
StringBuilder sb = new StringBuilder(); |
| 49 |
for (String line : lines) { |
| 50 |
sb.append(line).append('\n'); |
| 51 |
} |
| 52 |
return sb.toString(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void toSourceForInLoop() { |
| 57 |
assertEquals(lines( |
| 58 |
"for (x in y) ", |
| 59 |
" b = 1;" |
| 60 |
), toSource("for(x in y) b=1;")); |
| 61 |
assertEquals(lines( |
| 62 |
"for (x in y) {", |
| 63 |
" b = 1;", |
| 64 |
"}" |
| 65 |
), toSource("for(x in y) {b=1;}")); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void toSourceForLoop() { |
| 70 |
assertEquals(lines( |
| 71 |
"for (; ; ) ", |
| 72 |
" b = 1;" |
| 73 |
), toSource("for(;;) b=1;")); |
| 74 |
assertEquals(lines( |
| 75 |
"for (; ; ) {", |
| 76 |
" b = 1;", |
| 77 |
"}" |
| 78 |
), toSource("for(;;) {b=1;}")); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void toSourceWhileLoop() { |
| 83 |
assertEquals(lines( |
| 84 |
"while (a) ", |
| 85 |
" b = 1;" |
| 86 |
), toSource("while(a) b=1;")); |
| 87 |
assertEquals(lines( |
| 88 |
"while (a) {", |
| 89 |
" b = 1;", |
| 90 |
"}" |
| 91 |
), toSource("while(a) {b=1;}")); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void toSourceWithStatement() { |
| 96 |
assertEquals(lines( |
| 97 |
"with (a) ", |
| 98 |
" b = 1;" |
| 99 |
), toSource("with(a) b=1;")); |
| 100 |
assertEquals(lines( |
| 101 |
"with (a) {", |
| 102 |
" b = 1;", |
| 103 |
"}" |
| 104 |
), toSource("with(a) {b=1;}")); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void toSourceIfStatement() { |
| 109 |
assertEquals(lines( |
| 110 |
"if (a) ", |
| 111 |
" b = 1;" |
| 112 |
), toSource("if(a) b=1;")); |
| 113 |
assertEquals(lines( |
| 114 |
"if (a) {", |
| 115 |
" b = 1;", |
| 116 |
"}" |
| 117 |
), toSource("if(a) {b=1;}")); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void toSourceIfElseStatement() { |
| 122 |
assertEquals(lines( |
| 123 |
"if (a) ", |
| 124 |
" b = 1;", |
| 125 |
"else ", |
| 126 |
" b = 2;" |
| 127 |
), toSource("if(a) b=1; else b=2;")); |
| 128 |
assertEquals(lines( |
| 129 |
"if (a) {", |
| 130 |
" b = 1;", |
| 131 |
"} else ", |
| 132 |
" b = 2;" |
| 133 |
), toSource("if(a) { b=1; } else b=2;")); |
| 134 |
assertEquals(lines( |
| 135 |
"if (a) ", |
| 136 |
" b = 1;", |
| 137 |
"else {", |
| 138 |
" b = 2;", |
| 139 |
"}" |
| 140 |
), toSource("if(a) b=1; else { b=2; }")); |
| 141 |
assertEquals(lines( |
| 142 |
"if (a) {", |
| 143 |
" b = 1;", |
| 144 |
"} else {", |
| 145 |
" b = 2;", |
| 146 |
"}" |
| 147 |
), toSource("if(a) { b=1; } else { b=2; }")); |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void toSourceIfElseIfElseStatement() { |
| 152 |
assertEquals(lines( |
| 153 |
"if (a) ", |
| 154 |
" b = 1;", |
| 155 |
"else if (a) ", |
| 156 |
" b = 2;", |
| 157 |
"else ", |
| 158 |
" b = 3;" |
| 159 |
), toSource("if(a) b=1; else if (a) b=2; else b=3;")); |
| 160 |
assertEquals(lines( |
| 161 |
"if (a) {", |
| 162 |
" b = 1;", |
| 163 |
"} else if (a) ", |
| 164 |
" b = 2;", |
| 165 |
"else ", |
| 166 |
" b = 3;" |
| 167 |
), toSource("if(a) { b=1; } else if (a) b=2; else b=3;")); |
| 168 |
assertEquals(lines( |
| 169 |
"if (a) ", |
| 170 |
" b = 1;", |
| 171 |
"else if (a) {", |
| 172 |
" b = 2;", |
| 173 |
"} else ", |
| 174 |
" b = 3;" |
| 175 |
), toSource("if(a) b=1; else if (a) { b=2; } else b=3;")); |
| 176 |
assertEquals(lines( |
| 177 |
"if (a) {", |
| 178 |
" b = 1;", |
| 179 |
"} else if (a) {", |
| 180 |
" b = 2;", |
| 181 |
"} else ", |
| 182 |
" b = 3;" |
| 183 |
), toSource("if(a) { b=1; } else if (a) { b=2; } else b=3;")); |
| 184 |
assertEquals(lines( |
| 185 |
"if (a) ", |
| 186 |
" b = 1;", |
| 187 |
"else if (a) ", |
| 188 |
" b = 2;", |
| 189 |
"else {", |
| 190 |
" b = 3;", |
| 191 |
"}" |
| 192 |
), toSource("if(a) b=1; else if (a) b=2; else {b=3;}")); |
| 193 |
assertEquals(lines( |
| 194 |
"if (a) {", |
| 195 |
" b = 1;", |
| 196 |
"} else if (a) ", |
| 197 |
" b = 2;", |
| 198 |
"else {", |
| 199 |
" b = 3;", |
| 200 |
"}" |
| 201 |
), toSource("if(a) { b=1; } else if (a) b=2; else {b=3;}")); |
| 202 |
assertEquals(lines( |
| 203 |
"if (a) ", |
| 204 |
" b = 1;", |
| 205 |
"else if (a) {", |
| 206 |
" b = 2;", |
| 207 |
"} else {", |
| 208 |
" b = 3;", |
| 209 |
"}" |
| 210 |
), toSource("if(a) b=1; else if (a) { b=2; } else {b=3;}")); |
| 211 |
assertEquals(lines( |
| 212 |
"if (a) {", |
| 213 |
" b = 1;", |
| 214 |
"} else if (a) {", |
| 215 |
" b = 2;", |
| 216 |
"} else {", |
| 217 |
" b = 3;", |
| 218 |
"}" |
| 219 |
), toSource("if(a) { b=1; } else if (a) { b=2; } else {b=3;}")); |
| 220 |
} |
| 221 |
} |