Search in sources :

Example 1 with ErrorReporter

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

the class CanInitOutputVarVisitorTest method runTestHelper.

/**
 * @param indicesToNode Series of indices for walking down to the node we want to test.
 */
private static void runTestHelper(String soyCode, boolean isSameValueAsIsComputableAsJsExprsVisitor, int... indicesToNode) {
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse().fileSet();
    SoyNode node = SharedTestUtils.getNode(soyTree, indicesToNode);
    IsComputableAsJsExprsVisitor icajev = new IsComputableAsJsExprsVisitor();
    CanInitOutputVarVisitor ciovv = new CanInitOutputVarVisitor(icajev);
    assertThat(ciovv.exec(node).equals(icajev.exec(node))).isEqualTo(isSameValueAsIsComputableAsJsExprsVisitor);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyNode(com.google.template.soy.soytree.SoyNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 2 with ErrorReporter

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

the class SoyConformanceTest method getViolations.

private ImmutableList<SoyError> getViolations(String textProto, SoyFileSetParserBuilder builder) {
    ValidatedConformanceConfig config = parseConfigProto(textProto);
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    builder.setConformanceConfig(config).errorReporter(errorReporter).parse();
    ImmutableList<SoyError> errors = errorReporter.getErrors();
    Set<SoyErrorKind> expectedErrorKinds = new HashSet<>();
    for (RuleWithWhitelists rule : config.getRules()) {
        expectedErrorKinds.add(rule.getRule().error);
    }
    for (SoyError actualError : errors) {
        if (!expectedErrorKinds.contains(actualError.errorKind())) {
            throw new AssertionError("Found non-conformance error!: " + actualError + "\nexpected kind to be one of: " + expectedErrorKinds + " actual is: " + actualError.errorKind());
        }
    }
    return errors;
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyErrorKind(com.google.template.soy.error.SoyErrorKind) SoyError(com.google.template.soy.error.SoyError) HashSet(java.util.HashSet)

Example 3 with ErrorReporter

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

the class BytecodeCompiler method compile.

/**
 * Compiles all the templates in the given registry.
 *
 * @param registry All the templates to compile
 * @param developmentMode Whether or not we are in development mode. In development mode we
 *     compile classes lazily
 * @param reporter The error reporter
 * @return CompiledTemplates or {@code absent()} if compilation fails, in which case errors will
 *     have been reported to the error reporter.
 */
public static Optional<CompiledTemplates> compile(final TemplateRegistry registry, boolean developmentMode, ErrorReporter reporter) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    ErrorReporter.Checkpoint checkpoint = reporter.checkpoint();
    if (reporter.errorsSince(checkpoint)) {
        return Optional.absent();
    }
    CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry);
    if (developmentMode) {
        CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new CompilingClassLoader(compilerRegistry));
        // TODO(lukes): consider spawning a thread to load all the generated classes in the background
        return Optional.of(templates);
    }
    // TODO(lukes): once most internal users have moved to precompilation eliminate this and just
    // use the 'developmentMode' path above.  This hybrid only makes sense for production services
    // that are doing runtime compilation.  Hopefully, this will become an anomaly.
    List<ClassData> classes = compileTemplates(compilerRegistry, reporter, new CompilerListener<List<ClassData>>() {

        final List<ClassData> compiledClasses = new ArrayList<>();

        int numBytes = 0;

        int numFields = 0;

        int numDetachStates = 0;

        @Override
        public void onCompile(ClassData clazz) {
            numBytes += clazz.data().length;
            numFields += clazz.numberOfFields();
            numDetachStates += clazz.numberOfDetachStates();
            compiledClasses.add(clazz);
        }

        @Override
        public List<ClassData> getResult() {
            logger.log(Level.INFO, "Compilation took {0}\n" + "     templates: {1}\n" + "       classes: {2}\n" + "         bytes: {3}\n" + "        fields: {4}\n" + "  detachStates: {5}", new Object[] { stopwatch.toString(), registry.getAllTemplates().size(), compiledClasses.size(), numBytes, numFields, numDetachStates });
            return compiledClasses;
        }
    });
    if (reporter.errorsSince(checkpoint)) {
        return Optional.absent();
    }
    CompiledTemplates templates = new CompiledTemplates(compilerRegistry.getDelegateTemplateNames(), new MemoryClassLoader(classes));
    stopwatch.reset().start();
    templates.loadAll(compilerRegistry.getTemplateNames());
    logger.log(Level.INFO, "Loaded all classes in {0}", stopwatch);
    return Optional.of(templates);
}
Also used : Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) ErrorReporter(com.google.template.soy.error.ErrorReporter) ClassData(com.google.template.soy.jbcsrc.internal.ClassData) MemoryClassLoader(com.google.template.soy.jbcsrc.internal.MemoryClassLoader) ArrayList(java.util.ArrayList) List(java.util.List) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 4 with ErrorReporter

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

the class TemplateParserTest method testMultipleErrors.

@Test
public void testMultipleErrors() throws ParseException {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    parseTemplateContent(// Invalid callee name
    "{call .123 /}\n" + // Invalid callee name
    "{delcall 456 /}\n" + // Invalid foreach var
    "{for foo in bar}{/for}\n" + // Missing let var
    "{let /}\n", errorReporter);
    List<SoyError> errors = errorReporter.getErrors();
    assertThat(errors).hasSize(4);
    assertThat(errors.get(0).message()).isEqualTo("parse error at '1': expected identifier");
    assertThat(errors.get(1).message()).isEqualTo("parse error at '4': expected identifier or .");
    assertThat(errors.get(2).message()).isEqualTo("parse error at 'foo': expected variable");
    assertThat(errors.get(3).message()).isEqualTo("parse error at '/}': expected variable");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyError(com.google.template.soy.error.SoyError) Test(org.junit.Test)

Example 5 with ErrorReporter

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

the class TemplateParserTest method testParseMsgStmtWithIf.

@Test
public void testParseMsgStmtWithIf() throws Exception {
    ErrorReporter errorReporter = ErrorReporter.createForTest();
    parseTemplateContent("{@param boo :?}\n" + "  {msg desc=\"Blah.\"}\n" + "    Blah \n" + "    {if $boo}\n" + "      bleh\n" + "    {else}\n" + "      bluh\n" + "    {/if}\n" + "    .\n" + "  {/msg}\n", errorReporter);
    assertThat(errorReporter.getErrors()).isNotEmpty();
}
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