Fork me on GitHub
#clojure-spec
<
2022-05-31
>
folcon12:05:19

I've been reminding myself how to use spec and one thing I can't remember is how to get better errors from this sort of thing?

(defn load-order [order-book {:keys [side id] :as order}]
  {:pre [(s/valid? :order/order order)]}
  (assoc-in order-book [side id] order))
Which does give me this:
Execution error (AssertionError) at fruit-economy.sim.market/load-order (market.clj:23).
Assert failed: (is (s/valid? :order/order order))
But considering it's a spec, I was expecting a little more? I've hacked it a bit by wrapping it in an is, which gives me:
FAIL in () (market.clj:2)
expected: (s/valid? :order/order order)
  actual: (not
           (s/valid?
            :order/order
            {:price 1,
             :size 425.0,
             :side :sell,
             :id 10229,
             :good-kw :inventory}))
But I'm sure there's a better way? EDIT: @jcf suggested using fdef to specify args and instrument, so I've swapped to doing that and calling instrument on debug as so in my core ns:
(when (debug?)
  (let [instrument (requiring-resolve 'clojure.spec.test.alpha/instrument)]
    (instrument 'fruit-economy.sim.market/load-order)))
To be honest this is pretty great, but perhaps there's something I'm missing? So I thought I'd pop the question here! REF: https://github.com/Folcon/fruit-economy/commit/a92b4b5329eabd7347240804f04224b6440ebdb2

jcf17:05:24

You rang? 🙈

folcon17:05:48

Oh not ringing 😃, attributing 😉

❤️ 1
jcf17:05:30

Thanks for the shout out, @U0JUM502E! 🙇

1