Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions TUnit.Example.FsCheck.TestProject/PropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

namespace TUnit.Example.FsCheck.TestProject;

/// <summary>
/// Custom arbitrary that generates positive integers only.
/// </summary>
public class PositiveIntArbitrary
{
public static Arbitrary<int> PositiveInt()
{
return ArbMap.Default.GeneratorFor<int>()
.Where(x => x > 0)
.ToArbitrary();
}
}

public class PropertyTests
{
[Test, FsCheckProperty]
Expand Down Expand Up @@ -88,4 +101,9 @@ public Property StringReversalProperty()
});
}

[Test, FsCheckProperty(Arbitrary = new[] { typeof(PositiveIntArbitrary) })]
public bool PositiveNumbersArePositive(int value)
{
return value > 0;
}
}
49 changes: 47 additions & 2 deletions docs/docs/examples/fscheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,21 @@ public bool MyProperty(int value)

## Custom Generators

You can provide custom `Arbitrary` implementations for generating test data:
You can provide custom `Arbitrary` implementations for generating test data. FsCheck 3.x uses `ArbMap.Default` to access default arbitraries:

```csharp
using FsCheck;
using FsCheck.Fluent;

public class PositiveIntArbitrary
{
public static Arbitrary<int> PositiveInt()
{
return Arb.Default.Int32().Filter(x => x > 0);
// Use ArbMap.Default to get a generator for a type,
// then filter with Where() and convert to Arbitrary
return ArbMap.Default.GeneratorFor<int>()
.Where(x => x > 0)
.ToArbitrary();
}
}

Expand All @@ -140,6 +147,44 @@ public bool PositiveNumbersArePositive(int value)
}
```

### Alternative: Using Gen.Choose

For simple ranges, use `Gen.Choose` directly:

```csharp
public class PositiveIntArbitrary
{
public static Arbitrary<int> PositiveInt()
{
// Generate integers in a specific range
return Gen.Choose(1, int.MaxValue).ToArbitrary();
}
}
```

### Custom types

For custom types, compose generators using LINQ:

```csharp
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class PersonArbitrary
{
public static Arbitrary<Person> Person()
{
var gen = from name in ArbMap.Default.GeneratorFor<string>()
from age in Gen.Choose(0, 120)
select new Person { Name = name, Age = age };
return gen.ToArbitrary();
}
}
```

## Limitations

- **Native AOT**: TUnit.FsCheck is not compatible with Native AOT publishing because FsCheck requires reflection and dynamic code generation
Expand Down
Loading