Search in sources :

Example 6 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testRewriteWithPlural.

@Test
public void testRewriteWithPlural() {
    String soyCode = "" + "{@param num : ?}\n" + "{@param userGender : ?}\n" + "{msg genders=\"$userGender\" desc=\"...\"}\n" + "  {plural $num}{case 1}Send it{default}Send {$num}{/plural}\n" + "{/msg}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    // After.
    MsgNode msgAfterRewrite = (MsgNode) SharedTestUtils.getNode(soyTree, 0, 0);
    assertEquals(// Note: Still has genders="..." in command text.
    "{msg desc=\"...\" genders=\"$userGender\"}" + "{select $userGender}" + "{case 'female'}{plural $num}{case 1}Send it{default}Send {$num}{/plural}" + "{case 'male'}{plural $num}{case 1}Send it{default}Send {$num}{/plural}" + "{default}{plural $num}{case 1}Send it{default}Send {$num}{/plural}" + "{/select}", msgAfterRewrite.toSourceString());
    // ------ Test that it has same msg id as equivalent msg using 'select'. ------
    String soyCodeUsingSelect = "" + "{@param num : ?}\n" + "{@param userGender : ?}\n" + "{msg desc=\"...\"}\n" + "  {select $userGender}\n" + "    {case 'female'}{plural $num}{case 1}Send it{default}Send {$num}{/plural}\n" + "    {case 'male'}{plural $num}{case 1}Send it{default}Send {$num}{/plural}\n" + "    {default}{plural $num}{case 1}Send it{default}Send {$num}{/plural}\n" + "  {/select}\n" + "{/msg}\n";
    SoyFileSetNode soyTreeUsingSelect = SoyFileSetParserBuilder.forTemplateContents(soyCodeUsingSelect).parse().fileSet();
    MsgNode msgUsingSelect = (MsgNode) SharedTestUtils.getNode(soyTreeUsingSelect, 0, 0);
    assertThat(MsgUtils.computeMsgIdForDualFormat(msgAfterRewrite)).isEqualTo(MsgUtils.computeMsgIdForDualFormat(msgUsingSelect));
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 7 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testRewriteWithThreeGendersAndNoncollidingSelectVarNames.

@Test
public void testRewriteWithThreeGendersAndNoncollidingSelectVarNames() {
    String soyCode = "" + "{@param target : ?}\n" + "{msg genders=\"$ij.userGender, $target[0].gender, $target[1].gender\" " + "desc=\"...\"}\n" + "  You starred {$target[0].name}'s photo in {$target[1].name}'s album.\n" + "{/msg}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    // After.
    MsgNode msgAfterRewrite = (MsgNode) SharedTestUtils.getNode(soyTree, 0, 0);
    String expectedInnerSelectSrc = "" + "{select $target[1].gender phname=\"TARGET_1_GENDER\"}" + "{case 'female'}You starred {$target[0].name}'s photo in {$target[1].name}'s album." + "{case 'male'}You starred {$target[0].name}'s photo in {$target[1].name}'s album." + "{default}You starred {$target[0].name}'s photo in {$target[1].name}'s album." + "{/select}";
    String expectedMsgSrc = "" + // Note: Still has genders="..." in command text.
    "{msg desc=\"...\" genders=\"$ij.userGender, $target[0].gender, $target[1].gender\"}" + "{select $ij.userGender}" + // note: 'phname' not specified because generated is same
    "{case 'female'}" + "{select $target[0].gender phname=\"TARGET_0_GENDER\"}" + "{case 'female'}" + expectedInnerSelectSrc + "{case 'male'}" + expectedInnerSelectSrc + "{default}" + expectedInnerSelectSrc + "{/select}" + "{case 'male'}" + "{select $target[0].gender phname=\"TARGET_0_GENDER\"}" + "{case 'female'}" + expectedInnerSelectSrc + "{case 'male'}" + expectedInnerSelectSrc + "{default}" + expectedInnerSelectSrc + "{/select}" + "{default}" + "{select $target[0].gender phname=\"TARGET_0_GENDER\"}" + "{case 'female'}" + expectedInnerSelectSrc + "{case 'male'}" + expectedInnerSelectSrc + "{default}" + expectedInnerSelectSrc + "{/select}" + "{/select}";
    assertThat(msgAfterRewrite.toSourceString()).isEqualTo(expectedMsgSrc);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 8 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testRewriteSimple.

@Test
public void testRewriteSimple() {
    String soyCode = "" + "{@param userGender : ?}\n" + "{msg genders=\"$userGender\" desc=\"Button text.\"}\n" + "  Save\n" + "{/msg}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    // After.
    MsgNode msgAfterRewrite = (MsgNode) SharedTestUtils.getNode(soyTree, 0, 0);
    assertEquals(// Note: Still has genders="..." in command text.
    "{msg desc=\"Button text.\" genders=\"$userGender\"}" + "{select $userGender}{case 'female'}Save{case 'male'}Save{default}Save{/select}", msgAfterRewrite.toSourceString());
    // ------ Test that it has same msg id as equivalent msg using 'select'. ------
    String soyCodeUsingSelect = "" + "{@param userGender : ?}\n" + "{msg desc=\"Button text.\"}\n" + "  {select $userGender}\n" + "    {case 'female'}Save\n" + "    {case 'male'}Save\n" + "    {default}Save\n" + "  {/select}\n" + "{/msg}\n";
    SoyFileSetNode soyTreeUsingSelect = SoyFileSetParserBuilder.forTemplateContents(soyCodeUsingSelect).parse().fileSet();
    MsgNode msgUsingSelect = (MsgNode) SharedTestUtils.getNode(soyTreeUsingSelect, 0, 0);
    assertThat(MsgUtils.computeMsgIdForDualFormat(msgAfterRewrite)).isEqualTo(MsgUtils.computeMsgIdForDualFormat(msgUsingSelect));
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 9 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testRewriteWithGenderAndSelect.

@Test
public void testRewriteWithGenderAndSelect() {
    String soyCode = "" + "{@param userGender : ?}\n" + "{@param targetGender : ?}\n" + "{msg genders=\"$userGender\" desc=\"Button text.\"}\n" + "  {select $targetGender}\n" + "    {case 'female'}Reply to her\n" + "    {case 'male'}Reply to him\n" + "    {default}Reply to them\n" + "  {/select}\n" + "{/msg}\n";
    String expandedSoyCode = "{msg desc=\"Button text.\" genders=\"$userGender\"}{select $userGender}" + "{case 'female'}{select $targetGender}{case 'female'}Reply to her" + "{case 'male'}Reply to him{default}Reply to them{/select}" + "{case 'male'}{select $targetGender}{case 'female'}Reply to her" + "{case 'male'}Reply to him{default}Reply to them{/select}" + "{default}{select $targetGender}{case 'female'}Reply to her" + "{case 'male'}Reply to him{default}Reply to them{/select}{/select}";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    // After.
    MsgNode msgAfterRewrite = (MsgNode) SharedTestUtils.getNode(soyTree, 0, 0);
    assertThat(msgAfterRewrite.toSourceString()).isEqualTo(expandedSoyCode);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 10 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testRewriteWithGenderSelectPlural.

@Test
public void testRewriteWithGenderSelectPlural() {
    String soyCode = "" + "{@param n : ?}\n" + "{@param userGender : ?}\n" + "{@param targetGender : ?}\n" + "{msg genders=\"$userGender\" desc=\"...\"}\n" + "  {select $targetGender}\n" + "    {case 'female'}\n" + "      {plural $n}{case 1}Send it to her{default}Send {$n} to her{/plural}\n" + "    {default}\n" + "      {plural $n}{case 1}Send it to them{default}Send {$n} to them{/plural}\n" + "  {/select}\n" + "{/msg}\n";
    // Note: Still has genders="..." in command text.
    String expandedSoyCode = "{msg desc=\"...\" genders=\"$userGender\"}{select $userGender}{case 'female'}" + "{select $targetGender}{case 'female'}{plural $n}{case 1}Send it to her{default}" + "Send {$n} to her{/plural}{default}{plural $n}{case 1}Send it to them{default}" + "Send {$n} to them{/plural}{/select}{case 'male'}" + "{select $targetGender}{case 'female'}{plural $n}{case 1}Send it to her{default}" + "Send {$n} to her{/plural}{default}{plural $n}{case 1}Send it to them{default}" + "Send {$n} to them{/plural}{/select}{default}" + "{select $targetGender}{case 'female'}{plural $n}{case 1}Send it to her{default}" + "Send {$n} to her{/plural}{default}{plural $n}{case 1}Send it to them{default}" + "Send {$n} to them{/plural}{/select}{/select}";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    // After.
    MsgNode msgAfterRewrite = (MsgNode) SharedTestUtils.getNode(soyTree, 0, 0);
    assertThat(msgAfterRewrite.toSourceString()).isEqualTo(expandedSoyCode);
    // ------ Test that it has same msg id as equivalent msg using 'select'. ------
    String soyCodeUsingSelect = "" + "{@param n : ?}\n" + "{@param userGender : ?}\n" + "{@param targetGender : ?}\n" + "{msg desc=\"...\"}\n" + "  {select $userGender}\n" + "    {case 'female'}\n" + "      {select $targetGender}\n" + "        {case 'female'}\n" + "          {plural $n}{case 1}Send it to her{default}Send {$n} to her{/plural}\n" + "        {default}\n" + "          {plural $n}{case 1}Send it to them{default}Send {$n} to them{/plural}\n" + "      {/select}\n" + "    {case 'male'}\n" + "      {select $targetGender}\n" + "        {case 'female'}\n" + "          {plural $n}{case 1}Send it to her{default}Send {$n} to her{/plural}\n" + "        {default}\n" + "          {plural $n}{case 1}Send it to them{default}Send {$n} to them{/plural}\n" + "      {/select}\n" + "    {default}\n" + "      {select $targetGender}\n" + "        {case 'female'}\n" + "          {plural $n}{case 1}Send it to her{default}Send {$n} to her{/plural}\n" + "        {default}\n" + "          {plural $n}{case 1}Send it to them{default}Send {$n} to them{/plural}\n" + "      {/select}\n" + "  {/select}\n" + "{/msg}\n";
    SoyFileSetNode soyTreeUsingSelect = SoyFileSetParserBuilder.forTemplateContents(soyCodeUsingSelect).parse().fileSet();
    MsgNode msgUsingSelect = (MsgNode) SharedTestUtils.getNode(soyTreeUsingSelect, 0, 0);
    assertThat(MsgUtils.computeMsgIdForDualFormat(msgAfterRewrite)).isEqualTo(MsgUtils.computeMsgIdForDualFormat(msgUsingSelect));
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Aggregations

MsgNode (com.google.template.soy.soytree.MsgNode)22 Test (org.junit.Test)17 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)13 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)7 RawTextNode (com.google.template.soy.soytree.RawTextNode)6 ErrorReporter (com.google.template.soy.error.ErrorReporter)5 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)4 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)4 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)2 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)2 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 MsgPartsAndIds (com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds)1