Created
October 21, 2015 13:55
-
-
Save antony/247d036782cd215a7c0d to your computer and use it in GitHub Desktop.
Handlebars Testing Framework for Ratpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pm.renderer.handlebars | |
import com.github.jknack.handlebars.Handlebars | |
import com.github.jknack.handlebars.Options | |
import com.github.jknack.handlebars.Template | |
import energizedwork.support.HandlebarsSpecification | |
import ratpack.handlebars.NamedHelper | |
import spock.lang.Ignore | |
class ExampleTemplateRenderingHelperSpec extends HandlebarsSpecification<TemplateRenderingHelper, String> { | |
void setupSpec() { | |
helper = new TemplateRenderingHelper() | |
} | |
def 'render a template'() { | |
given: | |
String partialPath = '/path/to/grault' | |
addPartial(partialPath, '<p>hello</p>') | |
when: | |
apply(partialPath, message: 'hello') | |
then: | |
rendered.p.text() == 'hello' | |
} | |
} | |
class TemplateRenderingHelper implements NamedHelper<String> { | |
public String getName() { | |
return 'template-renderer' | |
} | |
public CharSequence apply(String context, Options options) throws IOException { | |
String templatePath = context | |
Template template = options.partial(templatePath, options.fn) ?: options.handlebars.compile(templatePath) | |
if (!template) { | |
return 'No template found' | |
} | |
options.apply(template) | |
return new Handlebars.SafeString( options.apply(template)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package energizedwork.support | |
import com.github.jknack.handlebars.Context | |
import com.github.jknack.handlebars.Handlebars | |
import com.github.jknack.handlebars.Options | |
import com.github.jknack.handlebars.TagType | |
import com.github.jknack.handlebars.Template | |
import com.github.jknack.handlebars.io.StringTemplateSource | |
import com.github.jknack.handlebars.io.TemplateLoader | |
import com.github.jknack.handlebars.io.TemplateSource | |
import groovy.util.slurpersupport.GPathResult | |
import ratpack.exec.Promise | |
import ratpack.handlebars.NamedHelper | |
import spock.lang.Shared | |
import spock.lang.Specification | |
class HandlebarsSpecification<H extends NamedHelper<?>, C> extends Specification { | |
@Shared H helper | |
@Shared GPathResult rendered | |
@Shared CharSequence raw | |
private TemplateLoader loader = new TemplateLoader() { | |
private String suffix = '' | |
private String prefix = '.hbs' | |
Map<String, String> templates = [:] | |
@Override | |
TemplateSource sourceAt(String location) throws IOException { | |
return new StringTemplateSource(location, templates[location]) | |
} | |
@Override | |
String resolve(String location) { | |
throw new UnsupportedOperationException('Not sure what resolve() is for. Implement me?') | |
} | |
@Override | |
String getPrefix() { | |
return prefix | |
} | |
@Override | |
String getSuffix() { | |
return prefix | |
} | |
@Override | |
void setPrefix(String prefix) { | |
this.prefix = prefix | |
} | |
@Override | |
void setSuffix(String suffix) { | |
this.suffix = suffix | |
} | |
} | |
GPathResult apply(Map hash = [:], C context) { | |
Handlebars hbs = new Handlebars(loader) | |
Context ctx = Context.newBuilder(new Object()).build() | |
Options options = new Options.Builder(hbs, 'Whatever', TagType.VAR, ctx, Mock(Template)) | |
.setHash(hash) | |
.build() | |
raw = helper.apply(context, options) | |
rendered = new XmlSlurper().parseText('<any>'+raw+'</any>' as String) | |
} | |
protected addPartial(String path, String html) { | |
loader.templates[path] = html | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment