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

com.github.mperry.fg.test.DbcContractValidator.groovy Maven / Gradle / Ivy

There is a newer version: 0.8
Show newest version
package com.github.mperry.fg.test

import fj.F
import fj.data.Validation
import groovy.transform.TypeChecked
import org.gcontracts.AssertionViolation
import org.gcontracts.ClassInvariantViolation
import org.gcontracts.PostconditionViolation
import org.gcontracts.PreconditionViolation

/**
 * Created with IntelliJ IDEA.
 * User: mwp
 * Date: 3/12/13
 * Time: 11:47 AM
 * To change this template use File | Settings | File Templates.
 */
@TypeChecked
class DbcContractValidator {

    static F, Boolean> validateValidation() {
        Model.validator(validateThrowable())
    }

    static F validateThrowable() {
        { Throwable t ->
            contractsOk(t)
        } as F
    }

    static Boolean contractViolation(Throwable t) {
        hasType(t, AssertionViolation.class)
    }

    static Boolean contractsOk(Throwable t) {
        invariantOk(t).and(preOk(t).implies(postOk(t)))
    }

    static Boolean preOk(Throwable t) {
        !hasType(t, PreconditionViolation.class)
    }

    static Boolean postOk(Throwable t) {
        !hasType(t, PostconditionViolation.class)
    }

    static Boolean invariantOk(Throwable t) {
        !hasType(t, ClassInvariantViolation.class)
    }

    static Boolean hasType(Object o, Class c) {
        c.isAssignableFrom(o.getClass())
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy