clojure-spec 2023-04-25

I have spent some time searching for information about clojure.spec.alpha.test/check and how to wrap it up so it is used in a test run. So how can I wrap around an above (stest/check mysymbol)` in a (deftest)?

(disclaimer: it's been a while since I've used spec) isn't the return value of stest/check just data? Can't you just verify it?

yes i can. i can wrap it up and dress in an itbody, and request the return data result key to be a :success. or whatever. It just seems like making a bit of a piruette...

so i lost my confidence and start questioning my reasons...

There's https://clojure.org/guides/test_check_beginner#_defspec in test.check for when you have generators providing the values and the actual function under test. but I don't think that's applicable to fspec's (or if there's some similar defsomething macro) for them.

yes. defspec seems like "half way there"

It very well mignt be that fspec is not really a 'testing' area but more for instrumentation and repl.

I'm not sure I'd agree. My understanding is that instrumentation is there to verify that other code invokes your function correctly (ie. it checks the :args part of fspec), but verifying that your function actually does what it's fspec promises (the :ret and :fn parts ) is the responsibility of s/check (= testing)

(t/deftest mean-invariant-properties
  (t/testing "Should pass fspec exercise"
    (t/is (nil? (:check-failed (stest/summarize-results (stest/check `mean)))))))

so this works. but it does not look 'good'

usually stuff that looks wrong, or is too much work ends up being me trying to force round pegs in square holes. hence me asking 😂

Looks ok to me 🤷‍♂️ I guess you could try minimizing the code if you really want to; remove the t/testing part and perhaps create some helper fn, which does the e.g. the (nil? (:check-failed (stest/summarize-results part to make it shorter.

yeah, but that is not even "correct". it also might throw and there is another keyword for checking that... It is not a big deal wrapping it in small macro and attaching it by invoke and pass of a namespace... I am just surprised and worried that is not there.

more worried. 🙂 but thanks for sharing your views on that. 🙇

flying solo often has me questioning my understanding, especially for new areas 🙂

a very similar discussion from a few years ago: https://groups.google.com/g/clojure/c/0UjSFT926vg 🙂

but yeah, it seems there's no out-of-the-box integration with fdef and clojure.test. Though it seems some test libraries (kaocha) bridge the gap and try to make it easier. But in any case I wouldn't feel very worried about it. My guess is that stest/check was designed to have a strict scope and return the data as data and it's up to you to decide what to do with that data.

👍 1

or the reason i can not find that is that is not supposed to be done?

One can argue that property tests as invaraibly a bit random are not a great fit for pipelines...?

are pre/post maps in a function my best bet for spec'ing input/output of parsing datetimes from strings? for instance I have this relatively simple conversion (-> "2023-10-27T09:00:00.000Z" Instant/parse (OffsetDateTime/ofInstant (ZoneId/systemDefault))) but unfortunately the API I'm consuming does not include the Z at the end. Is it better to have two specs, one a regex pattern for input and one a call to #(instance? OffsetDateTime %) as output?

Hi. Could you use an s/or spec and conform, and then do what is needed based on the result of that conform? You could provide different predicates using different regexps (or even use instance? so your fn can take in lots of different things) and then conform, and then use the result to operate on the result. You'd parse one way for one type, or another for another type. Or am I misunderstanding your question? It sounds as if this isn't necessarily a spec question but a polymorphism question in which you want to act on a variety of different possible inputs?

I think the input will always be uniform unless the controller of this API is changing it, which is out of my hands. I think using conform could be the right path forward, but I'd have to go back and look at this code. Haven't had a chance in a while. Thanks for the suggestion.

I didn’t spot that your question was from April sorry!

No problem! I appreciate the reply 🙏🏼