Fork me on GitHub
#clojure-spec
<
2018-05-02
>
triss14:05:15

so how do I stop stest/check from generating NaNs all the time?

triss14:05:31

the spec that fails is: (s/and number? #(<= 0 % 100))

Alex Miller (Clojure team)14:05:37

do you really need number? ? or is something narrower like int? appropriate?

Alex Miller (Clojure team)14:05:06

it is a known issue right now that NaNs cause issues for certain generators and something we intend to fix

triss14:05:54

ah ok. I’ll see if int-in works out for me. thanks.

Alex Miller (Clojure team)14:05:04

#(not (Double/isNaN %)) pred may be useful to filter too

guy14:05:40

Sounds silly but does the order matter inside the s/and

guy14:05:49

in the NaN example

guy14:05:59

would you have to have #(not (Double/isNaN %)) second?

guy14:05:09

then #(<= 0 % 100) last?

Alex Miller (Clojure team)15:05:54

yes, order matters as the first spec generates, then each subsequent one filters, so the order you suggest is probably good

guy15:05:09

ok thanks!

eoliphant14:05:34

hi I have , I guess a ‘phlisophical’ lol question about spec. Im working on a system that uses datomic, etc. So, I’d gone though and created a bunch of specs, now I’m building out datomic schemas, using my specs to name my attributes, etc. So of course, some of this isn’t especially DRY. I’m just wondering about creating my own (or using umlaut, etc) lightweight DSL that in turn gen’s datomic schemas, specs, etc. Is this the’right’ way or is starting from specs ‘better’, etc. Just wondering

kenny20:05:48

@eoliphant We wrote a library to do this called Spectomic https://github.com/Provisdom/spectomic and have been using it successfully for about a year now.

👍 4
Alex Miller (Clojure team)15:05:29

some people have done this to various degrees

Alex Miller (Clojure team)15:05:46

hard for me to say if they found that to be a win in the long run or not

👍 4
robert-stuttaford15:05:07

@eoliphant Datomic schema is not the same as a spec. they do similar things - link behaviour to a name, but they are not the same thing (they create different behaviour). conflate them only if you are confident that in doing so, users of your new combined API understand the consequences of this clearly. semantically, they are different. i find i prefer a little extra typing, and having decoupled systems that use very well documented APIs.

👍 4
robert-stuttaford15:05:35

my hobby project https://github.com/robert-stuttaford/bridge keeps them separate, and i don’t think it actually costs anything to do that

robert-stuttaford15:05:57

so i guess the question is, are you actually Repeating Yourself? 🙂

triss15:05:34

So I’ve got nice s/fdef’s for my functions. Is it possible to use those in a test?

triss15:05:47

I know

(-> (stest/enumerate-namespace 'fca.slipnet.math)
      (stest/check))

triss15:05:09

but when I run tests from CIDER it tells me there a no tests defined.

robert-stuttaford15:05:27

oh, that’s a different question 🙂

triss15:05:03

So stest/check returns a :clojure.test.check.clojure-test/trial how do I tell clojure.test to look out for it?

eoliphant16:05:51

yeah that’s kind of the way I was leaning @robert-stuttaford

javi16:05:14

Hi everyone, new to the channel. Does anyone know of any articles on best practices /process to spec a pre-existing codebase? I know spec, but so far haven't used it much and I have a "not-small" cljs codebase that i want to start spec-ing, but not sure where/how to start...

guy16:05:22

oh sorry right larger codebases sorry no

javi16:05:24

thanks!!

Alex Miller (Clojure team)16:05:33

start by writing specs :)

👍 4
Alex Miller (Clojure team)16:05:58

if you have a map that is used a lot, write specs for the attributes

Alex Miller (Clojure team)16:05:13

then write an s/fdef for a function that takes that map

Alex Miller (Clojure team)16:05:46

if you want to know where to start, pick the most common/stable data you have

Alex Miller (Clojure team)16:05:52

work outward from there

Alex Miller (Clojure team)16:05:13

or pick the thing you trust the least and add constraints around it by making specs and using stest/instrument

Alex Miller (Clojure team)16:05:49

if you get an instrument failure, you’ve learned something (either your data does things you didn’t anticipate, or something is wrong)

Alex Miller (Clojure team)16:05:00

if the former, fix your spec. if the latter, fix your code.

Alex Miller (Clojure team)16:05:16

if you have data transformation functions, write specs for the inputs and outputs and use stest/check

Alex Miller (Clojure team)16:05:31

these are ideas, not a plan

👍 8
🙂 4
javi16:05:32

> or pick the thing you trust the least and add constraints around it by making specs and using stest/instrument ha ha, good one 🙂 i think i ll start there. thanks!

robert-stuttaford16:05:16

one very useful practice is to spec anything before you make changes. we have code from clojure 1.6 days that we do this to, and it really helps deal with stuff like regression testing, safe refactoring, etc

👍 4
djtango20:05:25

Hey folks, probably a noob question but am really struggling to find an answer via search (probably because of my lack of terminology): If I define a spec using a non auto-resolved keyword (s/def :my/spec any?) how do I use it if I've required that namespace in a separate namespace? I can't get a call to (s/valid? :my/spec 1) to resolve nor does (s/valid? :: 1)

Alex Miller (Clojure team)20:05:09

the latter is not valid

Alex Miller (Clojure team)20:05:17

not a valid keyword that is

Alex Miller (Clojure team)20:05:54

user=> (ns foo (:require [clojure.spec.alpha :as s]))
nil
foo=> (s/def :foo/thing int?)
:foo/thing
foo=> (ns bar (:require [clojure.spec.alpha :as s] foo))
nil
bar=> (s/valid? :foo/thing 10)
true

djtango22:05:33

orz it works now. I think I must have had something else wrong in the file - thanks @alexmiller