Fork me on GitHub
#clojure-spec
<
2018-03-05
>
seancorfield02:03:28

@mpcarolin You can use back tick you expand a symbol's name:

(-> (test/check `my-index-of) (test/summarize-results))
will probably work.

mpcarolin02:03:52

@seancorfield Hey, thanks! I did not know that existed. It will save me a lot of redundant typing.

tianshu06:03:37

how to write a spec allows only the specified keys? is there a convenient way?

tianshu06:03:08

I want to use spec to constraint the schema of edn which used as a configuration file. I want useless keys are not allowed.

stathissideris09:03:50

@doglooksgood there is no official way, you use s/and with s/keys and another predicate that constrains the keys to be in a specified set, but then you are listing the keys twice. Some people have written macros for this sort of thing

stathissideris09:03:27

but spec is like that by design, probably the decision that has caused the most controversy about it

tianshu09:03:41

so I should write something like s/keys, okay

Andreas Liljeqvist14:03:12

Asking because it is like 3 months since the last commit

bronsa14:03:50

clojure repos usually see activity in bursts not continuous

bronsa14:03:10

apart from initial development

Andreas Liljeqvist14:03:02

ok, thanks. Looking to fix a bug

Andreas Liljeqvist14:03:18

Now I just have to figure out how it is built, I expected a .deps.edn,project.clj, build.boot, makefile....

Alex Miller (Clojure team)14:03:16

It’s maven - you can test with mvn clean test etc

Andreas Liljeqvist14:03:11

got it to run with mvn clojure:compile

Andreas Liljeqvist14:03:49

going with mvn clean test since I want to fix a problem

Nicolas Boskovic16:03:20

Is there a way to make a function return an error if parameters don't conform to their given spec?

mpcarolin17:03:32

I think what you’re looking for is spec.test/instrument. Once you execute it, it will tell you if any :args specs are violated for a given function.

mpcarolin17:03:47

E.g (stest/instrument ‘foo)

mpcarolin17:03:21

But if you want it at runtime without instrument, you can also use :pre condition for a function with s/valid?

Nicolas Boskovic16:03:37

The function's given spec, anyways

gfredericks17:03:53

does clojure 1.9 come with specs for clojure core functions somehow?

taylor17:03:15

there must be some coverage in there, here’s a spec error for invalid let:

CompilerException clojure.lang.ExceptionInfo: Call to clojure.core/let did not conform to spec:
In: [0] val: () fails spec: :clojure.core.specs.alpha/bindings at: [:args :bindings :init-expr] predicate: any?,  Insufficient input
 #:clojure.spec.alpha{:problems [{:path [:args :bindings :init-expr], :reason "Insufficient input", :pred clojure.core/any?, :val (), :via [:clojure.core.specs.alpha/bindings :clojure.core.specs.alpha/bindings], :in [0]}], :spec #object[clojure.spec.alpha$regex_spec_impl$reify__2436 0x482c71e3 "clojure.spec.alpha$regex_spec_impl$reify__2436@482c71e3"], :value ([x] (prn x)), :args ([x] (prn x))}, compiling:

gfredericks17:03:22

well that's a macro, which is a pretty different use case (in particular, it's checked at compilation; function specs would only get involved if a user intentionally instrumented them)

gfredericks17:03:34

(or are there official specs somewhere else?)

gfredericks17:03:51

I guess they'd probably be in core.specs.alpha if anywhere, and I see those are basically just a handful of macros

bronsa17:03:45

that’s all there is available

gfredericks19:03:30

surely somebody somewhere is assembling a non-official collection of such things?

bbrinck20:03:04

The two I know of are https://github.com/leifp/spec-play and https://github.com/tirkarthi/respec . I have used neither but @alexmiller has pointed out errors in those specs

bbrinck20:03:32

But they might be sufficient for some perf testing

bbrinck20:03:36

If we had a large enough test suite across enough projects, could we infer “good enough” specs for core functions with https://github.com/stathissideris/spec-provider ? :thinking_face:

bbrinck20:03:12

I understand that for general usage, “good enough” is not really a concept, or we’ll get confusing false positives and false negatives 🙂

bbrinck20:03:50

But in my case, “good enough” specs are useful so I can see if error messages can be improved

stathissideris08:03:08

@U08EKSQMS spec inference for functions is still work in progress in spec-provider (but not too far off). I suspect it wouldn’t be able to give you anything very useful if you ran it on map though (which I think is very tricky to write a spec for anyway)

gfredericks19:03:59

I wonder how much it would slow down a test to instrument all of clojure.core 😄

gfredericks20:03:59

do you think some aspect of this will be useful for some things at some point?

Alex Miller (Clojure team)20:03:02

I have worked with portions of core speced and it’s really useful at the repl

gfredericks20:03:41

do you think there's a reasonable place to draw the line that maximizes utility and speed?

Alex Miller (Clojure team)20:03:43

I think it’s mostly not useful in a test suite (if the suite already runs)

Alex Miller (Clojure team)20:03:01

I don’t feel ready to answer that yet

cap10morgan19:03:42

I have a utility fn that can operate on any map, so its :args spec is just (s/cat :v (s/keys)). But when I do generative testing with that, I get a bunch of empty maps (which makes some sense I suppose). What's a good way to make that generate a bunch of random maps instead (ideally w/ an emphasis on namespaced keyword keys)?

cap10morgan19:03:52

Should I just use map? instead of s/keys?

gfredericks19:03:12

sounds plausible I'm of the opinion that virtually all uses of maps fall into two nonoverlapping categories, roughly corresponding to s/keys and s/map-of

gfredericks19:03:25

map? seems to not commit to either

cap10morgan19:03:58

yeah, map? seems better

bbrinck20:03:21

@alexmiller Are specs for the core library something that contributors could help with? In other words, are they blocked on dev/reviewer time, or are they more fundamentally blocked on any upcoming changes in spec?

Alex Miller (Clojure team)20:03:23

They are really blocked on Rich review time. have written some of them but don’t have them public anywhere

Alex Miller (Clojure team)20:03:59

They tend to raise many questions with issues of taste and judgement

bbrinck20:03:42

I see, that indeed seems like it would require quite a bit of time to review

bbrinck20:03:52

and change accordingly

Alex Miller (Clojure team)20:03:06

Eric Normand actually was working on some of this at one point and I gave him some ideas of things to do with it

bbrinck20:03:32

He was working on official specs? Or on his own set?

bbrinck20:03:14

I understand the issues of taste will take time to resolve, but to the extent that the official specs need testing, I’m sure the community could provide feedback there

Alex Miller (Clojure team)20:03:24

He wanted to help and so I told him what would be helpful

bbrinck20:03:24

Perhaps correctness is much easier than making idiomatic specs that conform/generate cleanly 🙂

gfredericks20:03:42

I doubt correctness is easy either

seancorfield20:03:47

And there's also the issue that some functions are extremely hard to spec correctly...

bbrinck20:03:09

Correct, although that’s one place where the community could help - install specs, run tests, see what breaks

Alex Miller (Clojure team)20:03:40

I don’t remember now what that was but I think I asked Eric to help in categorizing what was out there. I think he did but it was a while ago

Alex Miller (Clojure team)20:03:42

My experience in testing core specs is that it’s extremely difficult to test more than one at a time

bbrinck20:03:40

Hm, that’s a good point. Since we can see the list of isntrumentable symbols, a project could loop over them, instrument each one in isolation, and run tests

bbrinck20:03:56

that way, if there was a failure, it would be isolated from other specs.

bbrinck20:03:40

again, if it was useful to get broader validation of specs by running across a large variety of project

Scot22:03:19

Hi, is there a good way to spec anonymous functions? I am trying to use fspec, but I keep getting Cannot read property for_all_STAR_ of undefined

seancorfield23:03:33

@scot-brown Do you have org.clojure/test.check as a dependency?

seancorfield23:03:19

In order to check whether the function conforms to the spec, it is tested used a generative approach, so you need test.check in your project for that (`clojure.spec` will try to load it dynamically and you'll get some odd-looking failures if it isn't available)