diff --git a/src/com/gfredericks/schpec/fns.clj b/src/com/gfredericks/schpec/fns.clj new file mode 100644 index 0000000..7f2a329 --- /dev/null +++ b/src/com/gfredericks/schpec/fns.clj @@ -0,0 +1,27 @@ +(ns com.gfredericks.schpec.fns) + +(defn commutative + "Returns a mapping fn suitable for use in clojure.spec.test/check that + specifies the given fn is commutative" + [f] + (fn [mapping] + (let [{:keys [args ret]} mapping] + (if (> (count args) 1) + (= ret (apply f (reverse args))) + true)))) + +(defn associative + "Returns a mapping fn suitable for use in clojure.spec.test/check that + specifies the given fn is associative" + [f] + (fn [mapping] + (let [{:keys [args ret]} mapping] + (= ret (f (first args) (apply f (rest args))))))) + +(defn has-identity + "Returns a mapping fn suitable for use in clojure.spec.test/check that + specifies the given fn has the given identity value" + [f i] + (fn [mapping] + (let [{:keys [args ret]} mapping] + (= ret (f i ret))))) diff --git a/test/com/gfredericks/schpec/fns_test.clj b/test/com/gfredericks/schpec/fns_test.clj new file mode 100644 index 0000000..9f5fd12 --- /dev/null +++ b/test/com/gfredericks/schpec/fns_test.clj @@ -0,0 +1,27 @@ +(ns com.gfredericks.schpec.fns-test + (:require [clojure.test :refer :all] + [com.gfredericks.schpec.fns :refer :all])) + +(deftest test-commutative + (let [f (commutative +)] + (is (f {:args [1 2] + :ret 3}))) + (let [f (commutative -)] + (is (not (f {:args [1 2] + :ret -1}))))) + +(deftest test-associative + (let [f (associative +)] + (is (f {:args [1 2 3] + :ret 6}))) + (let [f (associative -)] + (is (not (f {:args [1 2 3] + :ret -4}))))) + +(deftest test-has-identity + (let [f (has-identity + 0)] + (is (f {:args [1 2] + :ret 3}))) + (let [f (has-identity + 1)] + (is (not (f {:args [1 2] + :ret 3})))))