Fork me on GitHub
#testing
<
2019-05-07
>
dharrigan20:05:53

Is there a standard approach for testing a function that takes a single parameter (and evals to true/false), but have a test that you feed it a list (or vector) of values to test? I have something like this:

dharrigan20:05:30

(t/deftest test-palindromes
  (for [x palindromes]
     (t/is (= true (p/palindrome? x)))))

dharrigan20:05:38

Feels a bit clunky. Ideally, i would like an indvidual test for each element in the vector (in this case, palindromes), but there could be a lot!

seancorfield20:05:10

It feels like a job for generative testing...

dharrigan20:05:26

can generative testing generate palindromes?

dharrigan20:05:59

(I have about 20 words and a few sentences that are palindromes)

seancorfield20:05:32

Generate random strings and append them, reversed, to themselves, and you have randomly generated palindromes. That will test cases involving empty strings and weird characters. You can also append them, reversed with the first character removed, to get odd-length palindromes.

seancorfield20:05:14

The key property to test there is if you give your function random strings that are known to be palindromic, it will return true for all of them.

dharrigan20:05:52

Thanks for the insight 🙂

dharrigan20:05:43

I'll have to go away now and look at how to do generative testing (which I saw briefly on my skim so far)

seancorfield20:05:07

You could test for the false case by generating random strings and then filtering out those that start and end with the same character (strings need to be at least length 2).

dharrigan20:05:28

great! thank you kindly! 🙂