Further work on making JMustache work in GWT.

I've ironed out most of the kinks, but the GWT unit test still fails because
we're forced to do some sneaky things with java.io.Reader/Writer. I think it
will all work when compiled to JavaScript, so I need to check it in a real
project.
This commit is contained in:
Michael Bayne
2011-10-29 15:31:27 -07:00
parent f9319ce911
commit 407d999836
9 changed files with 143 additions and 3 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ libraryDependencies ++= Seq(
)
// filter the super-source directory from the build
unmanagedSources in Compile ~= (_.filterNot(_.getPath.indexOf("com/samskivert/mustache/gwt") != -1))
unmanagedSources in Compile ~= (_.filterNot(_.getPath.indexOf("com/samskivert/gwt") != -1))
// add our sources to the main jar file (including super-sources)
unmanagedResourceDirectories in Compile <+= baseDirectory / "src/main/java"
+26 -1
View File
@@ -41,6 +41,12 @@
</scm>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -59,6 +65,7 @@
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -73,10 +80,25 @@
<!-- yes, those quoted spaces are a workaround sanctioned by the Maven idiocracy -->
<compilerArgument>-Xlint" "-Xlint:-serial" "-Xlint:-path</compilerArgument>
<excludes>
<exclude>com/samskivert/mustache/gwt/**</exclude>
<exclude>com/samskivert/gwt/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<configuration>
<mode>htmlunit</mode>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
@@ -89,6 +111,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<includes><include>**/*Test.java</include></includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -0,0 +1,5 @@
<!-- defines our GWT module -->
<module>
<source path="mustache"/>
<super-source path="gwt"/>
</module>
@@ -17,8 +17,10 @@ public class DefaultCollector extends BasicCollector
if (iter != null) return iter;
if (value.getClass().isArray()) {
return Arrays.asList((Object[])value);
return Arrays.asList((Object[])value).iterator();
}
return null;
}
// TODO: override createFetcher and do some magic for JavaScript/JSON objects
}
@@ -0,0 +1,12 @@
//
// $Id$
package java.io;
/**
* A minimal version of {@code Reader} to satisfy GWT.
*/
public interface Reader
{
int read () throws IOException;
}
@@ -0,0 +1,21 @@
//
// $Id$
package java.io;
/**
* A basic implementation of {@code StringReader} for use in GWT.
*/
public class StringReader implements Reader
{
public StringReader (String data) {
_data = data;
}
public int read () throws IOException {
return (_pos >= _data.length()) ? -1 : _data.charAt(_pos++);
}
protected final String _data;
protected int _pos;
}
@@ -0,0 +1,20 @@
//
// $Id$
package java.io;
/**
* A basic implementation of {@code StringReader} for use in GWT.
*/
public class StringWriter implements Writer
{
public void write (String text) throws IOException {
_buf.append(text);
}
@Override public String toString () {
return _buf.toString();
}
protected final StringBuilder _buf = new StringBuilder();
}
@@ -0,0 +1,12 @@
//
// $Id$
package java.io;
/**
* A minimal version of {@code Writer} to satisfy GWT.
*/
public interface Writer
{
void write (String text) throws IOException;
}
@@ -0,0 +1,43 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache;
import java.util.HashMap;
import java.util.Map;
import com.google.gwt.junit.client.GWTTestCase;
import org.junit.*;
import static org.junit.Assert.*;
/**
* Tests basic Mustache operation in GWT.
*/
public class GwtTestMustache extends GWTTestCase
{
public String getModuleName () {
return "com.samskivert.Mustache";
}
@Test public void testSimpleVariable () {
test("bar", "{{foo}}", context("foo", "bar"));
}
protected void test (String expected, String template, Object ctx) {
test(Mustache.compiler(), expected, template, ctx);
}
protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx) {
assertEquals(expected, compiler.compile(template).execute(ctx));
}
protected Object context (Object... data) {
Map<String, Object> ctx = new HashMap<String, Object>();
for (int ii = 0; ii < data.length; ii += 2) {
ctx.put(data[ii].toString(), data[ii+1]);
}
return ctx;
}
}