|
| 1 | +package com.github.jknack.handlebars; |
| 2 | + |
| 3 | +import com.github.jknack.handlebars.context.FieldValueResolver; |
| 4 | +import com.github.jknack.handlebars.context.JavaBeanValueResolver; |
| 5 | +import com.github.jknack.handlebars.context.MapValueResolver; |
| 6 | +import com.github.jknack.handlebars.context.MethodValueResolver; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class InternalDataTest extends AbstractTest { |
| 12 | + |
| 13 | + @Override |
| 14 | + protected Handlebars newHandlebars() { |
| 15 | + Handlebars handlebars = new Handlebars(); |
| 16 | + handlebars.registerHelper("printFooAndBar", (context, options) -> |
| 17 | + String.format("%s %s", options.context.data("foo"), options.context.internalData("bar"))); |
| 18 | + return handlebars; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + protected Object configureContext(final Object model) { |
| 23 | + return Context.newBuilder(model) |
| 24 | + .resolver( |
| 25 | + MapValueResolver.INSTANCE, |
| 26 | + JavaBeanValueResolver.INSTANCE, |
| 27 | + FieldValueResolver.INSTANCE, |
| 28 | + MethodValueResolver.INSTANCE) |
| 29 | + .build() |
| 30 | + .data("foo", "foo") |
| 31 | + .internalData("bar", "bar"); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void dataAvailableForRendering() throws IOException { |
| 36 | + shouldCompileTo("{{foo}}", "", "foo"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void internalDataNotAvailableForRendering() throws IOException { |
| 41 | + shouldCompileTo("{{bar}}", "", ""); |
| 42 | + shouldCompileTo("{{./bar}}", "", ""); |
| 43 | + shouldCompileTo("{{../bar}}", "", ""); |
| 44 | + shouldCompileTo("{{.././bar}}", "", ""); |
| 45 | + shouldCompileTo("{{this.bar}}", "", ""); |
| 46 | + shouldCompileTo("{{internalData}}", "", ""); |
| 47 | + shouldCompileTo("{{internalData.bar}}", "", ""); |
| 48 | + shouldCompileTo("{{this.internalData}}", "", ""); |
| 49 | + shouldCompileTo("{{this.internalData.bar}}", "", ""); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void percentFormat() throws IOException { |
| 54 | + shouldCompileTo("{{printFooAndBar}}", "", "foo bar"); |
| 55 | + } |
| 56 | +} |
0 commit comments