Skip to content

Instantly share code, notes, and snippets.

@viebel
Forked from Risto-Stevcev/spectest.cljs
Created February 11, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/d00a0697e440d1596422ffe7636bb582 to your computer and use it in GitHub Desktop.
Save viebel/d00a0697e440d1596422ffe7636bb582 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 [run-tests 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
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment