Last active
October 31, 2019 18:53
-
-
Save Risto-Stevcev/dc628109abd840c7553de1c5d7d55608 to your computer and use it in GitHub Desktop.
Some functions to help integrate clojure.spec.test/check with cljs.test/deftest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns foo-test | |
(:require [cljs.spec :as s] | |
[cljs.spec.test :as stest] | |
[clojure.pprint :as pprint] | |
[cljs.test :refer-macros [deftest is]])) | |
;; Sample function and a function spec | |
(defn fooo [i] (+ i 20)) | |
(s/fdef fooo | |
:args (s/cat :i integer?) | |
:ret integer? | |
:fn #(> (:ret %) (-> % :args :i))) | |
;; Utility functions to intergrate clojure.spec.test/check with clojure.test | |
(defn summarize-results' [spec-check] | |
(map (comp #(pprint/write % :stream nil) stest/abbrev-result) spec-check)) | |
(defn check' [spec-check] | |
(is (nil? (-> spec-check first :failure)) (summarize-results' spec-check))) | |
;; Tests | |
(deftest fooish (check' (stest/check `fooo))) | |
;; Since spec does the generative tests for you, clojure specs become something like a gradually typed dependent | |
;; type system (types as values), because the only effort the developer needs to provide are type signatures and a line | |
;; to do the spec check (shown above). This is an ad-hoc type system especially if run via fighweel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment