use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class AddDebugAttributesPassTest method assertRewritten.
/**
* Returns the contextually rewritten and injected source.
*
* <p>The Soy tree may have multiple files, but only the source code for the first is returned.
*/
private void assertRewritten(String expectedOutput, String input) {
String namespace = "{namespace ns}\n\n";
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(namespace + input).addHtmlAttributesForDebugging(true).parse().fileSet();
StringBuilder src = new StringBuilder();
src.append(soyTree.getChild(0).toSourceString());
String output = src.toString().trim();
if (output.startsWith("{namespace ns")) {
output = output.substring(output.indexOf('}') + 1).trim();
}
assertThat(output).isEqualTo(expectedOutput);
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class AddHtmlCommentsForDebugPassTest method runPass.
/**
* Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
* a map that contains pairs of template names and rewritten template body.
*/
private static ImmutableMap<String, String> runPass(String input, String fileName) {
String soyFile = "{namespace ns}" + input;
IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
ErrorReporter boom = ErrorReporter.exploding();
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
// We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
new AddHtmlCommentsForDebugPass().run(soyTree, registry);
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (TemplateNode node : soyTree.getChild(0).getChildren()) {
StringBuilder sb = new StringBuilder();
node.appendSourceStringForChildren(sb);
String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
builder.put(templateName, sb.toString());
}
return builder.build();
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class VeLogInstrumentationVisitorTest method testLoggingFunctionsInsertingLetBlocks.
@Test
public void testLoggingFunctionsInsertingLetBlocks() {
// Test for using print node as attribute name.
assertThatSourceString(runPass("{let $foo : 'data-ved' /}" + "{velog Bar}<div><span {$foo}={currentVed()}></span></div>{/velog}")).isEqualTo("{let $foo : 'data-ved' /}{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "></span>" + "</div>" + "{/velog}");
// Test for multiple logging functions.
assertThatSourceString(runPass("{@param foo: string}{@param bar: string}" + "{velog Bar}<div>" + "<span {$foo}={currentVed()} {$bar}={currentVed(1)}></span>" + "</div>{/velog}")).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "{let $soy_logging_function_attribute_1}{$bar}{/let} " + "{$soy_logging_function_attribute_1}=\"placeholder\"" + "{$$loggingFunction('currentVed', [1], $soy_logging_function_attribute_1, 1)}" + ">" + "</span>" + "</div>" + "{/velog}");
// Test that counters work fine for nested tags.
assertThatSourceString(runPass("{@param foo: string}{@param bar: string}" + "{velog Bar}<div>" + "<span " + "{$foo}={currentVed()} " + "{let $baz kind=\"html\"}<input>{/let} " + "{$bar}={currentVed(1)}" + "></span>" + "</div>{/velog}")).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "{let $baz kind=\"html\"}<input>{/let}" + "{let $soy_logging_function_attribute_1}{$bar}{/let} " + "{$soy_logging_function_attribute_1}=\"placeholder\"" + "{$$loggingFunction('currentVed', [1], $soy_logging_function_attribute_1, 1)}" + ">" + "</span>" + "</div>" + "{/velog}");
// Test for calling another template.
SoyFileSetNode node = runPass("{velog Bar}<div>" + "<span {call .attr}{param foo : 'data-ved' /}{/call}></span>" + "</div>{/velog}" + "{/template}" + "" + "{template .attr kind=\"attributes\"}" + "{@param foo : string}" + "{$foo}={currentVed()}");
StringBuilder sb = new StringBuilder();
node.getChild(0).getChild(0).appendSourceStringForChildren(sb);
assertThat(sb.toString()).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span {call .attr}{param foo : 'data-ved' /}{/call}>" + "</span>" + "</div>" + "{/velog}");
sb = new StringBuilder();
node.getChild(0).getChild(1).appendSourceStringForChildren(sb);
assertThat(sb.toString()).isEqualTo("{let $soy_logging_function_attribute_0}{$foo}{/let}" + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}");
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testFallbackMsgsUsingSoySource.
@Test
public void testFallbackMsgsUsingSoySource() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
// Execute the visitor.
new InsertMsgsVisitor(null, /* msgBundle */
FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("trans1");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("noTrans1");
assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("trans1");
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testBasicMsgsUsingSoySource.
@Test
public void testBasicMsgsUsingSoySource() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(5);
MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
assertThat(msg.numChildren()).isEqualTo(5);
MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
// Execute the visitor.
new InsertMsgsVisitor(null, /* msgBundle */
FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(12);
assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("random");
assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$foo");
assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("<a href=\"");
assertThat(((PrintNode) template.getChild(5)).getExpr().toSourceString()).isEqualTo("$goo");
assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("\">");
assertThat(((RawTextNode) template.getChild(7)).getRawText()).isEqualTo("slimy");
assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("</a>");
assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Aggregations