From ccfddb19a418723d50234ec2079bddd4bf40cd7e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 22 Sep 2015 17:40:49 +0300 Subject: [PATCH] Add some tests for AstWriter --- .../java/org/teavm/jso/plugin/AstWriter.java | 17 +++--- .../org/teavm/jso/plugin/AstWriterTest.java | 54 +++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/AstWriter.java b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/AstWriter.java index 1e4dabdf2..27ac6aeba 100644 --- a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/AstWriter.java +++ b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/AstWriter.java @@ -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,7 +451,9 @@ public class AstWriter { writer.append(',').ws(); print(node.getVariables().get(i)); } - writer.append(';'); + if (node.isStatement()) { + writer.append(';'); + } } private void print(VariableInitializer node) throws IOException { @@ -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()); } diff --git a/teavm-jso-impl/src/test/java/org/teavm/jso/plugin/AstWriterTest.java b/teavm-jso-impl/src/test/java/org/teavm/jso/plugin/AstWriterTest.java index a67a57ab0..3bfd3281d 100644 --- a/teavm-jso-impl/src/test/java/org/teavm/jso/plugin/AstWriterTest.java +++ b/teavm-jso-impl/src/test/java/org/teavm/jso/plugin/AstWriterTest.java @@ -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