Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic generators #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AlexeyRaga
Copy link
Contributor

@AlexeyRaga AlexeyRaga commented Dec 23, 2024

This PR adds the ability to register custom generators for generic type.

It is the main reason FSCheck is used alongside with Hedgehog as a "fallback" solution in my company: we use a lot of generic data structures, mostly written in C#.
Having the ability to register generators for custom generic types simplified writing tests a lot.

To register a generator for a generic type we can now:

FSharp:

// Create a type in which generators are defined:
type GenericTestGenerators =

    // Create a static member that returns a generator for a generic structure
    // It can take a number of Gen<_> parameters if it needs to
    static member MyEitherGen<'a, 'b>(genLeft: Gen<'a>, genRight: Gen<'b>) : Gen<Either<'a, 'b>> = ???

    static member MyMaybeGen<'a>(genA: Gen<'a>): Gen<Maybe<'a>> = ???

CSharp:

public sealed class GenericTestGenerators
{
    public static Gen<Either<TLeft, TRight>> MyEitherGen<TLeft, TRight>(Gen<TLeft> genA, Gen<TRight> genB) => ???;
    public static Gen<Maybe<T>> MyMaybeGen<T>(Gen<T> gen) => ???;
}

And then these generators can be registered using addGenerators<'a> function:

FSharp:

let config = GenX.defaults |> AutoGenConfig.addGenerators<GenericTestGenerators>

CSharp:

var config = GenX.defaults.WithGenerators<GenericTestGenerators>();

Ref: #77

@AlexeyRaga AlexeyRaga marked this pull request as ready for review December 23, 2024 01:29
with:
dotnet-version: 6.0.201
dotnet-version: 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the .NET version?

Copy link
Contributor Author

@AlexeyRaga AlexeyRaga Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TysonMN
Because .NET 6 reached Out Of Support last year (in November, if I remember correctly).
.NET 8 is the next LTS which is supported until the end of 2026.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it wasn't needed to add this feature then?

If not, then please make a separate PR with a single commit that upgrades .NET. In the commit message body, put the explanation that you have given here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check, but I believe that it won't compile under .NET 6.
I can make a PR so we can upgrade to .NET 8 first, and then rebase this one on top.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, .NET 8 is needed for the feature the way it is now.
I just decided not to take any extra effort to add .NET 6 workarounds and simply use the officially supported version.

This way we can say that it is needed to add the feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make a PR so we can upgrade to .NET 8 first, and then rebase this one on top.

Yes. Two PRs in that order would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TysonMN
OK, let's merge #79 and then I'll rebase this one on top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TysonMN As discussed, I have rebased this branch on top of .NET 8 enabled master.
Please check again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants