In using test.check I need a generator for strings of a certain length. Phone numbers, postal codes, social security numbers are all examples of this type of data. Although the examples appear to be only numbers, my question is for strings in general.
Given length
the generator below generates random strings:
(gen/fmap #(apply str %)
(gen/vector gen/char-alpha length))
(gen/vector gen/char-alpha length)
generates sequences of characters and the fmap
converts them into strings:
(apply str [\a \b]) ;; => "ab"
If a custom alphabet
(say [\a \b \c]
) is needed gen/char-alpha
can be substituted with something like:
(gen/elements alphabet)
For more complex generators, like formatted phone numbers, test.chuck's string-from-regex
might be a better choice than manually combining official generators.
This function will generate a string of a given length with characters from a given alphabet (optional). If you don't pass any alphabet as an argument, a default will be used, which you can of course change.
(defn generate-string
([length]
(generate-string length
(map char (range 49 127))))
([length alphabet]
(apply str (take length (repeatedly #(rand-nth alphabet))))))
Examples:
(generate-string 7 [\a \b \c])
"bacacbb"
(generate-string 10)
"mxqE<OKH3L"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With