This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-29
Channels
- # ai (2)
- # beginners (12)
- # bitcoin (1)
- # boot (41)
- # chestnut (5)
- # cider (9)
- # clara (24)
- # cljs-dev (11)
- # clojure (107)
- # clojure-dev (2)
- # clojure-italy (4)
- # clojure-nl (4)
- # clojure-russia (10)
- # clojure-spec (19)
- # clojure-uk (71)
- # clojurescript (121)
- # cursive (3)
- # data-science (7)
- # datacrypt (1)
- # datomic (72)
- # docs (7)
- # duct (2)
- # emacs (3)
- # ethereum (1)
- # figwheel (1)
- # fulcro (58)
- # graphql (16)
- # hoplon (9)
- # jobs (2)
- # jobs-rus (1)
- # lein-figwheel (1)
- # leiningen (25)
- # luminus (2)
- # lumo (5)
- # off-topic (6)
- # onyx (22)
- # pedestal (3)
- # portkey (1)
- # proton (2)
- # re-frame (7)
- # remote-jobs (1)
- # ring-swagger (3)
- # rum (2)
- # shadow-cljs (38)
- # specter (7)
- # yada (30)
(s/def ::kebab-str
(s/with-gen
#(= %1 (csk/->kebab-case %1))
#(gen/fmap clojure.string/join (gen/list (gen/frequency [[1 (s/gen #{\-})] [9 gen/char-alpha]])))))
core> (s/gen ::kebab-str)
#clojure.test.check.generators.Generator{:gen #function[clojure.test.check.generators/such-that/fn--17362]}
and the result is usuable only for a few elements:
core> (gen/sample (s/gen ::kebab-str) 10)
("" "" "i" "rc" "sh" "s" "gh" "q" "" "od")
and not for many:
core> (gen/sample (s/gen ::kebab-str) 100)
ExceptionInfo Couldn't satisfy such-that predicate after 100 tries. clojure.core/ex-info (core.clj:4725)
@johanatan Generators always validate against the spec. Your generator probably generates values that do not satisfy your predicate.
What does your predicate return for "-a"
or "---"
?
@seancorfield ah, good call. looks like first digit needs to be non-dash
@seancorfield interesting that I wasn't getting a more obvious exception though (which I did get with a more egregious disparity between the validator and generator)
FYI... this version works:
(s/def ::kebab-str
(s/with-gen
#(= %1 (csk/->kebab-case %1))
#(gen/fmap (comp clojure.string/lower-case clojure.string/join)
(gen/such-that (fn [c] (and (not-empty c) (not= \- (first c)) (not= \- (last c))))
(gen/list (gen/frequency [[1 (s/gen #{\-})] [9 gen/char-alpha]]))))))
You probably ought to look at Gary Fredericks' test.chuck library -- it contains a generator for regex strings which would make this a lot simpler!
Aside from anything else, you could then define ::kebab-str
as a regex predicate and use test.chuck's regex generator directly on the same regex.
Hello! Any ideas on what I’m doing wrong here. (s/valid? ::bar-path ["foo" 1])
is ok, but not so in an s/fdef
(s/def ::foo-path
(s/cat :foo (partial = "foo")))
(s/def ::bar-path
(s/cat :foo ::foo-path :idx nat-int?))
(s/valid? ::foo-path ["foo"]) ; => true
(s/valid? ::bar-path ["foo" 1]) ; => true
(s/fdef my-fn
:args (s/cat :path ::bar-path)
:ret int?)
(defn my-fn
[path]
(second path))
(stest/instrument)
(my-fn ["foo" 1]) ; => my-fn did not conform to spec
@amar The s/cat
calls combine to make one sequence regex. I think you can say (s/spec ::bar-path)
as the :path
argument spec and it will work.
You might also want to look at s/tuple
for your ::bar-path
spec instead of s/cat
.