Rovanion
Rovanion

Reputation: 4582

Realistic Clojure Spec for function with named arguments

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?

Upvotes: 1

Views: 732

Answers (1)

Rovanion
Rovanion

Reputation: 4582

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")

Upvotes: 4

Related Questions