Say that we have a function clothe which requires one positional argument person in addition to a number of optional named arguments :hat, :shirt and :pants.
(defn clothe [person & {:keys [hat shirt pants]}]
(str "Clothing " person " with " hat shirt pants "."))
(clothe 'me :hat "top hat")
=> "Clothing me with top hat."
My current way of writing a spec for this function would be:
(require '[clojure.spec :as spec]
'[clojure.spec.gen :as gen])
(spec/def ::person symbol?)
(spec/def ::clothing
(spec/alt :hat (spec/cat :key #{:hat} :value string?)
:shirt (spec/cat :key #{:shirt} :value string?)
:pants (spec/cat :key #{:pants} :value string?)))
(spec/fdef clothe
:args (spec/cat :person ::person
:clothes (spec/* ::clothing))
:ret string?)
The problem then being that it allows for argument lists like
(clothe 'me :hat "top hat" :hat "nice hat")
=> "Clothing me with nice hat."
which even though allowed by the language itself probably is a mistake whenever made. But perhaps worse than that is that it makes the generated data unrealistic to how the function is usually called:
(gen/generate (spec/gen (spec/cat :person ::person
:clothes (spec/* ::clothing))))
=> (_+_6+h/!-6Gg9!43*e :hat "m6vQmoR72CXc6R3GP2hcdB5a0"
:hat "05G5884aBLc80s4AF5X9V84u4RW" :pants "3Q" :pants "a0v329r25f3k5oJ4UZJJQa5"
:hat "C5h2HW34LG732ifPQDieH" :pants "4aeBas8uWx1eQWYpLRezBIR" :hat "C229mzw"
:shirt "Hgw3EgUZKF7c7ya6q2fqW249GsB" :pants "byG23H2XyMTx0P7v5Ve9qBs"
:shirt "5wPMjn1F2X84lU7X3CtfalPknQ5" :pants "0M5TBgHQ4lR489J55atm11F3"
:shirt "FKn5vMjoIayO" :shirt "2N9xKcIbh66" :hat "K8xSFeydF" :hat "sQY4iUPF0Ef58198270DOf"
:hat "gHGEqi58A4pH2s74t0" :pants "" :hat "D6RKWJJoFLCAaHId8AF4" :pants "exab2w5o88b"
:hat "S7Ti2Cb1f7se7o86I1uE" :shirt "9g3K6q1" :hat "slKjK67608Y9w1sqV1Kxm"
:hat "cFbVMaq8bfP22P8cD678s" :hat "f57" :hat "2W83oa0WVWM10y1U49265k2bJx"
:hat "O6" :shirt "7BUJ824efBb81RL99zBrvH2HjziIT")
And worse of all, if you happen to have a recursive defenition with spec/* there is no way of limiting the number of potentially recursive occurences generated when running tests on the code.
So then my question becomes: Is there a way to specify named arguments to a function limiting the number of occurences per key to one?
If we look at the way the require macro is specced in clojure.core.specs we can see that it uses (spec/keys* :opt-un []) to specify the named arguments in the dependency list, such as :refer and :as in (ns (:require [a.b :as b :refer :all])).
(s/def ::or (s/map-of simple-symbol? any?))
(s/def ::as ::local-name)
(s/def ::prefix-list
(s/spec
(s/cat :prefix simple-symbol?
:suffix (s/* (s/alt :lib simple-symbol? :prefix-list ::prefix-list))
:refer (s/keys* :opt-un [::as ::refer]))))
(s/def ::ns-require
(s/spec (s/cat :clause #{:require}
:libs (s/* (s/alt :lib simple-symbol?
:prefix-list ::prefix-list
:flag #{:reload :reload-all :verbose})))))
The documentation doesn't mention what :req-un and :opt-un are for, but the Spec Guide on the other hand mentions that they are for specifying unqualified keys. Returning to our function defenition we could write it as:
(spec/def ::clothing (spec/keys* :opt-un [::hat ::shirt ::pants]))
(spec/def ::hat string?)
(spec/def ::shirt string?)
(spec/def ::pants string?)
(spec/fdef clothe
:args (spec/cat :person ::person
:clothes ::clothing)
:ret string?)
Sadly this doesn't help with the function accepting multiple instances of the same named argument
(stest/instrument `clothe)
(clothe 'me :hat "top hat" :hat "nice hat")
=> "Clothing me with nice hat."
Though it does mean that the generator maximally produces one instance of the same key which does help with the recursive specs.
(gen/generate (spec/gen (spec/cat :person ::person
:clothes ::clothing)))
=> (u_K_P6!!?4Ok!_I.-.d!2_.T-0.!+H+/At.7R8z*6?QB+921A
:shirt "B4W86P637c6KAK1rv04O4FRn6S" :pants "3gdkiY" :hat "20o77")
Related
My Clojure spec looks like :
(spec/def ::global-id string?)
(spec/def ::part-of string?)
(spec/def ::type string?)
(spec/def ::value string?)
(spec/def ::name string?)
(spec/def ::text string?)
(spec/def ::date (spec/nilable (spec/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))))
(spec/def ::interaction-name string?)
(spec/def ::center (spec/coll-of string? :kind vector? :count 2))
(spec/def ::context- (spec/keys :req [::global-id ::type]
:opt [::part-of ::center]))
(spec/def ::contexts (spec/coll-of ::context-))
(spec/def ::datasource string?)
(spec/def ::datasource- (spec/nilable (spec/keys :req [::global-id ::name])))
(spec/def ::datasources (spec/coll-of ::datasource-))
(spec/def ::location string?)
(spec/def ::location-meaning- (spec/keys :req [::global-id ::location ::contexts ::type]))
(spec/def ::location-meanings (spec/coll-of ::location-meaning-))
(spec/def ::context string?)
(spec/def ::context-association-type string?)
(spec/def ::context-association-name string?)
(spec/def ::priority string?)
(spec/def ::has-context- (spec/keys :req [::context ::context-association-type ::context-association-name ::priority]))
(spec/def ::has-contexts (spec/coll-of ::has-context-))
(spec/def ::fact- (spec/keys :req [::global-id ::type ::name ::value]))
(spec/def ::facts (spec/coll-of ::fact-))
(spec/def ::attribute- (spec/keys :req [::name ::type ::value]))
(spec/def ::attributes (spec/coll-of ::attribute-))
(spec/def ::fulltext (spec/keys :req [::global-id ::text]))
(spec/def ::feature- (spec/keys :req [::global-id ::date ::location-meanings ::has-contexts ::facts ::attributes ::interaction-name]
:opt [::fulltext]))
(spec/def ::features (spec/coll-of ::feature-))
(spec/def ::attribute- (spec/keys :req [::name ::type ::value]))
(spec/def ::attributes (spec/coll-of ::attribute-))
(spec/def ::ioi-slice string?)
(spec/def ::ioi- (spec/keys :req [::global-id ::type ::datasource ::features ::attributes ::ioi-slice]))
(spec/def ::iois (spec/coll-of ::ioi-))
(spec/def ::data (spec/keys :req [::contexts ::datasources ::iois]))
(spec/def ::data- ::data)
But it fails to generate samples with:
(spec/fdef data->graph
:args (spec/cat :data ::xml-spec/data-))
(println (stest/check `data->graph))
then it will fail to generate with an exception:
Couldn't satisfy such-that predicate after 100 tries.
It is very convenient to generate spec automatically with stest/check but how to beside spec also have generators?
When you see the error Couldn't satisfy such-that predicate after 100 tries. when generating data from specs, a common cause is an s/and spec because spec builds generators for s/and specs based solely on the first inner spec.
This spec seemed most likely to cause this, because the first inner spec/predicate in the s/and is string?, and the following predicate is a regex:
(s/def ::date (s/nilable (s/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))))
If you sample a string? generator, you'll see what it produces is unlikely to ever match your regex:
(gen/sample (s/gen string?))
=> ("" "" "X" "" "" "hT9" "7x97" "S" "9" "1Z")
test.check will try (100 times by default) to get a value that satisfies such-that conditions, then throw the exception you're seeing if it doesn't.
Generating Dates
You can implement a custom generator for this spec in several ways. Here's a test.check generator that will create ISO local date strings:
(def gen-local-date-str
(let [day-range (.range (ChronoField/EPOCH_DAY))
day-min (.getMinimum day-range)
day-max (.getMaximum day-range)]
(gen/fmap #(str (LocalDate/ofEpochDay %))
(gen/large-integer* {:min day-min :max day-max}))))
This approach gets the range of valid epoch days, uses that to control the range of large-integer* generator, then fmaps LocalDate/ofEpochDay over the generated integers.
(def gen-local-date-str
(gen/fmap #(-> (Instant/ofEpochMilli %)
(LocalDateTime/ofInstant ZoneOffset/UTC)
(.toLocalDate)
(str))
gen/large-integer))
This starts with the default large-integer generator and uses fmap to provide a function that creates a java.time.Instant from the generated integer, converts it to a java.time.LocalDate, and converts that to a string which happens to conveniently match your date string format. (This is slightly simpler on Java 9 and above with java.time.LocalDate/ofInstant.)
Another approach might use test.chuck's regex-based string generator, or different date classes/formatters. Note that both of my examples will generate years that are eons before/after -9999/+9999, which won't match your \d{4} year regex, but the generator should produce satisfactory values often enough that it may not matter for your use case. There are many ways to generate date values!
(gen/sample gen-local-date-str)
=>
("1969-12-31"
"1970-01-01"
"1970-01-01"
...)
Using Custom Generators with Specs
Then you can associate this generator with your spec using s/with-gen:
(s/def ::date
(s/nilable
(s/with-gen
(s/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))
(constantly gen-local-date-str))))
(gen/sample (s/gen ::date))
=>
("1969-12-31"
nil ;; note that it also makes nils b/c it's wrapped in s/nilable
"1970-01-01"
...)
You can also provide "standalone" custom generators to certain spec functions that take an overrides map, if you don't want to tie the custom generator directly to the spec definition:
(gen/sample (s/gen ::data {::date (constantly gen-local-date-str)}))
Using this spec and generator I was able to generate your larger ::data spec, although the outputs were very large due to some of the collection specs. You can also control the size of those during generation using :gen-max options in the specs.
This is an application that represents visual patterns as a collection of Sshapes.
An Sshape (styled shape) is a list of points and a map of style information.
An APattern is a record containing a list of Sshapes.
Here's the spec :
In sshape.clj
(spec/def ::stroke-weight int?)
(spec/def ::color (spec/* int?))
(spec/def ::stroke ::color)
(spec/def ::fill ::color)
(spec/def ::hidden boolean?)
(spec/def ::bezier boolean?)
(spec/def ::style (spec/keys :opt-un [::stroke-weight ::stroke ::fill ::hidden ::bezier]))
(spec/def ::point (spec/* number?))
(spec/def ::points (spec/* ::point))
(spec/def ::SShape (spec/keys :req-un [::style ::points]))
In groups.clj
(spec/def ::sshapes (spec/* :patterning.sshapes/SShape))
(spec/def ::APattern (spec/keys :req-un [::sshapes]))
Then in another file, I try to test that a superimpose function that puts two APatterns together is accepting APatterns
(defn superimpose-layout "simplest layout, two patterns located on top of each other "
[pat1 pat2]
{:pre [(spec/valid? :patterning.groups/APattern pat1)]}
(->APattern (concat (:sshapes pat1) (:sshapes pat2))) )
Without the pre-condition this runs.
With the pre-condition, I get this infinite recursion and stack overflow.
Exception in thread "main" java.lang.StackOverflowError, compiling:(/tmp/form-init7774655152686087762.clj:1:73)
at clojure.lang.Compiler.load(Compiler.java:7526)
at clojure.lang.Compiler.loadFile(Compiler.java:7452)
at clojure.main$load_script.invokeStatic(main.clj:278)
at clojure.main$init_opt.invokeStatic(main.clj:280)
at clojure.main$init_opt.invoke(main.clj:280)
at clojure.main$initialize.invokeStatic(main.clj:311)
at clojure.main$null_opt.invokeStatic(main.clj:345)
at clojure.main$null_opt.invoke(main.clj:342)
at clojure.main$main.invokeStatic(main.clj:424)
at clojure.main$main.doInvoke(main.clj:387)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:702)
at clojure.main.main(main.java:37)
Caused by: java.lang.StackOverflowError
at clojure.spec.alpha$regex_QMARK_.invokeStatic(alpha.clj:81)
at clojure.spec.alpha$regex_QMARK_.invoke(alpha.clj:78)
at clojure.spec.alpha$maybe_spec.invokeStatic(alpha.clj:108)
at clojure.spec.alpha$maybe_spec.invoke(alpha.clj:103)
at clojure.spec.alpha$the_spec.invokeStatic(alpha.clj:117)
at clojure.spec.alpha$the_spec.invoke(alpha.clj:114)
at clojure.spec.alpha$dt.invokeStatic(alpha.clj:742)
at clojure.spec.alpha$dt.invoke(alpha.clj:738)
at clojure.spec.alpha$dt.invokeStatic(alpha.clj:739)
at clojure.spec.alpha$dt.invoke(alpha.clj:738)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1480)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1491)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1491)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
etc.
Update :
OK. I've narrowed this down a bit in the repl.
Let's say a vector of points is defined so that pts is
[[-0.3 -3.6739403974420595E-17] [1.3113417037298127E-8 -0.2999999999999997] [0.2999999999999989 2.6226834037856828E-8] [-3.934025103841547E-8 0.29999999999999744] [-0.3 -3.6739403974420595E-17]]
Then calling
(spec/valid? :patterning.sshapes/points pts)
gives me the stack overflow :
StackOverflowError clojure.spec.alpha/regex? (alpha.clj:81)
So it looks like it just because I'm trying to match a spec/* of a spec/* of numbers.
Is there some reason that nested vectors trigger this kind of infinite recursion?
You should probably use spec/coll-of instead of s/* for this purpose:
(s/def ::point (s/coll-of number?))
(s/def ::points (s/coll-of ::point))
(s/def ::SShape (s/keys :req-un [::style ::points]))
(s/exercise (s/coll-of ::SShape))
;; => ([[] []] [[{:style {:hidden false, :bezier false}, :points [[1.0 -3.0 0 0.75 -1.0 -1.0 0 -1.5 1.0 3.0 -1 0] [-2.0 -1 2.0 2.0 0 ...
There are a couple of bugs in Clojure spec in this area, I believe.
This one looks like an instance of https://dev.clojure.org/jira/browse/CLJ-2002. It is triggered on conform:
(s/conform (s/* (s/* number?)) [[]]) ; => StackOverflowError
Maybe my question has already been answered but I am stuck with a submap specification.
Imagine I have two possibilities like that
{:type :a
:spec {:name "a"}}
{:type :b
:spec {:id "b"}}
In short: the :spec keys depends on the type. For the type :a, the :spec must contain the field :name and for type :b the spec must contain the field :id.
I tried this:
(s/def ::type keyword?)
(defmulti input-type ::type)
(defmethod input-type :a
[_]
(s/keys :req-un [::name]))
(defmethod input-type :b
[_]
(s/keys :req-un [::id]))
(s/def ::spec (s/multi input-type ::type))
(s/def ::input (s/keys :req-un [::type ::spec]))
This tells me: no method ([:spec nil]).
I think I see why: maybe type is not acccessible.
So I thought to make a multi-spec of a higher level (based on the whole map).
Problem: I do not know how to define :spec based on :type because they have the same name. Do you know how to perform this?
Thanks
(s/def ::type keyword?)
(s/def ::id string?)
(s/def ::name string?)
(s/def :id/spec (s/keys :req-un [::id]))
(s/def :name/spec (s/keys :req-un [::name]))
To accommodate the two different meanings for your :spec map, we can define those in different namespaces: :id/spec and :name/spec. Note that the non-namespace suffix of these keywords are both spec and our keys specs are using un-namespaced keywords. These are "fake" namespaces here, but you could also define these in other, "real" namespaces in your project.
(defmulti input-type :type)
(defmethod input-type :a [_]
(s/keys :req-un [::type :name/spec]))
(defmethod input-type :b [_]
(s/keys :req-un [::type :id/spec]))
(s/def ::input (s/multi-spec input-type :type))
(s/valid? ::input {:type :a, :spec {:name "a"}})
=> true
You can also get samples of this spec:
(gen/sample (s/gen ::input))
=>
({:type :a, :spec {:name ""}}
{:type :b, :spec {:id "aI"}} ...
Can anybody explain, what's wrong with the example below?
Why does it throw the StackOverflowError exception?
(s/def ::tag keyword?)
(s/def ::s string?)
(s/def ::n number?)
(s/def ::g
(s/cat :tag (s/? ::tag)
:ex (s/alt :string ::s
:number ::n
:and (s/+ ::g)
)))
(s/conform ::g '["abc"])
Similarly to what Alex Miller points out in this Google Groups discussion, s/+ tries to resolve ::g during the definition.
This should do what you want, I think:
(s/def ::g
(s/spec (s/cat :tag (s/? ::tag)
:ex (s/alt :string ::s
:number ::n
:and ::g))))
; REPL
user=> (s/conform ::g [:foo [:bar "abc"]])
{:ex [:and {:ex [:string "abc"] :tag :bar}] :tag :foo}
Lets say we have a macro which takes one required argument followed by optional positional arguments like
(require '[clojure.spec :as spec]
'[clojure.spec.gen :as gen])
(defmacro dress [what & clothes]
`(clojure.string/join " " '(~what ~#clothes)))
(dress "me")
=> "me"
(dress "me" :hat "favourite")
=> "me :hat favourite"
and we write it a spec for it like
(spec/def ::hat string?)
(spec/fdef dress
:args (spec/cat :what string?
:clothes (spec/keys* :opt-un [::hat]))
:ret string?)
we'll find that spec/exercise-fn fails to exercise the macro
(spec/exercise-fn `dress)
;1. Unhandled clojure.lang.ArityException
; Wrong number of args (1) passed to: project/dress
even though the data generated by the functions generator is accepted just fine by the macro:
(def args (gen/generate (spec/gen (spec/cat :what string?
:clothes (spec/keys* :opt-un [::hat])))))
; args => ("mO792pj0x")
(eval `(dress ~#args))
=> "mO792pj0x"
(dress "mO792pj0x")
=> "mO792pj0x"
Defining a function and exercising it the same way works fine on the other hand:
(defn dress [what & clothes]
(clojure.string/join " " (conj clothes what)))
(spec/def ::hat string?)
(spec/fdef dress
:args (spec/cat :what string?
:clothes (spec/keys* :opt-un [::hat]))
:ret string?)
(dress "me")
=> "me"
(dress "me" :hat "favourite")
=> "me :hat favourite"
(spec/exercise-fn `dress)
=> ([("") ""] [("l" :hat "z") "l :hat z"] [("") ""] [("h") "h"] [("" :hat "") " :hat "] [("m") "m"] [("8ja" :hat "N5M754") "8ja :hat N5M754"] [("2vsH8" :hat "Z") "2vsH8 :hat Z"] [("" :hat "TL") " :hat TL"] [("q4gSi1") "q4gSi1"])
And if we take a look at the built in macros with similar definition patterns we'll see the very same issue:
(spec/exercise-fn `let)
; 1. Unhandled clojure.lang.ArityException
; Wrong number of args (1) passed to: core/let
One interesting thing is that exercise-fn works fine when there's always one required named argument present:
(defmacro dress [what & clothes]
`(clojure.string/join " " '(~what ~#clothes)))
(spec/def ::hat string?)
(spec/def ::tie string?)
(spec/fdef dress
:args (spec/cat :what string?
:clothes (spec/keys* :opt-un [::hat] :req-un [::tie]))
:ret string?)
(dress "me" :tie "blue" :hat "favourite")
=> "me :tie blue :hat favourite"
(spec/exercise-fn `dress)
In other words: There seems to be some hidden arguments always passed to macros during normal invocation which aren't passed by spec. Sadly I'm not experienced enough with Clojure to know about such details, but a little bird told me that there are things named &env and &form.
But my question boils down to: Is it possible to spec a macro with named arguments in such a way that spec/exercise-fn can give it a good workout?
Addendum:
Wrapping keys* with an and seems to break exercise-fn again, even if it has a required named arg.
You can't use exercise-fn with macros as you can't use apply with macros. (Note that it's called exercise fn :).
This is exactly like (apply dress ["foo"]), which yields the familiar "can't take value of a macro". The different error message you see is because it's applying to the var rather than the macro, as what's really happening is like (apply #'user/dress ["foo"]).