All Downloads are FREE. Search and download functionalities are using the official Maven repository.

framework.src.org.checkerframework.framework.test.TypecheckResult Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.framework.test;

import org.checkerframework.framework.test.diagnostics.TestDiagnostic;
import org.checkerframework.framework.test.diagnostics.TestDiagnosticUtils;
import org.checkerframework.framework.util.PluginUtil;

import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Represents the test results from typechecking one or more java files using
 * the given TestConfiguration.
 */
public class TypecheckResult {
    private final TestConfiguration configuration;
    private final CompilationResult compilationResult;
    private final List expectedDiagnostics;

    private final boolean testFailed;

    private final List missingDiagnostics;
    private final List unexpectedDiagnostics;

    protected TypecheckResult(TestConfiguration configuration, CompilationResult compilationResult,
                              List expectedDiagnostics, boolean testFailed,
                              List missingDiagnostics, List unexpectedDiagnostics) {
        this.configuration = configuration;
        this.compilationResult = compilationResult;
        this.expectedDiagnostics = expectedDiagnostics;
        this.testFailed = testFailed;
        this.missingDiagnostics = missingDiagnostics;
        this.unexpectedDiagnostics = unexpectedDiagnostics;
    }

    public TestConfiguration getConfiguration() {
        return configuration;
    }

    public CompilationResult getCompilationResult() {
        return compilationResult;
    }

    public List> getActualDiagnostics() {
        return compilationResult.getDiagnostics();
    }

    public List getExpectedDiagnostics() {
        return expectedDiagnostics;
    }

    public boolean didTestFail() {
        return testFailed;
    }

    public List getMissingDiagnostics() {
        return missingDiagnostics;
    }

    public List getUnexpectedDiagnostics() {
        return unexpectedDiagnostics;
    }

    public List getErrorHeaders() {
        List errorHeaders = new ArrayList<>();

        // none of these should be true if the test didn't fail
        if (testFailed) {
            if (compilationResult.compiledWithoutError() && !expectedDiagnostics.isEmpty()) {
                errorHeaders.add("The test run was expected to issue errors/warnings, but it did not.");

            } else if (!compilationResult.compiledWithoutError() && expectedDiagnostics.isEmpty()) {
                errorHeaders.add("The test run was not expected to issue errors/warnings, but it did.");
            }

            List> actualDiagnostics = getActualDiagnostics();
            if (!unexpectedDiagnostics.isEmpty() || !missingDiagnostics.isEmpty()) {
                int numExpected = expectedDiagnostics.size();
                int numFound = numExpected - missingDiagnostics.size();
                errorHeaders.add(
                    numFound + " out of " + numExpected + " expected diagnostics "
                  + (numFound == 1 ? "was" : "were") + " found."
                );
            }


        }

        return errorHeaders;
    }

    public String summarize() {
        if (testFailed) {
            StringBuilder summaryBuilder = new StringBuilder();
            summaryBuilder.append(PluginUtil.join("\n", getErrorHeaders()));
            summaryBuilder.append("\n");

            if (!missingDiagnostics.isEmpty()) {
                summaryBuilder.append(
                     missingDiagnostics.size() == 1
                           ? "1 expected diagnostic was not found:\n"
                           : missingDiagnostics.size() + " expected diagnostics were not found:\n"
                );

                for (TestDiagnostic missing : missingDiagnostics) {
                    summaryBuilder.append(missing);
                    summaryBuilder.append("\n");
                }
            }

            if (!unexpectedDiagnostics.isEmpty()) {
                summaryBuilder.append(
                    unexpectedDiagnostics.size() == 1
                            ? "1 unexpected diagnostic was found:\n"
                            : unexpectedDiagnostics.size() + " unexpected diagnostics were found:\n"
                );

                for (TestDiagnostic unexpected : unexpectedDiagnostics) {
                    summaryBuilder.append(unexpected);
                    summaryBuilder.append("\n");
                }
            }

            summaryBuilder.append("While type-checking " + TestUtilities.summarizeSourceFiles(configuration.getTestSourceFiles()));
            return summaryBuilder.toString();
        }

        return "";
    }

    public static TypecheckResult fromCompilationResults(TestConfiguration configuration, CompilationResult result,
                                                         List expectedDiagnostics) {

        boolean usingAnomsgtxt = configuration.getOptions().containsKey("-Anomsgtext");
        final Set actualDiagnostics =
                TestDiagnosticUtils.fromJavaxDiagnosticList(result.getDiagnostics(), usingAnomsgtxt);


        final Set unexpectedDiagnostics = new LinkedHashSet<>();
        unexpectedDiagnostics.addAll(actualDiagnostics);
        unexpectedDiagnostics.removeAll(expectedDiagnostics);

        final List missingDiagnostics = new LinkedList<>(expectedDiagnostics);
        missingDiagnostics.removeAll(actualDiagnostics);

        boolean testFailed = !unexpectedDiagnostics.isEmpty() || !missingDiagnostics.isEmpty();

        return new TypecheckResult(configuration, result, expectedDiagnostics, testFailed,
                missingDiagnostics, new ArrayList<>(unexpectedDiagnostics));
    }


    public static TypecheckResult fromCompilationResultsExpectedDiagnostics(
            TestConfiguration configuration, CompilationResult result, List expectedDiagnostics) {

        boolean usingAnomsgtxt = configuration.getOptions().containsKey("-Anomsgtext");
        final Set actualDiagnostics =
                TestDiagnosticUtils.fromJavaxDiagnosticList(result.getDiagnostics(), usingAnomsgtxt);


        final Set unexpectedDiagnostics = new LinkedHashSet<>();
        unexpectedDiagnostics.addAll(actualDiagnostics);
        unexpectedDiagnostics.removeAll(expectedDiagnostics);

        final List missingDiagnostics = new LinkedList<>(expectedDiagnostics);
        missingDiagnostics.removeAll(actualDiagnostics);

        boolean testFailed = !unexpectedDiagnostics.isEmpty() || !missingDiagnostics.isEmpty();

        return new TypecheckResult(configuration, result, expectedDiagnostics, testFailed,
                missingDiagnostics, new ArrayList<>(unexpectedDiagnostics));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy