Search in sources :

Example 21 with ErrorReporter

use of com.google.template.soy.error.ErrorReporter 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 22 with ErrorReporter

use of com.google.template.soy.error.ErrorReporter in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testErrorIfCannotGenNoncollidingBaseNames.

@Test
public void testErrorIfCannotGenNoncollidingBaseNames() {
    String soyCode = "" + "{@param userGender : ?}\n" + "{@param gender : ?}\n" + "{@param owner : ?}\n" + "{msg genders=\"$userGender, $gender\" desc=\"Button text.\"}\n" + "  You joined {$owner}'s community.\n" + "{/msg}\n";
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(errorReporter).parse().fileSet();
    List<String> actualMessages = new ArrayList<>();
    for (SoyError error : errorReporter.getErrors()) {
        actualMessages.add(error.message());
    }
    assertThat(actualMessages).contains("Cannot generate noncolliding base names for vars. " + "Colliding expressions: '$gender' and '$userGender'. " + "Add explicit base names with the 'phname' attribute.");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) ArrayList(java.util.ArrayList) SoyError(com.google.template.soy.error.SoyError) Test(org.junit.Test)

Example 23 with ErrorReporter

use of com.google.template.soy.error.ErrorReporter 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 24 with ErrorReporter

use of com.google.template.soy.error.ErrorReporter 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 25 with ErrorReporter

use of com.google.template.soy.error.ErrorReporter in project closure-templates by google.

the class RewriteGenderMsgsVisitorTest method testMaxThreeGendersWithNestedSelect.

@Test
public void testMaxThreeGendersWithNestedSelect() {
    String soyCode = "" + "{@param userGender : ?}\n" + "{@param targetGender1 : ?}\n" + "{@param targetGender2 : ?}\n" + "{@param groupOwnerGender : ?}\n" + "{@param targetName1 : ?}\n" + "{@param targetName2 : ?}\n" + "{@param groupOwnerName : ?}\n" + "{msg genders=\"$userGender, $targetGender2\" desc=\"...\"}\n" + "  {select $groupOwnerGender}\n" + "    {case 'female'}\n" + "      {select $targetGender1}\n" + "        {case 'female'}\n" + "          {$targetName1} has asked to join {$groupOwnerName}'s group. If you accept," + "          {sp}{$groupOwnerName} will receive an email in her inbox, and{sp}" + "          {$targetName1} will receive a notification on her account. {$targetName2}" + "          {sp}and other members will not receive a notification.\n" + "        {default}\n" + "          {$targetName1} has asked to join {$groupOwnerName}'s group. If you accept," + "          {sp}{$groupOwnerName} will receive an email in her inbox, and{sp}" + "          {$targetName1} will receive a notification on their account." + "          {$targetName2} and other members will not receive a notification.\n" + "      {/select}\n" + "    {default}\n" + "      {select $targetGender1}\n" + "        {case 'female'}\n" + "          {$targetName1} has asked to join {$groupOwnerName}'s group. If you accept," + "          {sp}{$groupOwnerName} will receive an email in their inbox, and{sp}" + "          {$targetName1} will receive a notification on her account. {$targetName2}" + "          {sp}and other members will not receive a notification.\n" + "        {default}\n" + "          {$targetName1} has asked to join {$groupOwnerName}'s group. If you accept," + "          {sp}{$groupOwnerName} will receive an email in their inbox, and{sp}" + "          {$targetName1} will receive a notification on their account." + "          {$targetName2} and other members will not receive a notification.\n" + "      {/select}\n" + "  {/select}\n" + "{/msg}\n";
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(errorReporter).parse().fileSet();
    assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).isEqualTo("A message can only contain at most 3 genders between the 'genders' attribute and " + "'select' command.");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Aggregations

ErrorReporter (com.google.template.soy.error.ErrorReporter)70 Test (org.junit.Test)45 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)19 TemplateNode (com.google.template.soy.soytree.TemplateNode)11 SoyError (com.google.template.soy.error.SoyError)7 MsgNode (com.google.template.soy.soytree.MsgNode)5 RawTextNode (com.google.template.soy.soytree.RawTextNode)3 SoyNode (com.google.template.soy.soytree.SoyNode)3 ArrayList (java.util.ArrayList)3 Point (com.google.template.soy.base.SourceLocation.Point)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)2 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)2 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)2 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)1 SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)1 SoyJarFileWriter (com.google.template.soy.base.internal.SoyJarFileWriter)1