Better Support For C# Records
See original GitHub issueHello.
Currently, the library has support for C# records, at least it claims to have one. But that doesn’t seem to fully work (or I may be missing something 🤦♂️).
I’ve seen the entities in FsCheck/Records.cs at master · fscheck/FsCheck · GitHub and tests that use them FsCheck/Arbitrary.fs at master · fscheck/FsCheck · GitHub
At first glance since the tests pass it seems that random records are being generated. But they are rather “randomly empty” all the time 😢.
Checked versions of the packages:
FsCheck.Xunit
->2.16.4
FsCheck
->2.16.4
Example
F# project that references a C# project with a Person record:
namespace CSharp {
public record Person {
public string FirstName { get; init; }
public string LastName { get; init; }
}
}
and contains a similar F# record too:
type FsPerson = { FirstName: string; LastName: string }
[<EntryPoint>]
let main args =
let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
42
Values in the program are:
The tricky part is that the library fails for “init-only C# records” (as above), but indeed generates the data if at least one property in the C# record is mutable (has a public “set”).
namespace CSharp {
public record Person {
public string FirstName { get; init; }
public string LastName { get; set; }
}
}
type FsPerson = { FirstName: string; LastName: string }
[<EntryPoint>]
let main args =
let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
42
Values in the program are:
So, I wonder whether the library has support for init-only C# records since in my case I:
-
don’t want to make any properties mutable in my domain
-
must stick with C# for my domain because of other developers in the team who aren’t familiar with F#
-
don’t want to use an awkward hack like this one (which won’t work if the base type is sealed):
namespace CSharp {
public record Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
public record ChildForTestOnly: Person {
public int DummyMutableProp { get; set; }
}
}
type FsPerson = { FirstName: string; LastName: string }
[<EntryPoint>]
let main args =
let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
let childPersons = Arb.generate<CSharp.ChildForTestOnly> |> sample 10 10
// ... upcast the "child persons" to "persons" and test them ...
42
Values in the program are:
Any ideas or suggestions?
Issue Analytics
- State:
- Created a year ago
- Comments:19 (14 by maintainers)
Top GitHub Comments
overall size is distributed to properties, so the more properties you have the smaller the size will be for those properties. Same for tuples: https://github.com/fscheck/FsCheck/blob/master/src/FsCheck/ReflectArbitrary.fs#L83
so many properties => smaller size for each of them. Try the above example with a record with one or two properties and at size 10 you’ll see some non-zero values.
Released in https://www.nuget.org/packages/FsCheck/2.16.5