Search in sources :

Example 26 with ErrorReporter

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

Example 27 with ErrorReporter

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

the class RenderVisitorTest method renderWithDataAndMsgBundle.

/**
 * Renders the given input string (should be a template body) and returns the result.
 *
 * @param templateBody The input string to render (should be a template body).
 * @param data The soy data as a map of variables to objects.
 * @param msgBundle The bundle of translated messages.
 * @return The rendered result.
 * @throws Exception If there's an error.
 */
private String renderWithDataAndMsgBundle(String templateBody, SoyRecord data, @Nullable SoyMsgBundle msgBundle) throws Exception {
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(templateBody).errorReporter(boom).parse().fileSet();
    TemplateNode templateNode = (TemplateNode) SharedTestUtils.getNode(soyTree);
    StringBuilder outputSb = new StringBuilder();
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), outputSb, null, data, TEST_IJ_DATA, Predicates.<String>alwaysFalse(), msgBundle, xidRenamingMap, cssRenamingMap, false);
    rv.exec(templateNode);
    return outputSb.toString();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 28 with ErrorReporter

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

the class CheckTemplateVisibilityTest method testCallPrivateTemplateDifferentFile.

@Test
public void testCallPrivateTemplateDifferentFile() {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forFileContents("{namespace ns}\n" + "/** Private template. */\n" + "{template .foo visibility=\"private\"}\n" + "oops!\n" + "{/template}", "{namespace ns2}\n" + "/** Public template. */\n" + "{template .bar}\n" + "{call ns.foo /}\n" + "{/template}").errorReporter(errorReporter).parse();
    assertThat(errorReporter.getErrors()).hasSize(1);
    assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).isEqualTo("ns.foo has private access in no-path.");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Example 29 with ErrorReporter

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

the class CombineConsecutiveRawTextNodesPassTest method testPathologicalPerformance.

// There used to be a pathological performance issue when merging many raw text nodes, this stress
// test would have timed out under the old implementation but now succeeds quickly.
// Before the fix this test took > 2 minutes
// After the fix it was down to about 1.5s
@Test
public void testPathologicalPerformance() {
    String testFileContent = "{namespace boo}{template .foo}{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = soyTree.getChild(0).getChild(0);
    // Things like this like this could happen in templates with a large number of html tags (e.g.
    // in a literal block). since this is how they would be desugared.
    final int numCopies = 100_000;
    for (int i = 0; i < numCopies; i++) {
        template.addChild(new RawTextNode(0, "<", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "div", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, " ", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "class", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "=", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "foo", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, ">", template.getSourceLocation()));
    }
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo(Strings.repeat("<div class='foo'>", numCopies));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Point(com.google.template.soy.base.SourceLocation.Point) Test(org.junit.Test)

Example 30 with ErrorReporter

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

the class CombineConsecutiveRawTextNodesPassTest method testCombineConsecutiveRawTextNodes.

@Test
public void testCombineConsecutiveRawTextNodes() {
    String testFileContent = "{namespace boo}\n" + "\n" + "/** @param goo */\n" + "{template .foo}\n" + "  Blah{$goo}blah\n" + "{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    template.addChild(new RawTextNode(0, "bleh", template.getSourceLocation()));
    template.addChild(new RawTextNode(0, "bluh", template.getSourceLocation()));
    assertThat(template.numChildren()).isEqualTo(5);
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(3);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("Blah");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) 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