Fork me on GitHub
#clojure-spec
<
2017-12-06
>
zalky00:12:56

Hey all: I'd like make some spec assertions regardless of whether check-asserts has been set. It's important these assertions are always made. To that end I'm looking to use assert*. It's part of the public api, but the doc-string says Do not call this directly, use 'assert'.. Is there something important that I need to know? Seems like it fits the bill otherwise.

Oliver George00:12:29

Could you do something like (asset (s/valid? xxx)) or (binding [s/*assert-flag-thing* true] (s/assert ::x 1)).

Alex Miller (Clojure team)04:12:10

you can use assert* but it’s api is subject to change as part of the implementation - it’s public because it needs to be accessible by the macro when expanded

hlolli19:12:23

I'm a complete beginner in clojure-spec, my question is, why don't I get spec error here?

(s/def ::spec1 string?)

(defn silly []
  1)

(s/fdef silly
  :ret string?
  :fn string?)

(silly)

taylor19:12:54

if you’re looking to assert that calls to silly have valid inputs, you can use instrument. Although that’s not going to care about your :ret or :fn function specs

hlolli19:12:08

I thought that was done with :args within fdef? I was hoping it would tell me that the number 1 is not string

taylor19:12:27

that’s right, but there’s no :args spec in your example

taylor19:12:40

and you still have to instrument the function for those assertions to happen

hlolli19:12:18

ah ok! thanks, I'm reading trough the manual and therefore had this question, I'll continue to read further.

taylor19:12:48

oh, if you’re only concerned about the return value of your function, instrument isn’t going to assert on that. You can use check for that though.

hlolli19:12:44

yes that's what I was looking for, but in real world cases I'd see spec being most helpful in the args.

bbrinck21:12:11

FWIW, Orchestra can be used to check ret/fn specs on instrumentation

hlolli22:12:56

@U053S2W0V how is it different from cljs.spec.test.alpha/instrument that said, that's exacly the reason why I want to use spec, to get these errors right away.

taylor22:12:14

instrument only checks the :args

taylor22:12:46

looks like you can use Orchestra to get the same effect for :ret and :fn specs though

hlolli22:12:50

ah, nice I see

hlolli22:12:05

also like their macros, very neat!