How do I test a higher-order function in Clojure ?
I can always test a function that accepts a value and then check returned value against the expected one.
How do I do that with a higher order function ?
Or we generally avoid doing so ?
This is a good question. To be more precise about the issue:
Higher-order functions "take or produce other functions as arguments or results". So there's two cases:
functions that take functions. Examples: map
, reduce
, filter
. These aren't too hard to test; just supply all the parameters like you would to a normal function.
functions that return functions. Examples: (fn [x] (fn [y] (+ x y)))
, (partial filter #(> % 1))
.
These are difficult to test because we can't directly compare the equality of functions (search for intensional and extensional equality for a thorough discussion).
It should be obvious that simply not testing is not a very good strategy. So why not take the Haskell view that partially applied functions are essentially the same as functions that return functions -- in other words, pass enough parameters to the returned function to get a result that you can test for equality.
Just be careful to watch out for coupling in your tests -- make sure that your test cases are actually testing the specification of the higher-order function, not just the function that it returns.
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