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);
}
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;
}
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);
}
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");
}
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();
}
Aggregations