blob: fc228951a16b5f8f3085691f752549c2394a9938 [file] [log] [blame]
// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8;
import static org.junit.Assert.assertTrue;
import com.android.tools.r8.utils.ListUtils;
import java.util.ArrayList;
import java.util.List;
// Helper to check that a particular error occurred.
class DiagnosticsChecker implements DiagnosticsHandler {
public List<Diagnostic> errors = new ArrayList<>();
public List<Diagnostic> warnings = new ArrayList<>();
public List<Diagnostic> infos = new ArrayList<>();
@Override
public void error(Diagnostic error) {
errors.add(error);
}
@Override
public void warning(Diagnostic warning) {
warnings.add(warning);
}
@Override
public void info(Diagnostic info) {
infos.add(info);
}
public interface FailingRunner {
void run(DiagnosticsHandler handler) throws CompilationFailedException;
}
public static void checkErrorsContains(String snippet, FailingRunner runner)
throws CompilationFailedException {
DiagnosticsChecker handler = new DiagnosticsChecker();
try {
runner.run(handler);
} catch (CompilationFailedException e) {
System.out.println("Expecting match for '" + snippet + "'");
System.out.println("StdErr:\n" + handler.errors);
assertTrue(
"Expected to find snippet '"
+ snippet
+ "' in error messages:\n"
+ String.join("\n", ListUtils.map(handler.errors, Diagnostic::getDiagnosticMessage)),
handler.errors.stream().anyMatch(d -> d.getDiagnosticMessage().contains(snippet)));
throw e;
}
}
}