Search in sources :

Example 16 with ErrorReporter

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

the class HtmlRewritePassTest method testHtmlCommentWithControlFlow.

@Test
public void testHtmlCommentWithControlFlow() {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    TemplateNode node;
    // Control flow structure should be preserved.
    node = runPass("<!-- {if $foo} foo {else} bar {/if} -->", errorReporter);
    assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", "  RAW_TEXT_NODE", "  IF_NODE", "    IF_COND_NODE", "      RAW_TEXT_NODE", "    IF_ELSE_NODE", "      RAW_TEXT_NODE", "  RAW_TEXT_NODE", ""));
    assertThatSourceString(node).isEqualTo("<!-- {if $foo} foo {else} bar {/if} -->");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Example 17 with ErrorReporter

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

the class HtmlRewritePassTest method testBadHtmlComment.

@Test
public void testBadHtmlComment() {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    // These are examples that we haven't closed the HTML comments.
    for (String text : new String[] { "<!--", "<!-- --", "<!--->" }) {
        errorReporter = ErrorReporter.createForTest();
        runPass(text, errorReporter);
        assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).named("error message for: %s", text).isEqualTo("template changes context from 'pcdata' to 'html comment'. " + "Did you forget to close the html comment?");
    }
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Example 18 with ErrorReporter

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

the class ResolveExpressionTypesVisitorTest method testMapLiteral_duplicateKeys.

@Test
public void testMapLiteral_duplicateKeys() {
    ErrorReporter reporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $map: map('a': 1, 'a': 2)/}")).declaredSyntaxVersion(SyntaxVersion.V2_0).errorReporter(reporter).typeRegistry(TYPE_REGISTRY).parse().fileSet();
    assertThat(Iterables.getOnlyElement(reporter.getErrors()).message()).isEqualTo("Map literals with duplicate keys are not allowed.  Duplicate key: 'a'");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Example 19 with ErrorReporter

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

the class RewriteGenderMsgsVisitorTest method testMaxTwoGendersWithPlural.

@Test
public void testMaxTwoGendersWithPlural() {
    String soyCode = "" + "{@param userGender : ?}\n" + "{@param gender1 : ?}\n" + "{@param gender2 : ?}\n" + "{@param numPhotos : ?}\n" + "{@param name1 : ?}\n" + "{@param name2 : ?}\n" + "{msg genders=\"$userGender, $gender1, $gender2\" desc=\"\"}\n" + "  {plural $numPhotos}\n" + "    {case 1}Find {$name1}'s face in {$name2}'s photo\n" + "    {default}Find {$name1}'s face in {$name2}'s photos\n" + "  {/plural}\n" + "{/msg}\n";
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(errorReporter).parse().fileSet();
    assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).isEqualTo("A msg with 'plural' can contain at most 2 gender expressions between the " + "'genders' attribute and 'select' command (otherwise, combinatorial explosion " + "would cause a gigantic generated message).");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) Test(org.junit.Test)

Example 20 with ErrorReporter

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

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