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