use of com.google.template.soy.data.SoyRecord in project closure-templates by google.
the class RenderVisitorTest method testRenderDelegateCallWithoutDefault.
@Test
public void testRenderDelegateCallWithoutDefault() throws Exception {
String soyFileContent1a = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate allowemptydefault=\"true\"}\n" + " {param boo: 'aaaaaah' /}\n" + " {/delcall}\n" + "{/template}\n";
String soyFileContent1b = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate}\n" + " {param boo: 'aaaaaah' /}\n" + " {/delcall}\n" + "{/template}\n";
String soyFileContent2 = "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in SecretFeature
" 111 {$boo}\n" + "{/deltemplate}\n";
SoyRecord data = SoyValueConverterUtility.newDict();
ParseResult parseResult;
// ------ Test with only file 1a in bundle. ------
parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a).parse();
Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
// ------ Test with both files 1a and 2 in bundle. ------
parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a, soyFileContent2).parse();
activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "SecretFeature"));
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
// ------ Test with only file 1b in bundle. ------
activeDelPackageNames = Predicates.alwaysFalse();
try {
renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
}
try {
renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
}
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
}
use of com.google.template.soy.data.SoyRecord in project closure-templates by google.
the class RenderVisitorTest method testStreamLazyParamsToOutputStreamDirectly.
@Test
public void testStreamLazyParamsToOutputStreamDirectly() {
String soyFileContent = Joiner.on("\n").join("{namespace ns}", "", "{template .callee}", " {@param body: html}", " <div>", " {$body}", " </div>", "{/template}", "", "{template .caller}", " {@param future: string}", " {call .callee}", " {param body kind=\"html\"}", " static-content{sp}", " {$future}", " {/param}", " {/call}", "{/template}");
final StringBuilder outputSb = new StringBuilder();
final AtomicReference<String> outputAtFutureGetTime = new AtomicReference<>();
AbstractFuture<String> future = new AbstractFuture<String>() {
{
set("future-content");
}
@Override
public String get() throws InterruptedException, ExecutionException {
outputAtFutureGetTime.set(outputSb.toString());
return super.get();
}
};
SoyRecord data = SoyValueConverterUtility.newDict("future", future);
assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent).parse(), "ns.caller", data, TEST_IJ_DATA, Predicates.<String>alwaysFalse(), outputSb)).isEqualTo("<div>static-content future-content</div>");
// we only get the <div>. we used to get the 'static-content' as well but that was only
// because we aren't using the autoescaper.
assertThat(outputAtFutureGetTime.get()).isEqualTo("<div>");
}
use of com.google.template.soy.data.SoyRecord in project closure-templates by google.
the class DetachStateTest method testDetachOnParamTransclusion.
@Test
public void testDetachOnParamTransclusion() throws IOException {
CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "/** */", "{template .caller}", " {@param callerParam : string}", " {call .callee}", " {param calleeParam kind=\"text\"}", " prefix {$callerParam} suffix", " {/param}", " {/call}", "{/template}", "", "/** */", "{template .callee}", " {@param calleeParam : string}", " {$calleeParam}", "{/template}", "");
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.caller");
RenderContext context = getDefaultContext(templates);
SettableFuture<String> param = SettableFuture.create();
SoyRecord params = asRecord(ImmutableMap.of("callerParam", param));
CompiledTemplate template = factory.create(params, EMPTY_DICT);
BufferingAppendable output = LoggingAdvisingAppendable.buffering();
assertThat(template.render(output, context)).isEqualTo(RenderResult.continueAfter(param));
assertThat(output.toString()).isEqualTo("prefix ");
param.set("foo");
assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
assertThat(output.toString()).isEqualTo("prefix foo suffix");
}
use of com.google.template.soy.data.SoyRecord in project closure-templates by google.
the class RenderVisitorTest method testRenderDelegateCall.
@Test
public void testRenderDelegateCall() throws Exception {
String soyFileContent1 = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate}\n" + " {param boo: 'aaaaaah' /}\n" + " {/delcall}\n" + "{/template}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // default implementation (doesn't use $boo)
" 000\n" + "{/deltemplate}\n";
String soyFileContent2 = "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in SecretFeature
" 111 {$boo}\n" + "{/deltemplate}\n";
String soyFileContent3 = "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in AlternateSecretFeature
" 222 {call .helper data=\"all\" /}\n" + "{/deltemplate}\n";
String soyFileContent4 = "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{template .helper}\n" + " {$boo} {$ij.ijStr}\n" + "{/template}\n";
final ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3, soyFileContent4).errorReporter(FAIL).parse();
final SoyRecord data = SoyValueConverterUtility.newDict();
Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000");
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("111 aaaaaah");
activeDelPackageNames = Predicates.equalTo("AlternateSecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222 aaaaaah injected");
activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000");
activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "AlternateSecretFeature"));
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222 aaaaaah injected");
try {
renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, Predicates.in(ImmutableSet.of("SecretFeature", "AlternateSecretFeature")));
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("For delegate template 'myApp.myDelegate', found two active implementations with" + " equal priority");
}
}
use of com.google.template.soy.data.SoyRecord in project closure-templates by google.
the class RenderVisitorTest method testRenderFuture.
@Test
public void testRenderFuture() throws Exception {
final StringBuilder progress = new StringBuilder();
Flushable flushable = new Flushable() {
@Override
public void flush() {
progress.append("flush;");
}
};
String soyFileContent = "{namespace ns}\n" + "\n" + "/** @param boo @param foo @param goo */\n" + "{template .callerTemplate autoescape=\"deprecated-noncontextual\"}\n" + " {call .calleeTemplate data=\"all\" /}\n" + " {call .calleeTemplate data=\"$foo\" /}\n" + " {call .calleeTemplate data=\"all\"}\n" + " {param boo: $foo.boo /}\n" + " {/call}\n" + " {call .calleeTemplate data=\"all\"}\n" + " {param boo: 'moo' /}\n" + " {/call}\n" + " {call .calleeTemplate data=\"$foo\"}\n" + " {param boo}moo{/param}\n" + " {/call}\n" + " {call .calleeTemplate}\n" + " {param boo}zoo{/param}\n" + " {param goo: $foo.goo /}\n" + " {/call}\n" + "{/template}\n" + "\n" + "/**\n" + " * @param boo\n" + " * @param goo\n" + " */\n" + "{template .calleeTemplate autoescape=\"deprecated-noncontextual\"}\n" + " {$boo}{$ij.future}\n" + " {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
TemplateRegistry templateRegistry = SoyFileSetParserBuilder.forFileContents(soyFileContent).errorReporter(FAIL).parse().registry();
SoyDict foo = SoyValueConverterUtility.newDict("boo", new TestFuture("foo", progress), "goo", SoyValueConverterUtility.newList(3, 2, 1));
SoyDict data = SoyValueConverterUtility.newDict("boo", new TestFuture("boo", progress), "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
SoyRecord testIj = SoyValueConverterUtility.newDict("future", new TestFuture("ij", progress));
StringBuilder outputSb = new StringBuilder();
CountingFlushableAppendable output = new CountingFlushableAppendable(outputSb, flushable);
RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), output, templateRegistry, data, testIj, Predicates.<String>alwaysFalse(), null, xidRenamingMap, cssRenamingMap, false);
rv.exec(templateRegistry.getBasicTemplate("ns.callerTemplate"));
String expectedOutput = "booij 1 2 3\n" + "fooij 3 2 1\n" + "fooij 1 2 3\n" + "mooij 1 2 3\n" + "mooij 3 2 1\n" + "zooij 3 2 1\n";
assertThat(outputSb.toString()).isEqualTo(expectedOutput);
assertThat(progress.toString()).isEqualTo("booflush;ijflush;foo");
}
Aggregations