Search code examples
unit-testingtestingclojuretest.check

How do I write a generator to make an alpha string using test.check?


test.check has a built in generator gen/string-alphanumeric but it does not have a gen/string-alpha. How do I make a generator that will make strings only composed of letters and no numbers?


Solution

  • You can use gen/char-alpha with gen/fmap to make a string.

    (gen/fmap clojure.string/join (gen/vector gen/char-alpha))
    

    If you need to use this a lot, I recommend binding it. Example:

    (def gen-string-alpha
      (gen/fmap clojure.string/join (gen/vector gen/char-alpha)))