Skip to content

Instantly share code, notes, and snippets.

@Jannis
Last active June 17, 2016 09:44
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 Jannis/5dcc91473f20861d154dc8be2fff2bfd to your computer and use it in GitHub Desktop.
Save Jannis/5dcc91473f20861d154dc8be2fff2bfd to your computer and use it in GitHub Desktop.
;;;; Works
(ns workflo.macros.scratch
(:require #?(:cljs [cljs.spec :as s]
:clj [clojure.spec :as s])
[clojure.test.check.generators :as gen]))
(s/def ::item
symbol?)
(s/def ::pair
(s/cat :base ::item
:children ::items))
(s/def ::item-or-pair
(s/alt :item ::item
:pair ::pair))
(s/def ::items
(s/and vector?
(s/* ::item-or-pair)))
(println (s/conform ::items '[foo bar [baz ruux]]))
;; Success!
(println (s/explain ::items '[foo bar [baz ruux]]))
;; nil
;;;; Does not work
(ns workflo.macros.scratch
(:require #?(:cljs [cljs.spec :as s]
:clj [clojure.spec :as s])
[clojure.test.check.generators :as gen]))
(s/def ::item
symbol?)
(s/def ::pair
(s/with-gen
(s/cat :base ::item
:children ::items)
#(gen/tuple (s/gen ::item)
(s/gen ::items))))
(s/def ::item-or-pair
(s/alt :item ::item
:pair ::pair))
(s/def ::items
(s/and vector?
(s/* ::item-or-pair)))
(println (s/conform ::items '[foo bar [baz ruux]]))
;; :clojure.spec/invalid
(println (s/explain ::items '[foo bar [baz ruux]]))
;; In: [2] val: [baz ruux] fails spec: :workflo.macros.scratch/item at: [:item] predicate: symbol?
;; In: [2 1] val: ruux fails spec: :workflo.macros.scratch/items at: [:pair :workflo.macros.scratch/children] predicate: vector?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment