Search in sources :

Example 11 with ErrorReporter

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

the class TemplateSubject method causesError.

public TemplateSubject causesError(SoyErrorKind error) {
    ErrorReporter errorReporter = doParse();
    SoyError report = getFirstReport(error, errorReporter);
    if (report == null) {
        failWithRawMessage("%s should have failed to parse with <%s>, instead had errors: %s", actualAsString(), error, errorReporter.getErrors());
    }
    actualSourceLocation = report.location();
    return this;
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyError(com.google.template.soy.error.SoyError)

Example 12 with ErrorReporter

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

the class TemplateSubject method causesError.

public TemplateSubject causesError(String message) {
    ErrorReporter errorReporter = doParse();
    SoyError report = getFirstReport(message, errorReporter);
    if (report == null) {
        failWithRawMessage("%s should have failed to parse with <%s>, instead had errors: %s", actualAsString(), message, errorReporter.getErrors());
    }
    actualSourceLocation = report.location();
    return this;
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyError(com.google.template.soy.error.SoyError)

Example 13 with ErrorReporter

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

the class TemplateSubject method doParse.

private ErrorReporter doParse() {
    SoyFileSupplier sourceFile = SoyFileSupplier.Factory.create("{namespace test}\n" + "{template .foo}\n" + actual() + "\n" + "{/template}", SoyFileKind.SRC, "example.soy");
    ErrorReporter errorReporter = ErrorReporter.create(ImmutableMap.of(sourceFile.getFilePath(), sourceFile));
    SoyFileSetNode fileSet = SoyFileSetParserBuilder.forSuppliers(sourceFile).errorReporter(errorReporter).parse().fileSet();
    fileNode = fileSet.numChildren() == 1 ? fileSet.getChild(0) : null;
    return errorReporter;
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier)

Example 14 with ErrorReporter

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

the class SoyUtils method parseCompileTimeGlobals.

/**
 * Parses a globals file in the format created by {@link #generateCompileTimeGlobalsFile} into a
 * map from global name to primitive value.
 *
 * @param inputSource A source that returns a reader for the globals file.
 * @return The parsed globals map.
 * @throws IOException If an error occurs while reading the globals file.
 * @throws IllegalStateException If the globals file is not in the correct format.
 */
public static ImmutableMap<String, PrimitiveData> parseCompileTimeGlobals(CharSource inputSource) throws IOException {
    Builder<String, PrimitiveData> compileTimeGlobalsBuilder = ImmutableMap.builder();
    ErrorReporter errorReporter = ErrorReporter.exploding();
    try (BufferedReader reader = inputSource.openBufferedStream()) {
        int lineNum = 1;
        for (String line = reader.readLine(); line != null; line = reader.readLine(), ++lineNum) {
            if (line.startsWith("//") || line.trim().length() == 0) {
                continue;
            }
            SourceLocation sourceLocation = new SourceLocation("globals", lineNum, 1, lineNum, 1);
            Matcher matcher = COMPILE_TIME_GLOBAL_LINE.matcher(line);
            if (!matcher.matches()) {
                errorReporter.report(sourceLocation, INVALID_FORMAT, line);
                continue;
            }
            String name = matcher.group(1);
            String valueText = matcher.group(2).trim();
            ExprNode valueExpr = SoyFileParser.parseExprOrDie(valueText);
            // TODO: Consider allowing non-primitives (e.g. list/map literals).
            if (!(valueExpr instanceof PrimitiveNode)) {
                if (valueExpr instanceof GlobalNode || valueExpr instanceof VarRefNode) {
                    errorReporter.report(sourceLocation, INVALID_VALUE, valueExpr.toSourceString());
                } else {
                    errorReporter.report(sourceLocation, NON_PRIMITIVE_VALUE, valueExpr.toSourceString());
                }
                continue;
            }
            // Default case.
            compileTimeGlobalsBuilder.put(name, InternalValueUtils.convertPrimitiveExprToData((PrimitiveNode) valueExpr));
        }
    }
    return compileTimeGlobalsBuilder.build();
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ExprNode(com.google.template.soy.exprtree.ExprNode) PrimitiveNode(com.google.template.soy.exprtree.ExprNode.PrimitiveNode) PrimitiveData(com.google.template.soy.data.restricted.PrimitiveData) ErrorReporter(com.google.template.soy.error.ErrorReporter) VarRefNode(com.google.template.soy.exprtree.VarRefNode) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 15 with ErrorReporter

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

the class HtmlRewritePassTest method testUnmatchedContextChangingCloseTagUnquotedAttributeValue.

@Test
public void testUnmatchedContextChangingCloseTagUnquotedAttributeValue() {
    // matched script is fine
    runPass("<script>xxx</script>");
    // unmatched closing div is fine.
    runPass("</div>");
    for (String tag : new String[] { "</script>", "</style>", "</title>", "</textarea>", "</xmp>" }) {
        ErrorReporter errorReporter = ErrorReporter.createForTest();
        runPass(tag, errorReporter);
        assertThat(Iterables.getOnlyElement(errorReporter.getErrors()).message()).named("error message for: %s", tag).isEqualTo("Unexpected close tag for context-changing tag.");
    }
}
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