frozenlib / test-strategy

Procedural macro to easily write higher-order strategies in proptest.
Apache License 2.0
44 stars 10 forks source link

Using Arbitrary in test-only #14

Open dcherian opened 2 months ago

dcherian commented 2 months ago

Is there a way to use Arbitrary while only having test-strategy as a dev dependency?

proptest-derive supports this but I couldn't get it to work with test-strategy.

PS: Thanks for this library. I find it a lot more ergonomic that proptest, and love the async support.

frozenlib commented 1 month ago

I've rewritten the example from the link using test-strategy as follows, and it seems to work without any issues. Could you provide an example where the problem occurs?

[dev_dependencies]
proptest = "1.5.0"
test-strategy = "0.4.0"
#[cfg(test)]
mod test {
    use test_strategy::{proptest, Arbitrary};

    #[derive(Arbitrary, Debug)]
    struct MyStruct {
        // ...
    }

    #[proptest]
    fn test_one(my_struct: MyStruct) {
        // ...
    }
}
#[cfg(test)]
use test_strategy::Arbitrary;

#[cfg(test)]
use test_strategy::proptest;

#[derive(Debug)]
#[cfg_attr(test, derive(Arbitrary))]
struct MyStruct {
    #[cfg_attr(test, strategy(proptest::strategy::Just(42)))]
    answer: u32,
}

#[cfg(test)]
#[proptest]
fn test_one(x: MyStruct) {
    // ...
}

If you want to remove proptest = "1.5.0" from Cargo.toml, it's currently not possible.

This is designed to prevent version mismatches between the proptest version used by test-strategy and the one specified in Cargo.toml, by making test-strategy use the proptest version specified in Cargo.toml rather than a specific version.

When I first started developing test-strategy, proptest was in version 0.x, with frequent incompatible version updates, which is why I set it up this way. Now that proptest has reached version 1.0, it might be acceptable to modify it so that compilation is possible without proptest = "1.5.0" in Cargo.toml.

If this change is made, when proptest reaches version 2.0, I'll need to modify test-strategy to use proptest 2.0, even if there are no incompatible changes related to test-strategy.

Also, there's a possibility of compilation errors due to version mismatches if multiple versions of proptest 1.x.y are used during the build. However, since it's unlikely that multiple versions of proptest will be used simultaneously, this probably won't be an issue.