Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Last active October 31, 2019 18:53
Show Gist options
  • Save Risto-Stevcev/dc628109abd840c7553de1c5d7d55608 to your computer and use it in GitHub Desktop.
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
(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