mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-23 00:24:11 -08:00
Add some tests for AstWriter
This commit is contained in:
parent
06a4356543
commit
ccfddb19a4
|
@ -341,14 +341,10 @@ public class AstWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(DoLoop node) throws IOException {
|
private void print(DoLoop node) throws IOException {
|
||||||
writer.append("do");
|
writer.append("do ").ws();
|
||||||
if (node.getBody() instanceof Block) {
|
|
||||||
writer.ws();
|
|
||||||
} else {
|
|
||||||
writer.append(' ');
|
|
||||||
}
|
|
||||||
print(node.getBody());
|
print(node.getBody());
|
||||||
writer.append("while").ws().append('(');
|
writer.append("while").ws().append('(');
|
||||||
|
print(node.getCondition());
|
||||||
writer.append(");");
|
writer.append(");");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +368,6 @@ public class AstWriter {
|
||||||
print(node.getCondition());
|
print(node.getCondition());
|
||||||
writer.append(';');
|
writer.append(';');
|
||||||
print(node.getIncrement());
|
print(node.getIncrement());
|
||||||
writer.append(';');
|
|
||||||
writer.append(')').ws();
|
writer.append(')').ws();
|
||||||
print(node.getBody());
|
print(node.getBody());
|
||||||
}
|
}
|
||||||
|
@ -456,7 +451,9 @@ public class AstWriter {
|
||||||
writer.append(',').ws();
|
writer.append(',').ws();
|
||||||
print(node.getVariables().get(i));
|
print(node.getVariables().get(i));
|
||||||
}
|
}
|
||||||
writer.append(';');
|
if (node.isStatement()) {
|
||||||
|
writer.append(';');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(VariableInitializer node) throws IOException {
|
private void print(VariableInitializer node) throws IOException {
|
||||||
|
@ -580,12 +577,12 @@ public class AstWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(NumberLiteral node) throws IOException {
|
private void print(NumberLiteral node) throws IOException {
|
||||||
writer.append(node.getNumber());
|
writer.append(node.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(StringLiteral node) throws IOException {
|
private void print(StringLiteral node) throws IOException {
|
||||||
writer.append(node.getQuoteCharacter());
|
writer.append(node.getQuoteCharacter());
|
||||||
writer.append(ScriptRuntime.escapeString(node.getString(), node.getQuoteCharacter()));
|
writer.append(ScriptRuntime.escapeString(node.getValue(), node.getQuoteCharacter()));
|
||||||
writer.append(node.getQuoteCharacter());
|
writer.append(node.getQuoteCharacter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import static org.junit.Assert.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.theories.suppliers.TestedOn;
|
||||||
import org.mozilla.javascript.CompilerEnvirons;
|
import org.mozilla.javascript.CompilerEnvirons;
|
||||||
import org.mozilla.javascript.ast.AstRoot;
|
import org.mozilla.javascript.ast.AstRoot;
|
||||||
import org.teavm.codegen.SourceWriter;
|
import org.teavm.codegen.SourceWriter;
|
||||||
|
@ -88,6 +89,59 @@ public class AstWriterTest {
|
||||||
assertThat(transform("while(true) { continue; }"), is("while(true){continue;}"));
|
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 {
|
private String transform(String text) throws IOException {
|
||||||
CompilerEnvirons env = new CompilerEnvirons();
|
CompilerEnvirons env = new CompilerEnvirons();
|
||||||
env.setRecoverFromErrors(true);
|
env.setRecoverFromErrors(true);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user