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