Can QuickCheck use closures as properties?


#1

QuickCheck is looking more and more sponditious as I play with it. However, I really need to use closures rather than functions as properties for the testing I need to do. I guess an alternative would be to have partially evaluated functions but I haven’t found this capability in Rust (I may just have missed it). Currently the quickcheck call needs a type cast because of various things in the compiler currently and there appears no sensible way of casting a closure to a function. fn(usize)->bool fails due a “non-scalar cast” and I haven’t found a use of Fn that doesn’t cause an error about “unsized type”.


#2

It’s not possible. Quickcheck has the Testable trait for functions (and other types) that can be tested.

A closure cannot be cast to a function pointer type.

It’s not much typing to cast the function to a function pointer though — you can use _ in every argument and return value position when you cast. My test cases typically look like this:

#[test]
fn graphmap_remove() {
    fn prop(mut g: GraphMap<i8, ()>, a: i8, b: i8) -> bool {
        /* property test here */
    }
    quickcheck::quickcheck(prop as fn(_, _, _) -> _);
}

Sure, the inner prop function and the cast are boilerplate, but it’s not too bad.

Quickcheck supporting closures directly would be the best. It doesn’t look that easy to solve with the traits involved, and multiple possible closure signatures.