use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.
the class ParseExpressionTest method testParseProtoInitCall.
@Test
public void testParseProtoInitCall() throws Exception {
ExprNode expr = assertThatExpression("my.Proto(a: 1, b: glo.bal, c: fn('str'))").isValidExpression();
ProtoInitNode protoFn = (ProtoInitNode) expr;
assertThat(protoFn.getProtoName()).isEqualTo("my.Proto");
assertThat(protoFn.getParamNames()).containsExactly("a", "b", "c").inOrder();
assertThat(protoFn.numChildren()).isEqualTo(3);
assertThat(((IntegerNode) protoFn.getChild(0)).getValue()).isEqualTo(1);
assertThat(((GlobalNode) protoFn.getChild(1)).getName()).isEqualTo("glo.bal");
assertThat(((StringNode) ((FunctionNode) protoFn.getChild(2)).getChild(0)).getValue()).isEqualTo("str");
}
use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.
the class TemplateNodeTest method testValidVariant.
@Test
public void testValidVariant() {
// Variant is a string literal: There's no expression and the value is already resolved.
TemplateDelegateNode node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"'abc'\"}", "{/deltemplate}"));
assertEquals("namespace.boo", node.getDelTemplateName());
assertEquals("abc", node.getDelTemplateVariant());
assertEquals("abc", node.getDelTemplateKey().variant());
// Variant is a global, that was not yet resolved.
node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
assertEquals("namespace.boo", node.getDelTemplateName());
assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateVariant());
assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateKey().variant());
// Verify the global expression.
List<ExprRootNode> exprs = node.getExprList();
assertEquals(1, exprs.size());
ExprRootNode expr = exprs.get(0);
assertEquals("test.GLOBAL_CONSTANT", expr.toSourceString());
assertEquals(1, expr.numChildren());
assertTrue(expr.getRoot() instanceof GlobalNode);
// Substitute the global expression.
expr.replaceChild(0, new IntegerNode(123, expr.getRoot().getSourceLocation()));
// Check the new values.
assertEquals("123", node.getDelTemplateVariant());
assertEquals("123", node.getDelTemplateKey().variant());
// Resolve a global to a string.
node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
node.getExprList().get(0).replaceChild(0, new StringNode("variant", QuoteStyle.SINGLE, node.getSourceLocation()));
assertEquals("variant", node.getDelTemplateVariant());
assertEquals("variant", node.getDelTemplateKey().variant());
}
use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.
the class ExpressionSubject method isValidGlobalNamed.
void isValidGlobalNamed(String name) {
GlobalNode globalNode = (GlobalNode) parseExpression();
if (errorReporter.hasErrors()) {
fail("is valid global", errorReporter.getErrors());
}
String actualName = globalNode.getName();
if (!actualName.equals(name)) {
failWithBadResults("is global named", name, "has name", actualName);
}
String actualSourceString = globalNode.toSourceString();
if (!actualSourceString.equals(name)) {
failWithBadResults("is global named", name, "has source string", actualSourceString);
}
}
Aggregations