OSDN Git Service

Fixing the XML emitter to honor the expectations file.
authorJesse Wilson <jessewilson@google.com>
Sun, 20 Dec 2009 22:17:37 +0000 (14:17 -0800)
committerJesse Wilson <jessewilson@google.com>
Sun, 20 Dec 2009 22:27:34 +0000 (14:27 -0800)
libcore/tools/runner/java/dalvik/runner/Driver.java
libcore/tools/runner/java/dalvik/runner/ExpectedResult.java
libcore/tools/runner/java/dalvik/runner/TestRun.java
libcore/tools/runner/java/dalvik/runner/XmlReportPrinter.java

index 772118f..695417d 100644 (file)
@@ -31,7 +31,6 @@ import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Logger;
-import java.util.regex.Pattern;
 
 /**
  * Compiles, installs, runs and reports tests.
@@ -181,18 +180,7 @@ final class Driver {
     }
 
     private void printResult(TestRun testRun) {
-        ExpectedResult expected = testRun.getExpectedResult();
-        boolean patternSuccess;
-
-        if (expected.getPattern() != null) {
-            Pattern pattern = Pattern.compile(expected.getPattern(),
-                    Pattern.MULTILINE | Pattern.DOTALL);
-            patternSuccess = pattern.matcher(Strings.join(testRun.getOutputLines(), "\n")).matches();
-        } else {
-            patternSuccess = true;
-        }
-
-        if (expected.getResult() == testRun.getResult() && patternSuccess) {
+        if (testRun.isExpectedResult()) {
             logger.info("OK " + testRun.getQualifiedName() + " (" + testRun.getResult() + ")");
             return;
         }
@@ -203,17 +191,6 @@ final class Driver {
             logger.info("  \"" + description + "\"");
         }
 
-        if (expected.getResult() != Result.SUCCESS
-                && expected.getResult() != testRun.getResult()) {
-            logger.info("  Expected result: " + expected.getResult());
-        }
-
-        if (!patternSuccess) {
-            logger.info("  Expected output to match \"" + expected.getPattern() + "\"");
-        }
-
-        for (String output : testRun.getOutputLines()) {
-            logger.info("  " + output);
-        }
+        logger.info("  " + testRun.getFailureMessage().replace("\n", "\n  "));
     }
 }
index 6bb8a7f..a0244ce 100644 (file)
@@ -47,40 +47,35 @@ class ExpectedResult {
     /** Matches lines in the file containing a key and value pair. */
     private static final Pattern KEY_VALUE_PAIR_PATTERN = Pattern.compile("(\\w+)\\s+(.+)");
 
-    /**
-     * The expectation of a general successful test run.
-     */
-    static final ExpectedResult SUCCESS = new ExpectedResult(Result.SUCCESS, ".*");
-
-    /**
-     * The test's expected result, such as {@code EXEC_FAILED}. This property is
-     * required.
-     */
+    /** The pattern to use when no expected output is specified */
+    private static final Pattern MATCH_ALL_PATTERN
+            = Pattern.compile(".*", Pattern.MULTILINE | Pattern.DOTALL);
+
+    /** The expectation of a general successful test run. */
+    static final ExpectedResult SUCCESS = new ExpectedResult(Result.SUCCESS, null);
+
+    /** The test's expected result, such as {@code EXEC_FAILED}. */
     private final Result result;
 
-    /**
-     * A regular expression that is the expected output will match. This field
-     * is optional.
-     */
-    private final String pattern;
+    /** The pattern the expected output will match. */
+    private final Pattern pattern;
 
     private ExpectedResult(Result result, String pattern) {
         if (result == null) {
             throw new IllegalArgumentException();
         }
-        if (pattern != null) {
-            Pattern.compile(pattern); // verify that the pattern is well-formed
-        }
 
         this.result = result;
-        this.pattern = pattern;
+        this.pattern = pattern != null
+                ? Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL)
+                : MATCH_ALL_PATTERN;
     }
 
     public Result getResult() {
         return result;
     }
 
