com.github.mperry.fg.test.Specification.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalgroovy-main Show documentation
Show all versions of functionalgroovy-main Show documentation
FunctionalGroovy enhances FunctionalJava for Groovy
package com.github.mperry.fg.test
import fj.F
import fj.F2
import fj.F3
import fj.F4
import fj.F5
import fj.F6
import fj.F7
import fj.F8
import fj.P2
import fj.data.Option
import fj.data.Validation
import fj.test.Arbitrary
import fj.test.Bool
import fj.test.CheckResult
import fj.test.Property
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import org.codehaus.groovy.runtime.NullObject
/**
* Created with IntelliJ IDEA.
* User: MarkPerry
* Date: 30/11/13
* Time: 3:58 PM
* To change this template use File | Settings | File Templates.
*/
@TypeChecked
class Specification {
static final int MAX_ARGS = 5
static final Map> FUNC_TYPES = [1: F, 2: F2, 3: F3, 4: F4, 5: F5, 6: F6, 7: F7, 8: F8]
@TypeChecked(TypeCheckingMode.SKIP)
static Property createProp(Map, Arbitrary> map, Option> pre, Closure c, F, Boolean> validation) {
def list = c.getParameterTypes()
def listOptArbs = list.collect { Class it -> map.containsKey(it) ? Option.some(map[it]) : Option.none() }
def allMapped = listOptArbs.forAll { Option it -> it.isSome() }
if (!allMapped) {
throw new Exception("Not all function parameter types were found: ${list.findAll { !map.containsKey(it)}}")
}
def listArb = listOptArbs.collect { Option it -> it.some() }
dynamicCreateProp(listArb, pre, c, validation)
}
@TypeChecked(TypeCheckingMode.SKIP)
static Property dynamicCreateProp(List list, Option> pre, Closure c, F, Boolean> validate) {
this."createProp${list.size()}"(list, pre, c, validate)
}
static Property showAllWithMap(Boolean truth, Map, Arbitrary> map, Option> pre, Closure c, F, Boolean> validate) {
if (c.getMaximumNumberOfParameters() > MAX_ARGS) {
throw new Exception("Testing does not support ${c.getMaximumNumberOfParameters()}, maximum supported is $MAX_ARGS")
}
def p = createProp(map, pre, c, validate)
p
// def cr = p.check()
// p.checkBooleanWithNullableSummary(truth)
}
/**
*
* @param map Override the default map
* @param c
*/
@TypeChecked(TypeCheckingMode.SKIP)
static Property spec(Map, Arbitrary>> map, Closure c) {
def p = showAllWithMap(true, Model.DEFAULT_MAP + map, Option.none(), c, Model.DEFAULT_VALIDATOR)
p
// check(p, true)
}
static Property spec(Model config) {
def p = showAllWithMap(config.truth, config.map, config.pre, config.function, config.validator)
p
// check(p, config.truth)
}
@TypeChecked(TypeCheckingMode.SKIP)
static Property spec(Closure c) {
def p = showAllWithMap(true, Model.DEFAULT_MAP, Option.none(), c, Model.DEFAULT_VALIDATOR)
p
// check(p, true)
}
@TypeChecked(TypeCheckingMode.SKIP)
static CheckResult specAssert(Map, Arbitrary>> map, Closure c) {
check(spec(map, c), true)
}
static CheckResult specAssert(Model config) {
check(spec(config), config.truth)
}
@TypeChecked(TypeCheckingMode.SKIP)
static CheckResult specAssert(Closure c) {
// def p = showAllWithMap(true, Model.DEFAULT_MAP, Option.none(), c, Model.DEFAULT_VALIDATOR)
check(spec(c), true)
}
static CheckResult specAssert(Property p, boolean truth) {
check(p, truth)
}
static CheckResult specAssert(Property p) {
check(p, true)
}
static CheckResult check(Property p, Boolean truth) {
// def cr = p.check()
p.checkBooleanWithNullableSummary(truth)
}
static Property implies(Boolean pre, Boolean result) {
Bool.bool(pre).implies(result)
}
static Validation perform(Closure c, List args) {
try {
Validation.success(c.call(args))
} catch (Throwable t) {
Validation.fail(t)
}
}
static void checkTypes(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy