Search in sources :

Example 6 with CompiledTemplate

use of com.google.template.soy.jbcsrc.shared.CompiledTemplate in project closure-templates by google.

the class LazyClosureCompilerTest method testLetValueNode_captureSyntheticParameter.

// Regression test for a bug where captures of synthetic variables wouldn't be deduped properly
// and we would recapture the same synthetic multiple times.
@Test
public void testLetValueNode_captureSyntheticParameter() {
    // make sure that if we capture a synthetic we only capture it once
    CompiledTemplates templates = compileTemplateBody("{@param l : list<string>}", "{for $s in $l}", // the index function is implemented via a synthetic loop index
    "  {let $bar : index($s) + index($s) /}", "  {$bar}", "{/for}");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    List<Class<?>> innerClasses = Lists.newArrayList(template.getClass().getDeclaredClasses());
    innerClasses.remove(factory.getClass());
    Class<?> let = Iterables.getOnlyElement(innerClasses);
    assertThat(let.getSimpleName()).isEqualTo("let_bar");
    // the closures capture variables as constructor parameters.
    // in this case since index() always returns an unboxed integer the parameter should be a single
    // int.  In a previous version, we passed 2 ints.
    assertThat(let.getDeclaredConstructors()).hasLength(1);
    Constructor<?> cStruct = let.getDeclaredConstructors()[0];
    assertThat(Arrays.asList(cStruct.getParameterTypes())).isEqualTo(Arrays.asList(int.class));
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 7 with CompiledTemplate

use of com.google.template.soy.jbcsrc.shared.CompiledTemplate in project closure-templates by google.

the class BytecodeCompilerTest method testParamFields.

@Test
public void testParamFields() throws Exception {
    CompiledTemplate.Factory multipleParams = TemplateTester.compileTemplateBody("{@param foo : string}", "{@param baz : string}", "{@inject bar : string}", "{$foo + $baz + $bar}").getTemplateFactory("ns.foo");
    SoyDict params = SoyValueConverterUtility.newDict("foo", StringData.forValue("foo"), "bar", StringData.forValue("bar"), "baz", StringData.forValue("baz"));
    CompiledTemplate template = multipleParams.create(params, params);
    assertThat(getField("foo", template)).isEqualTo(StringData.forValue("foo"));
    assertThat(getField("bar", template)).isEqualTo(StringData.forValue("bar"));
    assertThat(getField("baz", template)).isEqualTo(StringData.forValue("baz"));
    TemplateMetadata templateMetadata = template.getClass().getAnnotation(TemplateMetadata.class);
    assertThat(templateMetadata.injectedParams()).asList().containsExactly("bar");
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).isEmpty();
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 8 with CompiledTemplate

use of com.google.template.soy.jbcsrc.shared.CompiledTemplate in project closure-templates by google.

the class BytecodeCompilerTest method render.

private String render(CompiledTemplates templates, SoyRecord params, String name) throws IOException {
    CompiledTemplate caller = templates.getTemplateFactory(name).create(params, EMPTY_DICT);
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    assertThat(caller.render(builder, getDefaultContext(templates))).isEqualTo(RenderResult.done());
    String output = builder.toString();
    return output;
}
Also used : BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate)

Example 9 with CompiledTemplate

use of com.google.template.soy.jbcsrc.shared.CompiledTemplate in project closure-templates by google.

the class BytecodeCompilerTest method testBasicFunctionality.

@Test
public void testBasicFunctionality() {
    // make sure we don't break standard reflection access
    CompiledTemplate.Factory factory = TemplateTester.compileTemplateBody("hello world").getTemplateFactory("ns.foo");
    assertThat(factory.getClass().getName()).isEqualTo("com.google.template.soy.jbcsrc.gen.ns.foo$Factory");
    assertThat(factory.getClass().getSimpleName()).isEqualTo("Factory");
    CompiledTemplate templateInstance = factory.create(EMPTY_DICT, EMPTY_DICT);
    Class<? extends CompiledTemplate> templateClass = templateInstance.getClass();
    assertThat(templateClass.getName()).isEqualTo("com.google.template.soy.jbcsrc.gen.ns.foo");
    assertThat(templateClass.getSimpleName()).isEqualTo("foo");
    TemplateMetadata templateMetadata = templateClass.getAnnotation(TemplateMetadata.class);
    assertThat(templateMetadata.contentKind()).isEqualTo("HTML");
    assertThat(templateInstance.kind()).isEqualTo(ContentKind.HTML);
    assertThat(templateMetadata.injectedParams()).isEmpty();
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).isEmpty();
    // ensure that the factory is an inner class of the template.
    assertThat(factory.getClass().getEnclosingClass()).isEqualTo(templateClass);
    assertThat(factory.getClass().getDeclaringClass()).isEqualTo(templateClass);
    assertThat(templateClass.getDeclaredClasses()).asList().contains(factory.getClass());
}
Also used : TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 10 with CompiledTemplate

use of com.google.template.soy.jbcsrc.shared.CompiledTemplate in project closure-templates by google.

the class DetachStateTest method testDetach_multipleNodes.

@Test
public void testDetach_multipleNodes() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("hello", // get merged
    "{' '}", "world");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    // Basic stuff works
    TestAppendable output = new TestAppendable();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("hello world");
    output = new TestAppendable();
    output.softLimitReached = true;
    // detached!!!
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello ");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello world");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // nothing was added
    assertThat(output.toString()).isEqualTo("hello world");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)20 Test (org.junit.Test)18 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)16 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)13 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)11 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)5 SoyRecord (com.google.template.soy.data.SoyRecord)2 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 SoyDict (com.google.template.soy.data.SoyDict)1 IOException (java.io.IOException)1