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));
}
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());
}
}
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);
}
}
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);
}
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);
}
Aggregations