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

org.tudalgo.algoutils.student.test.StudentTest Maven / Gradle / Ivy

The newest version!
package org.tudalgo.algoutils.student.test;

import java.util.function.Predicate;

/**
 * A generic test used by students to test their solutions.
 *
 * @param  The type of the object to test.
 */
public class StudentTest {
    /**
     * The {@link Predicate} used to test the object.
     */
    protected Predicate predicate;
    /**
     * The {@link StudentTestResultMessageProvider} used to generate the message for the result.
     */
    protected StudentTestResultMessageProvider messageProvider;

    /**
     * Creates a new {@link StudentTest} with the given {@code predicate} and {@code messageProvider}.
     *
     * @param predicate       The {@link Predicate} used to test the object.
     * @param messageProvider The {@link StudentTestResultMessageProvider} used to generate the message for the result.
     */
    public StudentTest(Predicate predicate, StudentTestResultMessageProvider messageProvider) {
        this.predicate = predicate;
        this.messageProvider = messageProvider;
    }

    /**
     * Tests the given {@code toTest} object with the {@link #predicate} and returns a {@link StudentTestResult} with
     * the result.
     *
     * @param toTest The object to test.
     * @return The generated {@link StudentTestResult}.
     */
    public StudentTestResult test(T toTest) {
        final var resultBuilder = StudentTestResult
            .builder(this)
            .setToTest(toTest);
        try {
            if (predicate.test(toTest)) {
                resultBuilder.setState(StudentTestState.PASSED);
            } else {
                resultBuilder.setState(StudentTestState.FAILED_BY_ASSERTION);
                resultBuilder.setThrowable(new AssertionError());
            }
        } catch (Throwable throwable) {
            resultBuilder
                .setState(StudentTestState.FAILED_WITH_EXCEPTION)
                .setThrowable(throwable);
        }
        resultBuilder.setMessage(messageProvider.getMessage(resultBuilder.build()));
        return resultBuilder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy