From bd432ed632d6046f6d757cda6e861453181b1e7f Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Sun, 7 Aug 2016 17:37:06 -0400 Subject: [PATCH 1/2] Add fn properties test helpers --- src/com/gfredericks/schpec/fns.clj | 27 ++++++++++++++++++++++++ test/com/gfredericks/schpec/fns_test.clj | 27 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/com/gfredericks/schpec/fns.clj create mode 100644 test/com/gfredericks/schpec/fns_test.clj 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..22d473f --- /dev/null +++ b/test/com/gfredericks/schpec/fns_test.clj @@ -0,0 +1,27 @@ +(ns com.gfredericks.schpecschpec.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}))))) From 0814cad55e9237d189e425769d89cd9d19395116 Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Sun, 7 Aug 2016 17:58:51 -0400 Subject: [PATCH 2/2] Fix egregious spelling error Stupid emacs keystroke foo --- test/com/gfredericks/schpec/fns_test.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/com/gfredericks/schpec/fns_test.clj b/test/com/gfredericks/schpec/fns_test.clj index 22d473f..9f5fd12 100644 --- a/test/com/gfredericks/schpec/fns_test.clj +++ b/test/com/gfredericks/schpec/fns_test.clj @@ -1,4 +1,4 @@ -(ns com.gfredericks.schpecschpec.fns-test +(ns com.gfredericks.schpec.fns-test (:require [clojure.test :refer :all] [com.gfredericks.schpec.fns :refer :all]))