use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testExpressionLineNumbers.
@Test
public void testExpressionLineNumbers() throws Exception {
CompiledTemplates templates = TemplateTester.compileFile(Joiner.on("\n").join("{namespace ns}", "", "{template .foo}", " {@param p1 : ?}", " {@param p2 : ?}", " {@param p3 : ?}", " {@param p4 : ?}", // This is a single expression split across multiple lines
"{$p1", " + $p2", " + $p3", " + $p4", "}", "{/template}"));
assertThat(render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", 3, "p4", 4)), "ns.foo")).isEqualTo("10");
ListenableFuture<?> failed = Futures.immediateFailedFuture(new RuntimeException("boom"));
// failed future, the template should show a different line number
try {
render(templates, asRecord(ImmutableMap.of("p1", failed, "p2", 2, "p3", 3, "p4", 4)), "ns.foo");
fail();
} catch (Exception e) {
assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(8);
}
try {
render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", failed, "p3", 3, "p4", 4)), "ns.foo");
fail();
} catch (Exception e) {
assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(9);
}
try {
render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", failed, "p4", 4)), "ns.foo");
fail();
} catch (Exception e) {
assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(10);
}
try {
render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", 3, "p4", failed)), "ns.foo");
fail();
} catch (Exception e) {
assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(11);
}
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method compileFiles.
private CompiledTemplates compileFiles(String... soyFileContents) {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContents).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
return templates;
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testDelCallEscaping_separateCompilation.
// Tests for a bug where we would overescape deltemplates at the call site when the strict
// content kind of the deltemplate was unknown at compile time.
@Test
public void testDelCallEscaping_separateCompilation() throws IOException {
String soyFileContent1 = Joiner.on("\n").join("{namespace ns}", "", "{template .callerTemplate}", " {delcall myApp.myDelegate/}", "{/template}", "");
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1).parse().fileSet();
// apply an escaping directive to the callsite, just like the autoescaper would
CallDelegateNode cdn = SoyTreeUtils.getAllNodesOfType(soyTree.getChild(0), CallDelegateNode.class).get(0);
cdn.setEscapingDirectives(ImmutableList.of(new EscapeHtmlDirective()));
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
CompiledTemplate.Factory caller = templates.getTemplateFactory("ns.callerTemplate");
try {
renderWithContext(caller, getDefaultContext(templates));
fail();
} catch (IllegalArgumentException iae) {
assertThat(iae).hasMessageThat().isEqualTo("Found no active impl for delegate call to \"myApp.myDelegate\" (and delcall does " + "not set allowemptydefault=\"true\").");
}
String soyFileContent2 = Joiner.on("\n").join("{namespace ns2}", "", "{deltemplate myApp.myDelegate}", " <span>Hello</span>", "{/deltemplate}", "");
CompiledTemplates templatesWithDeltemplate = compileFiles(soyFileContent2);
// By passing an alternate context, we ensure the deltemplate selector contains the delegate
assertThat(renderWithContext(caller, getDefaultContext(templatesWithDeltemplate))).isEqualTo("<span>Hello</span>");
}
Aggregations