Added standard Mustache specs tests suite.

The groups with failing tests are currently commented out (everything except
interpolation). The main reason for failures are due to the requirement that
standalone lines that contain nothing but whitespace be stripped from the
output.
This commit is contained in:
Yoryos Valotasios
2013-03-05 21:54:29 +01:00
committed by Michael Bayne
parent 3a5da915e9
commit 2889ee8692
9 changed files with 246 additions and 1 deletions
+3
View File
@@ -0,0 +1,3 @@
[submodule "src/test/resources/specs"]
path = src/test/resources/specs
url = git://github.com/mustache/spec.git
+6
View File
@@ -54,6 +54,12 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -8,7 +8,6 @@ import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -0,0 +1,47 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache.specs;
import java.util.Map;
/**
* @author Yoryos Valotasios
*/
public class Spec
{
private final Map<String, Object> map;
private final Map<String, String> partials;
public Spec (Map<String, Object> map) {
this.map = map;
@SuppressWarnings("unchecked") Map<String, String> partials =
(Map<String, String>) map.get("partials");
this.partials = partials;
}
public String getName () {
return (String) map.get("name");
}
public String getDescription () {
return (String) map.get("descr");
}
public String getTemplate () {
return (String) map.get("template");
}
public String getExpectedOutput () {
return (String) map.get("expected");
}
public Object getData () {
return map.get("data");
}
public String getPartial (String name) {
return partials == null ? null : partials.get(name);
}
}
@@ -0,0 +1,26 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache.specs;
import java.io.Reader;
import java.io.StringReader;
import com.samskivert.mustache.Mustache;
public class SpecAwareTemplateLoader implements Mustache.TemplateLoader
{
private static final String EMPTY_STRING = "";
private Spec spec;
public void setSpec (Spec spec) {
this.spec = spec;
}
@Override public Reader getTemplate (String name) throws Exception {
if (spec == null) return new StringReader(EMPTY_STRING);
String partial = spec.getPartial(name);
return new StringReader(partial == null ? EMPTY_STRING : partial);
}
}
@@ -0,0 +1,33 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache.specs;
import java.lang.reflect.Method;
import org.junit.runners.model.FrameworkMethod;
/**
* @author Yoryos Valotasios
*/
public class SpecFrameworkTest extends FrameworkMethod
{
private final String group;
private final Spec spec;
public SpecFrameworkTest (Method method, String group, Spec spec) {
super(method);
this.group = group;
this.spec = spec;
}
@Override
public Object invokeExplosively (Object target, Object... params) throws Throwable {
return super.invokeExplosively(target, spec);
}
@Override
public String getName () {
return group + ": " + spec.getName();
}
}
@@ -0,0 +1,91 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache.specs;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.yaml.snakeyaml.Yaml;
/**
* @author Yoryos Valotasios
*/
public class SpecRunner extends BlockJUnit4ClassRunner
{
private final Yaml yaml = new Yaml();
private final List<FrameworkMethod> tests;
public SpecRunner (Class<?> klass) throws InitializationError {
super(klass);
if (klass != SpecTest.class) throw new IllegalArgumentException(
SpecRunner.class.getSimpleName() + " should only be used with classes of type " +
SpecTest.class.getName());
this.tests = computeTests();
}
@Override
protected List<FrameworkMethod> computeTestMethods () {
return tests;
}
@Override
protected void validateInstanceMethods (List<Throwable> errors) {
validatePublicVoidNoArgMethods(After.class, false, errors);
validatePublicVoidNoArgMethods(Before.class, false, errors);
validateTestMethods(errors);
}
private List<FrameworkMethod> computeTests () throws InitializationError {
String[] groups = new String[] {
// "comments",
// "delimiters",
"interpolation",
// "inverted",
// "sections",
// "partials"
};
Method m = getTestClassMethod("test", Spec.class);
List<FrameworkMethod> tests = new ArrayList<FrameworkMethod>();
for (String name : groups) {
for (Map<String, Object> test: getTestsForGroup(name)) {
tests.add(new SpecFrameworkTest(m, name, new Spec(test)));
}
}
return tests;
}
private Method getTestClassMethod (String name, Class<?>... paramTypes)
throws InitializationError {
try {
return getTestClass().getJavaClass().getDeclaredMethod(name, paramTypes);
} catch (NoSuchMethodException ex) {
throw new InitializationError("Could not find " + name + " " +
getTestClass().getJavaClass());
} catch (SecurityException ex) {
throw new InitializationError("Could not find " + name + " " +
getTestClass().getJavaClass());
}
}
private Iterable<Map<String, Object>> getTestsForGroup (String name) {
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>)
yaml.load(getClass().getResourceAsStream("/specs/specs/" + name + ".yml"));
@SuppressWarnings("unchecked") List<Map<String, Object>> tests = (List<Map<String, Object>>)
map.get("tests");
return tests;
}
}
@@ -0,0 +1,39 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache.specs;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
/**
* @author Yoryos Valotasios
*/
@RunWith(SpecRunner.class)
public class SpecTest
{
private static Mustache.Compiler compiler;
private static SpecAwareTemplateLoader loader;
@BeforeClass
public static void setUp () {
loader = new SpecAwareTemplateLoader();
compiler = Mustache.compiler().defaultValue("").withLoader(loader);
}
public void test (Spec spec) {
loader.setSpec(spec);
String tmpl = spec.getTemplate();
Template t = compiler.compile(spec.getTemplate());
String out = t.execute(spec.getData());
Assert.assertEquals(String.format("When rendering '''%s''' with '%s'",
tmpl.replaceAll("\n", "\\\\n"),
spec.getData().toString().replaceAll("\n", "\\\\n")),
spec.getExpectedOutput(), out);
}
}