use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class TemplateParserTest method testParseForeachStmt.
@Test
public void testParseForeachStmt() throws Exception {
String templateBody = "{@param goose : ?}{@param foo: ?}\n" + " {for $goo in $goose}\n" + " {$goose.numKids} goslings.{\\n}\n" + " {/for}\n" + " {for $boo in $foo.booze}\n" + " Scary drink {$boo.name}!\n" + " {if not isLast($boo)}{\\n}{/if}\n" + " {ifempty}\n" + " Sorry, no booze.\n" + " {/for}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(2, nodes.size());
ForNode fn0 = (ForNode) nodes.get(0);
assertEquals("$goose", fn0.getExpr().toSourceString());
assertTrue(fn0.getExpr().getRoot() instanceof VarRefNode);
assertEquals(1, fn0.numChildren());
ForNonemptyNode fn0fnn0 = (ForNonemptyNode) fn0.getChild(0);
assertEquals("goo", fn0fnn0.getVarName());
assertEquals(2, fn0fnn0.numChildren());
assertEquals("$goose.numKids", ((PrintNode) fn0fnn0.getChild(0)).getExpr().toSourceString());
assertEquals(" goslings.\n", ((RawTextNode) fn0fnn0.getChild(1)).getRawText());
ForNode fn1 = (ForNode) nodes.get(1);
assertEquals("$foo.booze", fn1.getExpr().toSourceString());
assertTrue(fn1.getExpr().getRoot() instanceof FieldAccessNode);
assertEquals(2, fn1.numChildren());
ForNonemptyNode fn1fnn0 = (ForNonemptyNode) fn1.getChild(0);
assertEquals("boo", fn1fnn0.getVarName());
assertEquals("$foo.booze", fn1fnn0.getExpr().toSourceString());
assertEquals("boo", fn1fnn0.getVarName());
assertEquals(4, fn1fnn0.numChildren());
IfNode fn1fnn0in = (IfNode) fn1fnn0.getChild(3);
assertEquals(1, fn1fnn0in.numChildren());
assertEquals("not isLast($boo)", ((IfCondNode) fn1fnn0in.getChild(0)).getExpr().toSourceString());
ForIfemptyNode fn1fin1 = (ForIfemptyNode) fn1.getChild(1);
assertEquals(1, fn1fin1.numChildren());
assertEquals("Sorry, no booze.", ((RawTextNode) fn1fin1.getChild(0)).getRawText());
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class TemplateParserTest method testParsePrintStmtWithPhname.
@Test
public void testParsePrintStmtWithPhname() throws Exception {
String templateBody = "{@param boo : ?}\n" + "{msg desc=\"...\"}\n" + " {$boo.foo}\n" + " {$boo.foo phname=\"booFoo\"}\n" + " {$boo.foo phname=\"booFoo\" }\n" + " {print $boo.foo phname=\"boo_foo\"}\n" + "{/msg}";
List<StandaloneNode> nodes = ((MsgFallbackGroupNode) parseTemplateContent(templateBody, FAIL).getChild(0)).getChild(0).getChildren();
assertEquals(4, nodes.size());
PrintNode pn0 = (PrintNode) ((MsgPlaceholderNode) nodes.get(0)).getChild(0);
assertEquals("$boo.foo", pn0.getExpr().toSourceString());
assertEquals("FOO", pn0.genBasePhName());
assertEquals("{$boo.foo}", pn0.toSourceString());
PrintNode pn1 = (PrintNode) ((MsgPlaceholderNode) nodes.get(1)).getChild(0);
assertEquals("$boo.foo", pn1.getExpr().toSourceString());
assertEquals("BOO_FOO", pn1.genBasePhName());
assertEquals("{$boo.foo phname=\"booFoo\"}", pn1.toSourceString());
assertEquals(0, pn1.getChildren().size());
assertTrue(pn1.getExpr().getRoot() instanceof FieldAccessNode);
PrintNode pn2 = (PrintNode) ((MsgPlaceholderNode) nodes.get(2)).getChild(0);
assertEquals("$boo.foo", pn2.getExpr().toSourceString());
assertEquals("BOO_FOO", pn2.genBasePhName());
assertEquals("{$boo.foo phname=\"booFoo\"}", pn2.toSourceString());
PrintNode pn3 = (PrintNode) ((MsgPlaceholderNode) nodes.get(3)).getChild(0);
assertEquals("$boo.foo", pn3.getExpr().toSourceString());
assertEquals("BOO_FOO", pn3.genBasePhName());
assertEquals("{print $boo.foo phname=\"boo_foo\"}", pn3.toSourceString());
assertFalse(pn0.genSamenessKey().equals(pn1.genSamenessKey()));
assertTrue(pn1.genSamenessKey().equals(pn2.genSamenessKey()));
assertFalse(pn1.genSamenessKey().equals(pn3.genSamenessKey()));
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class MsgCompiler method putPlaceholderIntoMap.
private void putPlaceholderIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgPlaceholderPart placeholder) throws AssertionError {
String placeholderName = placeholder.getPlaceholderName();
if (!placeholderNameToPutStatement.containsKey(placeholderName)) {
MsgPlaceholderNode repPlaceholderNode = originalMsg.getRepPlaceholderNode(placeholder.getPlaceholderName());
if (repPlaceholderNode.numChildren() == 0) {
throw new IllegalStateException("empty rep node for: " + placeholderName);
}
StandaloneNode initialNode = repPlaceholderNode.getChild(0);
Statement putEntyInMap;
if (initialNode instanceof MsgHtmlTagNode) {
putEntyInMap = addHtmlTagNodeToPlaceholderMap(mapExpression, placeholderName, (MsgHtmlTagNode) initialNode);
} else if (initialNode instanceof CallNode) {
putEntyInMap = addCallNodeToPlaceholderMap(mapExpression, placeholderName, (CallNode) initialNode);
} else if (initialNode instanceof PrintNode) {
putEntyInMap = addPrintNodeToPlaceholderMap(mapExpression, placeholderName, (PrintNode) initialNode);
} else if (initialNode instanceof RawTextNode) {
putEntyInMap = addRawTextNodeToPlaceholderMap(mapExpression, placeholderName, (RawTextNode) initialNode);
} else {
// the AST for MsgNodes guarantee that these are the only options
throw new AssertionError("Unexpected child: " + initialNode.getClass());
}
placeholderNameToPutStatement.put(placeholder.getPlaceholderName(), putEntyInMap.withSourceLocation(repPlaceholderNode.getSourceLocation()));
}
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class ResolvePackageRelativeCssNamesVisitorTest method testBaseCssOnNamespace.
@Test
public void testBaseCssOnNamespace() {
TemplateNode template = compileTemplate("{namespace boo cssbase=\"some.test.package\"}\n\n" + "/** Test template.*/\n" + "{template .foo}\n" + " <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class ResolvePackageRelativeCssNamesVisitorTest method testBaseCssOnTemplate.
@Test
public void testBaseCssOnTemplate() {
TemplateNode template = compileTemplate("{namespace boo}\n\n" + "/** Test template. */\n" + "{template .foo cssbase=\"some.test.package\"}\n" + " <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
Aggregations