Add nested property support
Description
Environment
Activity
Reid Draper May 26, 2014 at 5:47 PM
Ok, let's translate that to using gen/bind
. Just like with nested properties, bind
allows you to create generators that depend on the value of another generator. For example, your gen-random-path
depends on the previously generated matrix
variable. So let's write a generator that returns both a matrix
and a random-path
.
(def matrix-and-path
"Return a two-tuple of [matrix random-path]"
(gen/bind (gen-matrix 6)
(fn [matrix]
(gen/tuple (gen/return matrix) (gen-random-path matrix)))))
(prop/for-all [[matrix random-path] matrix-and-path]
(<= (cost (tsp matrix)) (cost random-path))
import May 23, 2014 at 12:22 PM
Comment made by: maciej.jaskowski
ping!
import May 18, 2014 at 10:56 PM
Comment made by: maciej.jaskowski
Sure!
I would like to write something like that:
(prop/for-all [matrix (gen-matrix 6)]
(prop/for-all [random-path (gen-random-path matrix)]
(<= (cost (tsp matrix)) (cost random-path))))
Reid Draper May 17, 2014 at 6:44 PM
I'm still not sure what this would look like using a nested-property. Can you please write the code the way you'd like to see it as a nested property? This will involve at least two for-alls.
import May 17, 2014 at 9:26 AM
Comment made by: maciej.jaskowski
Ok but now I can only write something like this:
(prop/for-all [data (gen-matrix-and-paths 6)]
(let [ [matrix random-paths] data ]
(not-worse-then-others? tsp matrix random-paths))))
which, if my 'tsp' algorithm is not working correctly would point me to a matrix and a vector (length 500) of paths one of which is counterexample. It would be better if instead I got the matrix and a single counterexample.
Including functionality to add support for nested properties would be beneficial. An example of how this may work follows:
(defn exists? [e coll] (some #{e} coll)) (defn remove-elem [e coll] (remove #{e} coll)) ;; this test should fail, and illustrates nested properties (quick-check (for-all [v (gen/vector gen/int)] (for-all [e (elements v)] #(exists? e (remove-elem e v)))))
The requirement for this is support for reifying properties, which is not currently implemented.
Original issue - GitHub #10