Search in sources :

Example 31 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgNamespacedSoyExpression.

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

Example 32 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgMultipleSoyExpressions.

@Test
public void testMsgMultipleSoyExpressions() {
    String soyCode = "{@param greet:?}\n" + "{@param username:?}\n" + "{msg desc=\"var placeholder\"}" + "{$greet} {$username}" + "{/msg}\n";
    String expectedPyCode = "translator_impl.render(" + "translator_impl.prepare(" + "###, " + "'{GREET} {USERNAME}', " + "('GREET', 'USERNAME')), " + "{" + "'GREET': str(data.get('greet')), " + "'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 33 with PyExpr

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

the class GenPyExprsVisitorTest method testIf_nested.

@Test
public void testIf_nested() {
    String soyNodeCode = "{@param boo:?}\n" + "{@param goo:?}\n" + "{if $boo}\n" + "  {if $goo}\n" + "    Blah\n" + "  {/if}\n" + "{else}\n" + "  Bleh\n" + "{/if}\n";
    String expectedPyExprText = "('Blah' if data.get('goo') else '') if data.get('boo') else 'Bleh'";
    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 34 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgOnlyLiteralWithBraces.

@Test
public void testMsgOnlyLiteralWithBraces() {
    // Should escape '{' and '}' in format string.
    // @see https://docs.python.org/2/library/string.html#formatstrings
    String soyCode = "{msg meaning=\"verb\" desc=\"The word 'Archive' used as a verb.\"}" + "{lb}Archive{rb}" + "{/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 35 with PyExpr

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

the class GenPyExprsVisitorTest method testMsgWithPluralWithGender.

@Test
public void testMsgWithPluralWithGender() {
    String soyCode = "{@param people:?}\n" + "{msg genders=\"$people[0]?.gender, $people[1]?.gender\" desc=\"plural w offsets\"}\n" + "  {plural length($people)}\n" + "    {case 1}{$people[0].name} is attending\n" + "    {case 2}{$people[0].name} and {$people[1]?.name} are attending\n" + "    {case 3}{$people[0].name}, {$people[1]?.name}, and 1 other are attending\n" + "    {default}{$people[0].name}, {$people[1]?.name}, and length($people) others\n" + "  {/plural}\n" + "{/msg}\n";
    String expectedPyCode = "translator_impl.render_icu" + "(translator_impl.prepare_icu" + "(###, " + "'{PEOPLE_0_GENDER,select," + "female{{PEOPLE_1_GENDER,select," + "female{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "male{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "other{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}}" + "}" + "}" + "male{{PEOPLE_1_GENDER,select," + "female{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "male{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "other{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}}" + "}" + "}" + "other{{PEOPLE_1_GENDER,select," + "female{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "male{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}" + "}" + "other{{NUM,plural," + "=1{{NAME_1} is attending}" + "=2{{NAME_1} and {NAME_2} are attending}" + "=3{{NAME_1}, {NAME_2}, and 1 other are attending}" + "other{{NAME_1}, {NAME_2}, and length($people) others}}}" + "}" + "}" + "}', " + "('PEOPLE_0_GENDER', 'PEOPLE_1_GENDER', 'NUM', 'NAME_1', 'NAME_2')), " + "{" + "'PEOPLE_0_GENDER': None " + "if runtime.key_safe_data_access(data.get('people'), 0) is None " + "else runtime.key_safe_data_access(data.get('people'), 0).get('gender'), " + "'PEOPLE_1_GENDER': None " + "if runtime.key_safe_data_access(data.get('people'), 1) is None " + "else runtime.key_safe_data_access(data.get('people'), 1).get('gender'), " + "'NUM': len(data.get('people')), " + "'NAME_1': str(runtime.key_safe_data_access(data.get('people'), 0).get('name')), " + "'NAME_2': str(None " + "if runtime.key_safe_data_access(data.get('people'), 1) is None " + "else runtime.key_safe_data_access(data.get('people'), 1).get('name'))" + "}" + ")";
    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