This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-03
Channels
- # adventofcode (198)
- # aleph (10)
- # announcements (7)
- # aws (17)
- # beginners (353)
- # boot (1)
- # calva (13)
- # cider (18)
- # cljdoc (2)
- # cljs-dev (11)
- # cljsrn (1)
- # clojure (87)
- # clojure-austin (1)
- # clojure-brasil (2)
- # clojure-greece (13)
- # clojure-italy (18)
- # clojure-kc (2)
- # clojure-nl (9)
- # clojure-quebec (1)
- # clojure-russia (1)
- # clojure-spec (55)
- # clojure-uk (114)
- # clojurescript (18)
- # clojurex (14)
- # code-reviews (5)
- # core-async (17)
- # cursive (23)
- # data-science (1)
- # datomic (82)
- # docker (8)
- # duct (10)
- # emacs (8)
- # figwheel (3)
- # figwheel-main (5)
- # fulcro (13)
- # hyperfiddle (8)
- # jobs (1)
- # midje (1)
- # mount (1)
- # nrepl (2)
- # off-topic (72)
- # om (2)
- # pathom (10)
- # portkey (2)
- # re-frame (9)
- # reagent (3)
- # reitit (9)
- # ring-swagger (14)
- # schema (1)
- # shadow-cljs (91)
- # spacemacs (21)
- # sql (6)
- # tools-deps (19)
- # unrepl (9)
- # vim (41)
Does anyone know of a tool where I can give it a datastructure, and it will generate a spec for me based upon that datastructure?
I feel like I’m a little rusty with spec, and getting tripped up by something… Can anyone see what I’m doing wrong here?
(s/fdef probably-diagram-yaml?
:args (s/cat :v (s/or :is-diagram :ss/diagram-yaml-str
:is-not-diagram string?))
:ret boolean?)
When I test this function with stest/check
I’m seeing values generated via string?
being validated against :ss/diagram-yaml-str
— is that what’s supposed to happen? If so… why? (This is probably just PEBKAC)What's the generator for :ss/diagram-yaml-str
look like?
(I would expect that to also have string?
in it based on its name)
The thing is, whenever I exercise
that spec it succeeds just fine. And… hmm, well, I hope that exercise
does validate the generated values against the spec 😅
Generator:
#(gen/fmap
(fn [diagram]
(str (sometimes (str seyaml/default-front-matter doc-separator))
(seyaml/stringify diagram)))
(s/gen ::st/diagram))
and sometimes
is:
(defmacro sometimes [body]
`(when (< (rand) 0.5)
~body))
FYI you’ll probably want to replace that sometimes
with one of test.check’s generators, otherwise it’s an unbound/uncontrolled source of randomness to test.check
that’s my untested assumption 🙂 and there might be an even more appropriate function in test.check I’m forgetting/don’t know about
based on the link you provided, looks like I should use (gen/return nil)
rather than just nil
I suppose you could use gen/elements
too, if your values are static/not-generated. I suppose elements
given a two-element coll would choose between them equally?
I wonder if this is on the right track:
#(gen/fmap
(fn [diagram]
(str
(gen/frequency
[[1 (gen/return nil)]
[1 (gen/return (str seyaml/default-front-matter doc-separator))]])
(seyaml/stringify diagram)))
(s/gen ::st/diagram))))
looks pretty reasonable to me. I still wonder if elements
would be better suited in this case b/c it'd definitely be more concise
Ah yes, peeked at the source of exercise
and it calls conform
on all generated values. (It’s mentioned in the docstring too.)
I think this is starting to dawn on me… I think maybe (I’d like to think because I’ve been rusty) I’ve just been not quite grokking s/or
… when a value is conformed against an s/or
spec it will quite naturally be conformed against each scenario in order. And after a value is generated via a generator, I guess there’s no mechanism at that point to conform it straight off against its original scenario spec… in other words, s/or
isn’t like pattern matching, which I guess was for some reason how I was trying to use it.
I think it's pretty conceptually similar to pattern matching, but I'm not sure I follow this issue: >And after a value is generated via a generator, I guess there’s no mechanism at that point to conform it straight off against its original scenario spec
BTW one way I think s/or
differs from pattern matching like in ML languages is that s/or
clauses don't have to be exhaustive by default. Don't have to be non-overlapping either
er, yeah, sorry. I think it was just my rusty thinking.
• An or
spec is a spec, and (I suppose) most functions that work with specs will treat/use an or
spec as an opaque spec, like any other spec.
• Accordingly, the workflows of conform
and stest/check
have no affordances for taking values generated from the or
spec and influencing how those values are conformed
against the or
spec and its internal scenarios
• Accordingly, values generated by any given scenario of an or
spec might be conformed against any other given scenario of an or
spec, because that’s how an or
spec works — values are conformed against its scenarios one by one in order
• The only reason I got caught up on this is because one of my specs would sometimes throw an exception when passed an invalid value
Yeah I’ve been bitten using a predicate in an or that wasn’t nil safe. Order matters in some cases
I feel like I need a couch to lie down on and someone with a pipe to nod and doodle on a notepad
hmm, have a strange question: how to actually spec that something is a sentence? or write a generator for a good sentence? (let's say lorem ipsum language with limited number of words is enough)
just realized im specing stuff as string? but would like my generators to be clever and generate usernames as random strings but description as some sort of lorem ipsum with spaces. if this question is too beginner like for this channel, let me know pls.
If you mean actual grammatically correct English, or some other natural language, then you need some kind of NLP library or a product of machine learning for that. If by "sentence" you mean something else, please clarify.
and spec explicitly isn't a string parsing library - anything for differentiating one string from another will be wrapped up and just be another function as far as spec is concerned
Sorry, you did mention "some sort of lorem ipsum with spaces", but still might need some clarification of exactly what kind of strings you want to include vs. exclude. A regular expression could be written over strings that matches most lorem ipsum like text, but rejects things that contain weird special characters.
If you did that, and then wanted to generate random examples for testing, you would likely only be able to do so by writing a custom string generator, because otherwise the default test.check string generator would, with very high probability, generate strings that do not satisfy the spec.
cool. I probably saw that mentioned before and forgot about it. Such a thingamabob sounds pretty non-trivial, unless you found a library that transforms regexes into state machines for you.
it was nontrivial
the most nontrivial parts were punted on (esp. lookahead, lookbehind)
Makes sense. Common regex libraries include so many features like that, that complicate analyzing them (and IMO, understanding what they do)
one big hard part was parsing the regexes correctly, and the other one was handling characters correctly
I saw a quote similar to the following (paraphrasing from memory) in the early 1990s in someone's signature on a public Internet forum and loved it: "Ain't nothin's easy when you're doing it fer real! -- a gunnery sergeant in the U.S. Army"
well some things are, but they're not very interesting to talk about