Search in sources :

Example 1 with GenPyExprsVisitorFactory

use of com.google.template.soy.pysrc.internal.GenPyExprsVisitor.GenPyExprsVisitorFactory in project closure-templates by google.

the class PySrcMain method createVisitor.

@VisibleForTesting
static GenPyCodeVisitor createVisitor(SoyPySrcOptions pySrcOptions, ImmutableMap<String, String> currentManifest) {
    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;
    return new GenPyCodeVisitor(pySrcOptions, currentManifest, isComputableAsPyExprs, genPyExprsFactory, provider.get());
}
Also used : Supplier(com.google.common.base.Supplier) GenPyExprsVisitorFactory(com.google.template.soy.pysrc.internal.GenPyExprsVisitor.GenPyExprsVisitorFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with GenPyExprsVisitorFactory

use of com.google.template.soy.pysrc.internal.GenPyExprsVisitor.GenPyExprsVisitorFactory 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)

Aggregations

Supplier (com.google.common.base.Supplier)2 GenPyExprsVisitorFactory (com.google.template.soy.pysrc.internal.GenPyExprsVisitor.GenPyExprsVisitorFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 SoyNode (com.google.template.soy.soytree.SoyNode)1