Update testing framework

* Move and extend the diagnostics testing
* Move the inner class collection to ToolHelper

Change-Id: I8808a7f754d1056c64939f755a34bb09cc95de14
diff --git a/src/test/java/com/android/tools/r8/TestBuilder.java b/src/test/java/com/android/tools/r8/TestBuilder.java
index 61bf1b3..c67c05c 100644
--- a/src/test/java/com/android/tools/r8/TestBuilder.java
+++ b/src/test/java/com/android/tools/r8/TestBuilder.java
@@ -3,16 +3,12 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8;
 
-import static com.android.tools.r8.utils.FileUtils.CLASS_EXTENSION;
-
 import com.android.tools.r8.debug.DebugTestConfig;
 import com.android.tools.r8.utils.ListUtils;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
 
 public abstract class TestBuilder<RR extends TestRunResult, T extends TestBuilder<RR, T>> {
 
@@ -92,14 +88,6 @@
   }
 
   static Collection<Path> getFilesForInnerClasses(Collection<Class<?>> classes) throws IOException {
-    Set<Path> paths = new HashSet<>();
-    for (Class clazz : classes) {
-      Path path = ToolHelper.getClassFileForTestClass(clazz);
-      String prefix = path.toString().replace(CLASS_EXTENSION, "$");
-      paths.addAll(
-          ToolHelper.getClassFilesForTestDirectory(
-              path.getParent(), p -> p.toString().startsWith(prefix)));
-    }
-    return paths;
+    return ToolHelper.getClassFilesForInnerClasses(classes);
   }
 }
diff --git a/src/test/java/com/android/tools/r8/TestCompileResult.java b/src/test/java/com/android/tools/r8/TestCompileResult.java
index 0c84e7c..9700d56 100644
--- a/src/test/java/com/android/tools/r8/TestCompileResult.java
+++ b/src/test/java/com/android/tools/r8/TestCompileResult.java
@@ -4,9 +4,6 @@
 package com.android.tools.r8;
 
 import static com.android.tools.r8.TestBase.Backend.DEX;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.fail;
 
 import com.android.tools.r8.TestBase.Backend;
 import com.android.tools.r8.ToolHelper.DexVm;
@@ -83,57 +80,27 @@
   }
 
   public CR assertNoMessages() {
-    assertEquals(0, getDiagnosticMessages().getInfos().size());
-    assertEquals(0, getDiagnosticMessages().getWarnings().size());
-    assertEquals(0, getDiagnosticMessages().getErrors().size());
+    getDiagnosticMessages().assertNoMessages();
     return self();
   }
 
   public CR assertOnlyInfos() {
-    assertNotEquals(0, getDiagnosticMessages().getInfos().size());
-    assertEquals(0, getDiagnosticMessages().getWarnings().size());
-    assertEquals(0, getDiagnosticMessages().getErrors().size());
+    getDiagnosticMessages().assertOnlyInfos();
     return self();
   }
 
   public CR assertOnlyWarnings() {
-    assertEquals(0, getDiagnosticMessages().getInfos().size());
-    assertNotEquals(0, getDiagnosticMessages().getWarnings().size());
-    assertEquals(0, getDiagnosticMessages().getErrors().size());
+    getDiagnosticMessages().assertOnlyWarnings();
     return self();
   }
 
   public CR assertWarningMessageThatMatches(Matcher<String> matcher) {
-    assertNotEquals(0, getDiagnosticMessages().getWarnings().size());
-    for (int i = 0; i < getDiagnosticMessages().getWarnings().size(); i++) {
-      if (matcher.matches(getDiagnosticMessages().getWarnings().get(i).getDiagnosticMessage())) {
-        return self();
-      }
-    }
-    StringBuilder builder = new StringBuilder("No warning matches " + matcher.toString());
-    builder.append(System.lineSeparator());
-    if (getDiagnosticMessages().getWarnings().size() == 0) {
-      builder.append("There where no warnings.");
-    } else {
-      builder.append("There where " + getDiagnosticMessages().getWarnings().size() + " warnings:");
-      builder.append(System.lineSeparator());
-      for (int i = 0; i < getDiagnosticMessages().getWarnings().size(); i++) {
-        builder.append(getDiagnosticMessages().getWarnings().get(i).getDiagnosticMessage());
-        builder.append(System.lineSeparator());
-      }
-    }
-    fail(builder.toString());
+    getDiagnosticMessages().assertWarningMessageThatMatches(matcher);
     return self();
   }
 
   public CR assertNoWarningMessageThatMatches(Matcher<String> matcher) {
-    assertNotEquals(0, getDiagnosticMessages().getWarnings().size());
-    for (int i = 0; i < getDiagnosticMessages().getWarnings().size(); i++) {
-      String message = getDiagnosticMessages().getWarnings().get(i).getDiagnosticMessage();
-      if (matcher.matches(message)) {
-        fail("The warning: \"" + message + "\" + matches " + matcher + ".");
-      }
-    }
+    getDiagnosticMessages().assertNoWarningMessageThatMatches(matcher);
     return self();
   }
 
diff --git a/src/test/java/com/android/tools/r8/TestDiagnosticMessages.java b/src/test/java/com/android/tools/r8/TestDiagnosticMessages.java
index 3e734cc..d1e55f4 100644
--- a/src/test/java/com/android/tools/r8/TestDiagnosticMessages.java
+++ b/src/test/java/com/android/tools/r8/TestDiagnosticMessages.java
@@ -5,6 +5,8 @@
 package com.android.tools.r8;
 
 import java.util.List;
