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

com.insightfullogic.lambdabehave.impl.CompleteSpecification Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
package com.insightfullogic.lambdabehave.impl;

import com.insightfullogic.lambdabehave.expectations.Expect;
import com.insightfullogic.lambdabehave.impl.reports.SpecificationReport;
import com.insightfullogic.lambdabehave.specifications.Specification;

import java.util.Optional;

import static com.insightfullogic.lambdabehave.impl.reports.SpecificationReport.*;

/**
 * A complete behaviour is a composite object consisting of a specification behaviour with its associated
 * setup and teardown behaviours.
 */
final class CompleteSpecification implements CompleteBehaviour {

    private final Blocks prefixes;
    private final Behaviour behaviour;
    private final Blocks postfixes;
    private final String suiteName;

    public CompleteSpecification(final Blocks prefixes, final Behaviour behaviour, final Blocks postfixes, final String suiteName) {
        this.prefixes = prefixes;
        this.behaviour = behaviour;
        this.postfixes = postfixes;
        this.suiteName = suiteName;
    }

    @Override
    public SpecificationReport checkCompleteBehaviour() {
        SpecificationReport report = prefixes.runAll(getDescription())
                                    .orElseGet(this::checkBehaviour);

        Optional suffixReport = postfixes.runAll(getDescription());
        if (report.isSuccess() && suffixReport.isPresent()) {
            return suffixReport.get();
        } else {
            return report;
        }
    }

    private SpecificationReport checkBehaviour() {
        Specification specification = behaviour.getSpecification();
        String description = behaviour.getDescription();
        try {
            Expect expect = new Expect();
            specification.specifyBehaviour(expect);
            return success(description);
        } catch (final AssertionError cause) {
            return failure(description, cause);
        } catch (final Throwable cause) {
            return error(description, cause);
        }
    }

    @Override
    public String getDescription() {
        return behaviour.getDescription();
    }

    @Override
    public String getSuiteName() {
        return suiteName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy