Search in sources :

Example 31 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 32 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 33 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 34 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 35 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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

SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)106 Test (org.junit.Test)81 TemplateNode (com.google.template.soy.soytree.TemplateNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)29 ErrorReporter (com.google.template.soy.error.ErrorReporter)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 SoyNode (com.google.template.soy.soytree.SoyNode)6 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 PrintNode (com.google.template.soy.soytree.PrintNode)5 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 ImmutableList (com.google.common.collect.ImmutableList)2