Search in sources :

Example 76 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgWithPlural.

@Test
public void testMsgWithPlural() {
    String soyCode = "{@param numDrafts:?}\n" + "{msg desc=\"simple plural\"}" + "{plural $numDrafts}" + "{case 0}No drafts" + "{case 1}1 draft" + "{default}{$numDrafts} drafts" + "{/plural}" + "{/msg}";
    String expectedPyCode = "translator_impl.render_plural(" + "translator_impl.prepare_plural(" + "###, " + "{" + "'=0': 'No drafts', " + "'=1': '1 draft', " + "'other': '{NUM_DRAFTS_2} drafts'" + "}, " + "('NUM_DRAFTS_1', 'NUM_DRAFTS_2')), " + "data.get('numDrafts'), " + "{" + "'NUM_DRAFTS_1': data.get('numDrafts'), " + "'NUM_DRAFTS_2': str(data.get('numDrafts'))" + "})";
    assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 77 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgWithSelect.

@Test
public void testMsgWithSelect() {
    String soyCode = "{@param userGender:?}\n" + "{@param targetGender:?}\n" + "{msg desc=\"...\"}\n" + "  {select $userGender}\n" + "    {case 'female'}\n" + "      {select $targetGender}\n" + "        {case 'female'}Reply to her.\n" + "        {case 'male'}Reply to him.\n" + "        {default}Reply to them.\n" + "      {/select}\n" + "    {case 'male'}\n" + "      {select $targetGender}\n" + "        {case 'female'}Reply to her.\n" + "        {case 'male'}Reply to him.\n" + "        {default}Reply to them.\n" + "      {/select}\n" + "    {default}\n" + "      {select $targetGender}\n" + "        {case 'female'}Reply to her.\n" + "        {case 'male'}Reply to him.\n" + "        {default}Reply to them.\n" + "      {/select}\n" + "   {/select}\n" + "{/msg}\n";
    String expectedPyCode = "translator_impl.render_icu(" + "translator_impl.prepare_icu(" + "###, " + "'{USER_GENDER,select," + "female{" + "{TARGET_GENDER,select," + "female{Reply to her.}" + "male{Reply to him.}" + "other{Reply to them.}}" + "}" + "male{" + "{TARGET_GENDER,select," + "female{Reply to her.}" + "male{Reply to him.}" + "other{Reply to them.}}" + "}" + "other{" + "{TARGET_GENDER,select," + "female{Reply to her.}" + "male{Reply to him.}" + "other{Reply to them.}}" + "}" + "}', " + "('USER_GENDER', 'TARGET_GENDER')), " + "{" + "'USER_GENDER': data.get('userGender'), " + "'TARGET_GENDER': data.get('targetGender')" + "})";
    assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 78 with PyExpr

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

the class GenPyExprsVisitorTest method testIf.

@Test
public void testIf() {
    String soyNodeCode = "{@param boo:?}\n" + "{@param goo:?}\n" + "{if $boo}\n" + "  Blah\n" + "{elseif not $goo}\n" + "  Bleh\n" + "{else}\n" + "  Bluh\n" + "{/if}\n";
    String expectedPyExprText = "'Blah' if data.get('boo') else 'Bleh' if not data.get('goo') else 'Bluh'";
    assertThatSoyExpr(soyNodeCode).compilesTo(new PyExpr(expectedPyExprText, PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL)));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 79 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgOnlyLiteral.

@Test
public void testMsgOnlyLiteral() {
    String soyCode = "{msg meaning=\"verb\" desc=\"The word 'Archive' used as a verb.\"}" + "Archive" + "{/msg}\n";
    String expectedPyCode = "translator_impl.render_literal(" + "translator_impl.prepare_literal(" + "###, " + "'Archive'))";
    assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 80 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgWithHtmlNode.

@Test
public void testMsgWithHtmlNode() {
    // msg with HTML tags and raw texts
    String soyCode = "{@param url:?}\n" + "{msg desc=\"with link\"}" + "Please click <a href='{$url}'>here</a>." + "{/msg}";
    String expectedPyCode = "translator_impl.render(" + "translator_impl.prepare(" + "###, " + "'Please click {START_LINK}here{END_LINK}.', " + "('START_LINK', 'END_LINK')), " + "{" + "'START_LINK': ''.join(['<a href=\\'',str(data.get('url')),'\\'>']), " + "'END_LINK': '</a>'" + "})";
    assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
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