+import org.hamcrest.Matcher;
+
 
 public interface TestDiagnosticMessages {
 
@@ -13,4 +15,20 @@
   public List<Diagnostic> getWarnings();
 
   public List<Diagnostic> getErrors();
+
+  public TestDiagnosticMessages assertNoMessages();
+
+  public TestDiagnosticMessages assertOnlyInfos();
+
+  public TestDiagnosticMessages assertOnlyWarnings();
+
+  public TestDiagnosticMessages assertInfosCount(int count);
+
+  public TestDiagnosticMessages assertWarningsCount(int count);
+
+  public TestDiagnosticMessages assertErrorsCount(int count);
+
+  public TestDiagnosticMessages assertWarningMessageThatMatches(Matcher<String> matcher);
+
+  public TestDiagnosticMessages assertNoWarningMessageThatMatches(Matcher<String> matcher);
 }
diff --git a/src/test/java/com/android/tools/r8/TestDiagnosticMessagesImpl.java b/src/test/java/com/android/tools/r8/TestDiagnosticMessagesImpl.java
index d0476b9..43d71b1 100644
--- a/src/test/java/com/android/tools/r8/TestDiagnosticMessagesImpl.java
+++ b/src/test/java/com/android/tools/r8/TestDiagnosticMessagesImpl.java
@@ -4,8 +4,13 @@
 
 package com.android.tools.r8;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
 import java.util.List;
+import org.hamcrest.Matcher;
 
 public class TestDiagnosticMessagesImpl implements DiagnosticsHandler, TestDiagnosticMessages {
   private final List<Diagnostic> infos = new ArrayList<>();
@@ -38,4 +43,75 @@
   public List<Diagnostic> getErrors() {
     return errors;
   }
+
+
+  public TestDiagnosticMessages assertNoMessages() {
+    assertEquals(0, getInfos().size());
+    assertEquals(0, getWarnings().size());
+    assertEquals(0, getErrors().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertOnlyInfos() {
+    assertNotEquals(0, getInfos().size());
+    assertEquals(0, getWarnings().size());
+    assertEquals(0, getErrors().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertOnlyWarnings() {
+    assertEquals(0, getInfos().size());
+    assertNotEquals(0, getWarnings().size());
+    assertEquals(0, getErrors().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertInfosCount(int count) {
+    assertEquals(count, getInfos().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertWarningsCount(int count) {
+    assertEquals(count, getWarnings().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertErrorsCount(int count) {
+    assertEquals(count, getErrors().size());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertWarningMessageThatMatches(Matcher<String> matcher) {
+    assertNotEquals(0, getWarnings().size());
+    for (int i = 0; i < getWarnings().size(); i++) {
+      if (matcher.matches(getWarnings().get(i).getDiagnosticMessage())) {
+        return this;
+      }
+    }
+    StringBuilder builder = new StringBuilder("No warning matches " + matcher.toString());
+    builder.append(System.lineSeparator());
+    if (getWarnings().size() == 0) {
+      builder.append("There where no warnings.");
+    } else {
+      builder.append("There where " + getWarnings().size() + " warnings:");
+      builder.append(System.lineSeparator());
+      for (int i = 0; i < getWarnings().size(); i++) {
+        builder.append(getWarnings().get(i).getDiagnosticMessage());
+        builder.append(System.lineSeparator());
+      }
+    }
+    fail(builder.toString());
+    return this;
+  }
+
+  public TestDiagnosticMessages assertNoWarningMessageThatMatches(Matcher<String> matcher) {
+    assertNotEquals(0, getWarnings().size());
+    for (int i = 0; i < getWarnings().size(); i++) {
+      String message = getWarnings().get(i).getDiagnosticMessage();
+      if (matcher.matches(message)) {
+        fail("The warning: \"" + message + "\" + matches " + matcher + ".");
+      }
+    }
+    return this;
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/ToolHelper.java b/src/test/java/com/android/tools/r8/ToolHelper.java
index 1a17a62..8e135ed 100644
--- a/src/test/java/com/android/tools/r8/ToolHelper.java
+++ b/src/test/java/com/android/tools/r8/ToolHelper.java
@@ -3,6 +3,7 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8;
 
+import static com.android.tools.r8.utils.FileUtils.CLASS_EXTENSION;
 import static com.android.tools.r8.utils.FileUtils.isDexFile;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -57,6 +58,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -851,6 +853,28 @@
         Paths.get("", parts.toArray(new String[parts.size() - 1])));
   }
 
+  public static Collection<Path> getClassFilesForInnerClasses(Path path) throws IOException {
+    Set<Path> paths = new HashSet<>();
+    String prefix = path.toString().replace(CLASS_EXTENSION, "$");
+    paths.addAll(
+        ToolHelper.getClassFilesForTestDirectory(
+            path.getParent(), p -> p.toString().startsWith(prefix)));
+    return paths;
+  }
+
+  public static Collection<Path> getClassFilesForInnerClasses(Collection<Class<?>> classes)
+      throws IOException {
+    Set<Path> paths = new HashSet<>();
+    for (Class clazz : classes) {
+      Path path = ToolHelper.getClassFileForTestClass(clazz);
+      String prefix = path.toString().replace(CLASS_EXTENSION, "$");
+      paths.addAll(
+          ToolHelper.getClassFilesForTestDirectory(
+              path.getParent(), p -> p.toString().startsWith(prefix)));
+    }
+    return paths;
+  }
+
   public static Path getFileNameForTestClass(Class clazz) {
     List<String> parts = getNamePartsForTestClass(clazz);
     return Paths.get("", parts.toArray(new String[parts.size() - 1]));