Fork me on GitHub
#clojure-spec
<
2017-02-13
>
dbushenko08:02:36

how to test side-effected functions with clojure.spec?

Oliver George08:02:33

That complicates things but there are some features you might like to check out. Pretty sure you can stub out functions. That might let you isolate you're logic for testing.

Oliver George08:02:45

I haven't used that feature myself.

not-raspberry10:02:39

@dbushenko Attach the spec as a validator to the var/ref/agent/atom, maybe... https://clojuredocs.org/clojure.core/set-validator!

joshjones12:02:03

@dbushenko @olivergeorge regarding stubbing a function — after fdefing the function, use

(stest/instrument `your-func {:stub #{`your-func}})
to use the :ret in the fdef instead of calling the real function, fyi

cgrand13:02:19

every doesn’t use res on its element predicate argument:

=> (s/form (s/every string?))
(clojure.spec/every
 string?
 :clojure.spec/kind-form
 nil
 :clojure.spec/cpred
 #object[user$eval8666$fn__8668 0x5ba5f0bd “user$eval8666$fn__8668@5ba5f0bd"])

I’ve found http://dev.clojure.org/jira/browse/CLJ-2035 which covers that but with a larger scope. @alexmiller Would it be sensible to open a ticket just for the resolution of the predicate?

Alex Miller (Clojure team)13:02:13

No, please add to 2035 if it's not already covered by the patch there

Alex Miller (Clojure team)13:02:02

I think there may actually be another ticket for this already though

Alex Miller (Clojure team)13:02:07

Nah, I was thinking of 2059 which is in explain-data

mss14:02:03

are js objects spec-able without converting to cljs data structures?

gfredericks15:02:02

you can always write arbitrary predicates that check whatever you want

sveri15:02:57

Hi, I have a list of maps and each map has an :idx key which is an int. Now what I want is that given one list every :idx is a unique int. Is there a way to enforce that?

frank15:02:03

(= (count my-list) (-> (map :idx my-list) distinct count))

sveri15:02:59

Cool, even exercise respects that function, thank you @frank

Alex Miller (Clojure team)16:02:25

@sveri the collection spec have a :distinct option

Alex Miller (Clojure team)16:02:52

Although I guess that goes farther than you want

piotr-yuxuan16:02:45

Hi here! Let’s say I’ve got a map of user. The key ::password must be present if and only if a user is new (the key ::new? is bound to a truthy value). Is it possible to express this with clojure.spec?

sveri17:02:47

@alexmiller I have seen that, but I suspect it means distinct for the whole map and not only for the :idx key

Alex Miller (Clojure team)17:02:32

@piotr2b: sure, use a custom predicate

Oliver George19:02:21

@mss if you can convert to clojure data structures first then you'll get access to more spec features. In particular s/keys & all the standard generators

gfredericks23:02:02

@sveri test.check has distinct-by generators for that sort of thing; I don't know if spec exposes it in the collection specs or not