Add some tests for AstWriter

This commit is contained in:
Alexey Andreev 2015-09-22 17:40:49 +03:00
parent 06a4356543
commit ccfddb19a4
2 changed files with 61 additions and 10 deletions

View File

@ -341,14 +341,10 @@ public class AstWriter {
}
private void print(DoLoop node) throws IOException {
writer.append("do");
if (node.getBody() instanceof Block) {
writer.ws();
} else {
writer.append(' ');
}
writer.append("do ").ws();
print(node.getBody());
writer.append("while").ws().append('(');
print(node.getCondition());
writer.append(");");
}
@ -372,7 +368,6 @@ public class AstWriter {
print(node.getCondition());
writer.append(';');
print(node.getIncrement());
writer.append(';');
writer.append(')').ws();
print(node.getBody());
}
@ -456,8 +451,10 @@ public class AstWriter {
writer.append(',').ws();
print(node.getVariables().get(i));
}
if (node.isStatement()) {
writer.append(';');
}
}
private void print(VariableInitializer node) throws IOException {
print(node.getTarget());
@ -580,12 +577,12 @@ public class AstWriter {
}
private void print(NumberLiteral node) throws IOException {
writer.append(node.getNumber());
writer.append(node.getValue());
}
private void print(StringLiteral node) throws IOException {
writer.append(node.getQuoteCharacter());
writer.append(ScriptRuntime.escapeString(node.getString(), node.getQuoteCharacter()));
writer.append(ScriptRuntime.escapeString(node.getValue(), node.getQuoteCharacter()));
writer.append(node.getQuoteCharacter());
}

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.io.StringReader;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.ast.AstRoot;
import org.teavm.codegen.SourceWriter;
@ -88,6 +89,59 @@ public class AstWriterTest {
assertThat(transform("while(true) { continue; }"), is("while(true){continue;}"));
}
@Test
public void writesBlock() throws IOException {
assertThat(transform("{ foo(); bar(); }"), is("{foo();bar();}"));
}
@Test
public void writesFor() throws IOException {
assertThat(transform("for (var i = 0; i < array.length; ++i) foo(array[i]);"),
is("for(var i=0;i<array.length;++i)foo(array[i]);"));
}
@Test
public void writesEmptyFor() throws IOException {
assertThat(transform("for (;;) foo();"), is("for(;;)foo();"));
}
@Test
public void writesForIn() throws IOException {
assertThat(transform("for (var property in window) alert(property);"),
is("for(var property in window)alert(property);"));
}
@Test
public void writesWhile() throws IOException {
assertThat(transform("while (shouldProceed()) proceed();"), is("while(shouldProceed())proceed();"));
}
@Test
public void writesDoWhile() throws IOException {
assertThat(transform("do proceed(); while(shouldRepeat());"), is("do proceed();while(shouldRepeat());"));
}
@Test
public void writesIfElse() throws IOException {
assertThat(transform("if (test()) performTrue(); else performFalse();"),
is("if(test())performTrue();else performFalse();"));
}
@Test
public void writesIf() throws IOException {
assertThat(transform("if (shouldPerform()) perform();"), is("if(shouldPerform())perform();"));
}
@Test
public void writesSwitch() throws IOException {
assertThat(transform("switch (c) { "
+ "case '?': matchAny(); break; "
+ "case '*': matchSequence(); break;"
+ "default: matchChar(c); break; } "),
is("switch(c){case '?':matchAny();break;case '*':matchSequence();break;"
+ "default:matchChar(c);break;}"));
}
private String transform(String text) throws IOException {
CompilerEnvirons env = new CompilerEnvirons();
env.setRecoverFromErrors(true);