Search in sources :

Example 36 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class GenPyExprsVisitorTest method testMsgSimpleSoyExpression.

@Test
public void testMsgSimpleSoyExpression() {
    String soyCode = "{@param username:?}\n" + "{msg desc=\"var placeholder\"}" + "Hello {$username}" + "{/msg}\n";
    String expectedPyCode = "translator_impl.render(" + "translator_impl.prepare(" + "###, " + "'Hello {USERNAME}', " + "('USERNAME',)), " + "{'USERNAME': str(data.get('username'))})";
    assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 37 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class SoyExprForPySubject method compilesTo.

/**
 * Asserts the subject compiles to the correct list of PyExprs.
 *
 * <p>The given Soy expr is wrapped in a full body of a template. The actual result is replaced
 * with ids for ### so that tests don't break when ids change.
 *
 * @param expectedPyExprs the expected result of compilation
 */
public void compilesTo(List<PyExpr> expectedPyExprs) {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(getSubject()).parse().fileSet();
    SoyNode node = SharedTestUtils.getNode(soyTree, 0);
    SharedTestUtils.simulateNewApiCall(injector, null, BidiGlobalDir.LTR);
    final IsComputableAsPyExprVisitor isComputableAsPyExprs = new IsComputableAsPyExprVisitor();
    // here we resolve it with a mutable field in a custom provider
    class PyCallExprVisitorSupplier implements Supplier<GenPyCallExprVisitor> {

        GenPyExprsVisitorFactory factory;

        @Override
        public GenPyCallExprVisitor get() {
            return new GenPyCallExprVisitor(isComputableAsPyExprs, checkNotNull(factory));
        }
    }
    PyCallExprVisitorSupplier provider = new PyCallExprVisitorSupplier();
    GenPyExprsVisitorFactory genPyExprsFactory = new GenPyExprsVisitorFactory(isComputableAsPyExprs, provider);
    provider.factory = genPyExprsFactory;
    GenPyExprsVisitor genPyExprsVisitor = genPyExprsFactory.create(localVarExprs, ErrorReporter.exploding());
    List<PyExpr> actualPyExprs = genPyExprsVisitor.exec(node);
    assertThat(actualPyExprs).hasSize(expectedPyExprs.size());
    for (int i = 0; i < expectedPyExprs.size(); i++) {
        PyExpr expectedPyExpr = expectedPyExprs.get(i);
        PyExpr actualPyExpr = actualPyExprs.get(i);
        assertThat(actualPyExpr.getText().replaceAll("\\([0-9]+", "(###")).isEqualTo(expectedPyExpr.getText());
        assertThat(actualPyExpr.getPrecedence()).isEqualTo(expectedPyExpr.getPrecedence());
    }
}
Also used : SoyNode(com.google.template.soy.soytree.SoyNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Supplier(com.google.common.base.Supplier) GenPyExprsVisitorFactory(com.google.template.soy.pysrc.internal.GenPyExprsVisitor.GenPyExprsVisitorFactory)

Example 38 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class SoyExprForPySubject method translatesTo.

/**
 * Asserts the subject translates to the expected PyExpr including verification of the exact
 * PyExpr class (e.g. {@code PyStringExpr.class}).
 *
 * @param expectedPyExpr the expected result of translation
 * @param expectedClass the expected class of the resulting PyExpr
 */
public void translatesTo(PyExpr expectedPyExpr, Class<? extends PyExpr> expectedClass) {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(untypedTemplateBodyForExpression(getSubject())).options(opts).parse().fileSet();
    PrintNode node = (PrintNode) SharedTestUtils.getNode(soyTree, 0);
    ExprNode exprNode = node.getExpr();
    PyExpr actualPyExpr = new TranslateToPyExprVisitor(localVarExprs, ErrorReporter.exploding()).exec(exprNode);
    assertThat(actualPyExpr.getText()).isEqualTo(expectedPyExpr.getText());
    assertThat(actualPyExpr.getPrecedence()).isEqualTo(expectedPyExpr.getPrecedence());
    if (expectedClass != null) {
        assertThat(actualPyExpr.getClass()).isEqualTo(expectedClass);
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 39 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TranslateToPyExprVisitorTest method testDataRef_localVars.

@Test
public void testDataRef_localVars() {
    Map<String, PyExpr> frame = Maps.newHashMap();
    frame.put("zoo", new PyExpr("zooData8", Integer.MAX_VALUE));
    assertThatSoyExpr("$zoo").with(frame).translatesTo("zooData8", Integer.MAX_VALUE);
    assertThatSoyExpr("$zoo.boo").with(frame).translatesTo("zooData8.get('boo')", Integer.MAX_VALUE);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 40 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TranslateToPyExprVisitorTest method testListLiteral.

@Test
public void testListLiteral() {
    assertThatSoyExpr("[]").translatesTo(new PyExpr("[]", Integer.MAX_VALUE), PyListExpr.class);
    assertThatSoyExpr("['blah', 123, $foo]").translatesTo(new PyExpr("['blah', 123, data.get('foo')]", Integer.MAX_VALUE), PyListExpr.class);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Aggregations

PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)82 Test (org.junit.Test)58 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)21 PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)6 LinkedHashMap (java.util.LinkedHashMap)4 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 SoyNode (com.google.template.soy.soytree.SoyNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)2 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1