Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;;;; 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