-    public String getPattern() {
+    public Pattern getPattern() {
         return pattern;
     }
 
index ab88bac..ff72297 100644 (file)
@@ -21,6 +21,7 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.Arrays;
 import java.util.List;
+import java.util.regex.Pattern;
 
 /**
  * A test run and its outcome. This class tracks the complete lifecycle of a
@@ -176,11 +177,51 @@ public final class TestRun {
         return outputLines;
     }
 
-    public ExpectedResult getExpectedResult() {
-        return expectedResult;
-    }
-
     public Class<? extends TestRunner> getTestRunner() {
         return testRunner;
     }
+
+    /**
+     * Returns true if the outcome of this run matches what was expected.
+     */
+    public boolean isExpectedResult() {
+        return result == expectedResult.getResult() && matchesExpectedPattern();
+    }
+
+    /**
+     * Returns true if the test's output matches the expected output.
+     */
+    private boolean matchesExpectedPattern() {
+        return expectedResult.getPattern()
+                .matcher(Strings.join(outputLines, "\n"))
+                .matches();
+    }
+
+    /**
+     * Returns the failure message for this failed test run. This message is
+     * intended to help to diagnose why the test result didn't match what was
+     * expected.
+     */
+    public String getFailureMessage() {
+        StringBuilder builder = new StringBuilder();
+
+        if (expectedResult.getResult() != Result.SUCCESS
+                && expectedResult.getResult() != result) {
+            builder.append("Expected result: ")
+                    .append(expectedResult.getResult())
+                    .append("\n");
+        }
+
+        if (!matchesExpectedPattern()) {
+            builder.append("Expected output to match \"")
+                    .append(expectedResult.getPattern().pattern())
+                    .append("\"\n");
+        }
+
+        for (String output : outputLines) {
+            builder.append(output).append("\n");
+        }
+
+        return builder.toString();
+    }
 }
index 9217fc0..669a26c 100644 (file)
@@ -25,7 +25,6 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
-import java.util.EnumSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -43,18 +42,6 @@ import java.util.TimeZone;
  */
 public class XmlReportPrinter {
 
-    /**
-     * Test results of these types are omitted from the report, as if the tests
-     * never existed. Equivalent to the core test runner's @BrokenTest.
-     */
-    private static final EnumSet<Result> IGNORED_RESULTS
-            = EnumSet.of(Result.COMPILE_FAILED);
-
-    private static final EnumSet<Result> FAILED_RESULTS
-            = EnumSet.of(Result.EXEC_FAILED);
-    private static final EnumSet<Result> ERROR_RESULTS
-            = EnumSet.complementOf(EnumSet.of(Result.EXEC_FAILED, Result.SUCCESS));
-
     private static final String TESTSUITE = "testsuite";
     private static final String TESTCASE = "testcase";
     private static final String ERROR = "error";
@@ -116,7 +103,7 @@ public class XmlReportPrinter {
     private Map<String, Suite> testsToSuites(Collection<TestRun> testRuns) {
         Map<String, Suite> result = new LinkedHashMap<String, Suite>();
         for (TestRun testRun : testRuns) {
-            if (IGNORED_RESULTS.contains(testRun.getResult())) {
+            if (testRun.getResult() == Result.UNSUPPORTED) {
                 continue;
             }
 
@@ -128,10 +115,13 @@ public class XmlReportPrinter {
             }
 
             suite.tests.add(testRun);
-            if (FAILED_RESULTS.contains(testRun.getResult())) {
-                suite.failuresCount++;
-            } else if (ERROR_RESULTS.contains(testRun.getResult())) {
-                suite.errorsCount++;
+
+            if (!testRun.isExpectedResult()) {
+                if (testRun.getResult() == Result.EXEC_FAILED) {
+                    suite.failuresCount++;
+                } else {
+                    suite.errorsCount++;
+                }
             }
         }
         return result;
@@ -172,11 +162,8 @@ public class XmlReportPrinter {
             serializer.attribute(ns, ATTR_CLASSNAME, testRun.getSuiteName());
             serializer.attribute(ns, ATTR_TIME, "0");
 
-            String result = ERROR_RESULTS.contains(testRun.getResult()) ? ERROR
-                    : FAILED_RESULTS.contains(testRun.getResult()) ? FAILURE
-                    : null;
-
-            if (result != null) {
+            if (!testRun.isExpectedResult()) {
+                String result = testRun.getResult() == Result.EXEC_FAILED ? FAILURE : ERROR;
                 serializer.startTag(ns, result);
                 String title = testRun.getDescription();
                 if (title != null && title.length() > 0) {