Fork me on GitHub

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?


Thats wonderful, thank you!

🤝 4

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)


It’s a little crazy


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 😅



      (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)


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


Great tip — thanks so much!

👍 4

maybe something like (gen/frequency [[1 (gen/return "thing")] [1 (gen/return nil)]])


ah that’d work? equal frequencies means equally likely?


(I didn’t look at the docstring for frequency yet)


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


oh yeah, sorry… my example had concrete values instead of generators, fixing

aw_yeah 4

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 dunno what the distribution is


IIRC some test.check generators don’t use even distributions


I wonder if this is on the right track:

  (fn [diagram]
        [[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




thanks for helping me improve my code!


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


I hope that might begin to approach making some kind of sense


Yeah I’ve been bitten using a predicate in an or that wasn’t nil safe. Order matters in some cases


yes, exactly


my spec calls a function that throws if passed an empty string


related to a regex search


I feel like I need a couch to lie down on and someone with a pipe to nod and doodle on a notepad

🛋️ 4
🙏 4

sorry for the noise!


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.


test.chuck (different lib) has a regex -> generator thingamabob


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


thanks for explanation, you all above confirmed what i thought. need to dive deeper into generators, check possibilities and decide what i really wanna do 🙂 basically i want a fixtures generator for particular domain. hence need for sentences.


is there an existing generator for normal distribution from given range?