Search in sources :

Example 61 with ErrorReporter

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

the class VerifyPhnameAttrOnlyOnPlaceholdersVisitorTest method assertInvalidSoyCode.

private void assertInvalidSoyCode(String soyCode) {
    ErrorReporter errors = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(errors).parse();
    assertThat(Iterables.getOnlyElement(errors.getErrors()).message()).contains("'phname' attributes are only valid inside '{msg...' tags.");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter)

Example 62 with ErrorReporter

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

the class ContextualAutoescaperTest method rewrite.

public SoyFileNode rewrite(String... inputs) {
    ErrorReporter reporter = ErrorReporter.createForTest();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(inputs).errorReporter(reporter).allowUnboundGlobals(true).addPrintDirectives(SOY_PRINT_DIRECTIVES).runAutoescaper(true).parse().fileSet();
    if (!reporter.getErrors().isEmpty()) {
        SoyError soyError = reporter.getErrors().get(0);
        String message = soyError.message();
        if (message.startsWith(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX)) {
            // Grab the part after the prefix (and the "- " used for indentation).
            message = message.substring(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX.length() + 2);
            // reporter, we can stop throwing and simply add explicit checks in the cases.
            throw new RewriteError(soyError, message);
        } else {
            throw new IllegalStateException("Unexpected error: " + message);
        }
    }
    return soyTree.getChild(0);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) SoyError(com.google.template.soy.error.SoyError)

Example 63 with ErrorReporter

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

the class AddHtmlCommentsForDebugPassTest method runPass.

/**
 * Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
 * a map that contains pairs of template names and rewritten template body.
 */
private static ImmutableMap<String, String> runPass(String input, String fileName) {
    String soyFile = "{namespace ns}" + input;
    IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
    TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
    // We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
    new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
    new AddHtmlCommentsForDebugPass().run(soyTree, registry);
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (TemplateNode node : soyTree.getChild(0).getChildren()) {
        StringBuilder sb = new StringBuilder();
        node.appendSourceStringForChildren(sb);
        String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
        builder.put(templateName, sb.toString());
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 64 with ErrorReporter

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

the class CheckDelegatesVisitorTest method assertInvalidSoyFiles.

private void assertInvalidSoyFiles(String expectedErrorMsgSubstr, String... soyFileContents) {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forFileContents(soyFileContents).errorReporter(errorReporter).parse();
    assertThat(errorReporter.getErrors()).hasSize(1);
    assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).isEqualTo(expectedErrorMsgSubstr);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter)

Example 65 with ErrorReporter

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

the class CheckFunctionCallsVisitorTest method assertFunctionCallsInvalid.

private void assertFunctionCallsInvalid(SyntaxVersion declaredSyntaxVersion, String errorMessage, String... lines) {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    SoyFileSetParserBuilder.forFileContents(Joiner.on('\n').join(lines)).declaredSyntaxVersion(declaredSyntaxVersion).errorReporter(errorReporter).parse();
    assertThat(errorReporter.getErrors()).hasSize(1);
    assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).contains(errorMessage);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter)

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