Source Generator for custom classes
For complex objects, building a generator by hand gets a bit complicated.
It would be nice if CsCheck provided an incremental generator that looks for types marked with e.g. [CsCheck], and automatically builds a generator that returns instances of the given type with all the fields filled in.
I'm not such a fan of using reflection to generate Gens. I think you need to have more control and understanding of the generation parameters. This is why I made them as easy to compose as possible. I can see the point of generating an initial version of a complex type for you to then edit possibly.
I'm happy for someone to have a go. I think there may be a lot of complexity to this from looking at how this has been done in FsCheck over time.
I think you need to have more control and understanding of the generation parameter
I think you can have quite a bit of control via attributes on fields, but yeah, if your fields are interdependent, then you might still want to fallback to manual implementation. But for majority of simple types this kind of autogenerated implementation would suffice.
Personally I'm mostly coming from Rust world these days. In Rust, most things can be easily implemented via #[derive(...)] - which is similar to Roslyn codegen - and that applies to property testing as well.
For example, test-strategy crate implements such derives for one of the popular property testing frameworks (first example from docs, feel free to check out more complex examples there as well):
use test_strategy::Arbitrary;
#[derive(Arbitrary, Debug)]
struct TestInputStruct {
x: u32,
#[strategy(1..10u32)]
y: u32,
#[strategy(0..#y)]
z: u32,
}
#[derive(Arbitrary, Debug)]
enum TestInputEnum {
A,
B,
#[weight(3)]
C,
X(u32),
Y(#[strategy(0..10u32)] u32),
}
I used it across a lot of Rust codebases, and yeah, sometimes I still had to drop back to manual implementation, but it still saved a lot of boilerplate for majority of data structures.
I was kind of hoping to find similar property testing framework with ready "derives" in C# as well, but I only found a few manual property testing libraries instead, and not a single source generator.
Even among those found testing libraries, some - like FsCheck - crash in very surprising ways on some combinations (internal bugs that, as far as I could tell, are caused by conversions between F# types and C# ones, and not even by my own code), and CsCheck is the best C# solution I could find so far.
I love the flexibility it provides, and so far it worked without issues, just a bit cumbersome for complex data structures. That's why I figured I might raise a feature request here to see if you'd be interested in going one step further, and thought I'd provide Rust example of similar annotations for inspiration :)
I'm not such a fan of using reflection to generate Gens.
Oh, also just to be clear, I was talking specifically about Roslyn source generators (entirely compile-time, and you can see the generated C# output and even copy-paste for manual modification if you wish), definitely not reflection in the traditional runtime sense.