This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-14
Channels
- # announcements (1)
- # beginners (206)
- # calva (2)
- # cider (64)
- # cljs-dev (12)
- # clojars (2)
- # clojure (177)
- # clojure-europe (2)
- # clojure-finland (1)
- # clojure-italy (2)
- # clojure-losangeles (5)
- # clojure-nl (7)
- # clojure-russia (69)
- # clojure-spec (41)
- # clojure-uk (92)
- # clojurescript (60)
- # core-async (16)
- # cursive (48)
- # data-science (6)
- # datomic (73)
- # duct (5)
- # events (2)
- # figwheel-main (5)
- # fulcro (29)
- # hoplon (1)
- # off-topic (52)
- # pathom (11)
- # reagent (4)
- # reitit (5)
- # remote-jobs (1)
- # rum (7)
- # shadow-cljs (58)
- # slack-help (10)
- # spacemacs (3)
- # testing (3)
- # tools-deps (5)
Seems to work now. I’m generating regexes using a simplified regex spec (that exists solely for generating) and I’m generating strings that match it using test.check. I’m using these combinations to test re-find, etc. Thanks for the suggestions all 🙂
The only problem left is making it work on CLJS and self-hosted CLJS 😕 https://github.com/gfredericks/test.chuck/blob/master/src/com/gfredericks/test/chuck/generators.cljc#L244
@borkdude there's a PR for that 🙂
I'm not sure if it works though
PR seems out of date. Even if it’s incomplete I think it’s better than nothing? @wilkerlucio
you're suggesting I just merge it? I'm not sure what you mean by "out of date"
I wouldn't merge it, the range of things it works is quite narrow at this point, I'm using something simpler to generate strings, if we want to get that working in cljs (which would be awesome) that code needs better testing and impl, not good to merge as is IMO.
what I'm using for cljs these days is a much simpler string generator that knows just some basic patterns (numbers, letters, alphanum):
(ns string-gen
(:require [com.wsscode.test.chuck.charsets :as charsets]
[clojure.test.check.generators :as gen]))
(defn charset->gen [charset]
(let [size (charsets/size charset)]
(if (zero? size)
(throw (ex-info "Cannot generate characters from empty class!"
{:type ::ungeneratable}))
(gen/fmap (partial charsets/nth charset)
(gen/choose 0 (dec size))))))
(def type->charset
{"D" (charsets/range "0" "9")
"W" (charsets/union (charsets/range "0" "9") (charsets/range "a" "z") (charsets/range "A" "Z"))
"A" (charsets/range "A" "Z")})
(defn parse-int [x]
(js/parseInt x))
(defn token->gen [token]
(cond
(string? token)
(gen/return token)
(keyword? token)
(if-let [[_ t n] (re-find #"([DAW])(\d+)" (name token))]
(gen/fmap #(apply str %)
(gen/vector (charset->gen (type->charset t)) (parse-int n)))
(throw (ex-info "Invalid keyword token" {:token token})))
:else
(throw (ex-info "Invalid token" {:token token}))))
(defn string-gen [tokens]
(->> tokens
(mapv token->gen)
(apply gen/tuple)
(gen/fmap #(apply str %))))
I just realized that core.match could play nice with clojure spec conform results (in general, not related to this regex discussion)? https://stackoverflow.com/a/54687183/6264
i'm probably missing something obvious -- how can i define a spec so that its value conforms to one of a collection of specs? for example, a person can have a pet that's either a mammal or a fish, neither of which share a common attribute.
(s/def :animal.class/mammal (s/keys :req [:utters/count]))
(s/def :animal.class/fish (s/keys :req [:fins/count]))
(s/def :person/pet (s/or :animal.class/mammal :animal.class/fish))
(s/explain :person/pet {:utters/count 4})
val: #:utters{:count 4} fails spec: :animal.class/fish at: [:animal.class/mammal] predicate: (contains? % :fins/count)
=> nil
(s/or :animal.class/mammal :animal.class/fish)
=> (s/or :mammal :animal.class/mammal :fish :animal.class/fish)
borkdude, are you basically looking for bugs in the JVM's and/or JavaScript's regex library using spec and generative testing?
I wouldn't be surprised if you find bugs in those, and/or test.chuck's generate-a-string-matching-a-regex-from-a-regex code, or all of the above. Just so you realize that the time scale for fixing the JVM and/or JavaScript's regex library might be a bit long 🙂 Those libraries must be challenging to maintain.
The situation is like this: someone used Orchestra with speculative specs. Orchestra checks ret specs at runtime (in contrast to spec). It turned out the ret specs of regex functions were incomplete and I could have found this if I had used generative testing (which I have for almost all spec’ed functions except these). Now I have almost fixed this generative testing, but I ran into the CLJS limitation of test.chuck as a last issue.
I can bypass this by generating simpler strings for CLJS. It will still find the case I forgot to spec.
I’m not using it for anything more serious than this, just wanted to see how for I could go with it.
any planned changes (or options) about fn as argument triggering gen, something like wrapping their arguments with asserts instead so that they fail at invoke time ?
Is there a way to provide a custom generator for a spec distant to the spec's definition/local to the test context? The docs for instrument
say that the :replace
only relates to fn-specs
and :gen
option is only for stubbed vars
though could also do stest/check
- was it stest/check
that lets you also patch in your own generators?