This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-30
Channels
- # arachne (1)
- # beginners (1)
- # boot (13)
- # cljs-dev (35)
- # cljsjs (12)
- # cljsrn (11)
- # clojure (77)
- # clojure-austin (14)
- # clojure-brasil (3)
- # clojure-france (1)
- # clojure-poland (4)
- # clojure-russia (23)
- # clojure-spec (39)
- # clojure-uk (14)
- # clojurescript (17)
- # cursive (16)
- # datascript (10)
- # devcards (1)
- # dirac (3)
- # editors (1)
- # emacs (4)
- # jobs (1)
- # jobs-rus (2)
- # luminus (5)
- # off-topic (1)
- # om (85)
- # parinfer (1)
- # perun (12)
- # re-frame (20)
- # reagent (4)
- # spacemacs (1)
- # specter (9)
- # yada (6)
int?
will capture all fixed-precision integers. It would still be nice to have a bigint? pred so that the generator specifically produces bigints.
@lvh I don't mind the top level namespace for things that are pretty useful/general
Doesn't integer? map to that?
Oh you want just bigints
(s/and int? (s/conformer bigint))
perhaps?
boot.user=> (s/exercise (s/and int? (s/conformer bigint)))
([-1 -1N] [0 0N] [0 0N] [0 0N] [4 4N] [4 4N] [-12 -12N] [-45 -45N] [4 4N] [-63 -63N])
BTW @alexmiller I think the docstring on s/conformer
is very confusing about its intent...
"takes a predicate function with the semantics of conform i.e. it should return either a (possibly converted) value or :clojure.spec/invalid, and returns a spec that uses it as a predicate/conformer"
Because the function argument it takes isn’t really a "predicate function"… that makes it sound like it should be something like int?
...
…perhaps if it said "takes a conforming function i.e., it should return either a (possibly converted) value or :clojure.spec/invalid"…?
(predicate is pretty much otherwise used for functions that return truthy / falsey values in the Spec docs I think?)
&
and and
take predicates, fdef
’s :ret
, merge
, spec
(although there is mention in a couple of places that some of those "predicate" contexts may also conform the value… so it’s not entirely clear what’s intended)
@lvh @eggsyntax I tested and it looks like you can use (s/and (s/double-in …) (s/conformer bigdec))
if you wanted BigDecimals — and (s/conformer rationalize)
if you wanted ratios… and the example above produces big ints
Oh, simpler: (s/exercise bigdec?)
— didn’t realize there was a direct generator for that
and (s/exercise ratio?)
...
So bigint?
is kind of the only missing one...
Oh, interesting. I was thinking something roughly like (s/with-gen int? (gen/fmap #(bigint %) int?))
(that's just offhand & may have errors; I don't have a repl at hand).
Using s/conformer
seems simpler… but now Alex has made me question whether that’s entirely valid 😸
We use s/conformer
as a way to map input field formats to domain model formats along with specs for input fields. We’re now using specs as part of input validation and as part of our domain model testing.
Nice. I haven't really played with s/conformer
yet (although I'm certainly making use of s/conform
).
@seancorfield: great, thanks 🙂
@alexmiller: I’m OK with any integer; I didn’t realize integer? had been introduced and was using int?
integer? is old, int? is the new one
It seems that :ret
is not checked at all currently:
(defn foo [x] x)
(s/fdef foo
:args (s/cat :x int?)
:ret string?)
(stest/instrument `foo)
(foo 1)
However, when I run (stest/check
foo)`, it complains about wrong :ret
. Should it be this way, only checked while testing?
Oh, it seems to be a feature: https://groups.google.com/forum/#!msg/clojure/RLQBFJ0vGG4/K-5Rp6QXCgAJ
@dryewo: yes, instrument
is to test that functions are called correctly, check
is to test that functions are implemented correctly.