Fork me on GitHub
#clojure-spec
<
2017-06-01
>
Alex Miller (Clojure team)00:06:03

like

(gen/sample 
  (gen/fmap (fn [[name host]] (format "%s@%s" name host)) 
            (s/gen (s/tuple #{"fred" "tom" "mary"} 
                            #{"" ""}))))

Alex Miller (Clojure team)00:06:35

@misha I’d echo Sean’s advice -either don’t spec the ids, or spec them with an s/or (validation and gen will work better then), or combine an entity spec with an additional id spec if needed in some places, etc.

jjttjj04:06:25

thanks all

stbgz06:06:54

hey all I was wondering if there is a tool around that would take a swagger spec and transform it to a clojure spec

stbgz06:06:29

I am interested in generating test for api specified in swagger

misha08:06:33

@alexmiller is your f/map approach superior in some way to the following? Or is it just a matter of taste?

;;clojure.test.check.generators/let
(gen/sample
  (tgen/let [name (s/gen #{"fred" "tom" "mary"})
             host (s/gen #{"" ""})]
    (format "%s@%s" name host)))

grierson09:06:02

How do I define a spec with a generator? e.g. (spec/def ::letter (spec/and (gen/char-alpha) #(Character/isUpperCase %)))

grierson09:06:20

When I try to exercise the def, I get an error

grierson09:06:06

When I evaluate the def it works.

andrewmcveigh09:06:03

I think the spec/gen in your custom generator is the problem

andrewmcveigh09:06:31

Is (gen/char-alpha) not already a generator?

grierson09:06:26

It works when I (gen/sample gen/char-alpha)

misha09:06:28

(spec/def ::letter (spec/with-gen
                     (spec/and char? #(Character/isUpperCase %))
                     #(gen/char-alpha)))
(spec/exercise ::letter)
=>
([\S \S] ...)

grierson09:06:21

@misha ❤️ I didn't need the (spec/gen)

grierson09:06:06

I was trying to generate a generator :@

ikitommi13:06:12

@stbgz have been waiting for someone to do the json schema -> spec converter too. Would also enable things like server code-gen from swagger-spec to a clojure(.spec) web server. Might help the enterprise adoption.

ikitommi13:06:35

there are spec-tools & speculate which do the spec -> json schema. and spec-swagger for spec -> swagger (only 80% done)

danielneal16:06:28

is there a recursive version s/describe which returns definitions of any specs referenced in the top level spec? Kinda like a macroexpand-all?

royalaid17:06:52

So I am experimenting with setting up specs for http://aleph.io/manifold/deferreds.html

royalaid17:06:09

Because I want to try and add specs for aleph responses

royalaid18:06:54

Part of the problem is because deferred are used encapsulate async code and unrealized values the only way I can think of to check the spec is to use a wrapper function that unwraps the value and checks the spec

wilkerlucio19:06:04

@grierson recommendation: use s as the alias instead of spec, I say that because it's a standard name for everybody, going away from well defined conventions is not a good ideia

wilkerlucio19:06:43

@grierson yeah, this article is a bit contrived, I recommend you check this one from Stuart Sierra: https://stuartsierra.com/2016/clojure-how-to-ns.html

grierson19:06:21

@wilkerlucio Thank you, I will check it out.

wilkerlucio19:06:01

you'r welcome 🙂

bfabry19:06:20

@grierson that article has the right idea but we make exceptions for very-widely-used libraries. in those cases there's often a shorthand convention rather than following the rule

Alex Miller (Clojure team)19:06:48

@misha one benefit of my approach is that I use only generators built from clojure.spec.gen.alpha, which uses dynamic loading, which means that you don’t need test.check at runtime to load the namespace. other than that, personal preference.

Alex Miller (Clojure team)19:06:18

@danieleneal no, there is not a deep-describe, but it’s something we’ve talked about adding. I think having spec specs (CLJ-2112) would be a big help for this to generically walk specs to find sub specs, so I’ve kind of filed it behind that one. Another alternative would be to actually support this as some kind of recursive op built into the protocol.