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

Update FsCheck to 3.0 #812

Merged
merged 2 commits into from
Jan 27, 2025
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
<PackageVersion Include="FsCheck.Xunit" Version="2.16.4" />
<PackageVersion Include="FsCheck.Xunit" Version="3.0.1" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
<PackageVersion Include="Verify.XUnit" Version="16.8.1" />
<PackageVersion Include="Verify.SourceGenerators" Version="1.4.0" />
Expand Down
29 changes: 16 additions & 13 deletions Funcky.Async.Test/AsyncGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using FsCheck;
using FsCheck.Fluent;

namespace Funcky.Async.Test;

public class AsyncGenerator<T>
internal static class AsyncGenerator
{
public static Arbitrary<IAsyncEnumerable<T>> GenerateAsyncEnumerable()
=> Arb.Default.List<T>().Generator.Select(list => list.ToAsyncEnumerable()).ToArbitrary();
public static Arbitrary<IAsyncEnumerable<T>> GenerateAsyncEnumerable<T>(IArbMap map)
=> map.GeneratorFor<List<T>>().Select(list => list.ToAsyncEnumerable()).ToArbitrary();

public static Arbitrary<Func<T, ValueTask<T>>> GenerateAwaitSelector()
=> Arb.Default.SystemFunc1<T, T>().Generator.Select(ResultToValueTask).ToArbitrary();
public static Arbitrary<AwaitSelector<T>> GenerateAwaitSelector<T>(IArbMap map)
=> map.GeneratorFor<Func<T, T>>().Select(ResultToValueTask).ToArbitrary();

public static Arbitrary<Func<T, CancellationToken, ValueTask<T>>> GenerateAwaitWithCancellationSelector()
=> Arb.Default.SystemFunc1<T, T>().Generator.Select(ResultToValueTaskX).ToArbitrary();
public static Arbitrary<AwaitSelectorWithCancellation<T>> GenerateAwaitWithCancellationSelector<T>(IArbMap map)
=> map.GeneratorFor<Func<T, T>>().Select(ResultToValueTaskX).ToArbitrary();

private static Func<T, ValueTask<T>> ResultToValueTask(Func<T, T> f)
=> value
=> ValueTask.FromResult(f(value));
private static AwaitSelector<T> ResultToValueTask<T>(Func<T, T> f)
=> new(value => ValueTask.FromResult(f(value)));

private static Func<T, CancellationToken, ValueTask<T>> ResultToValueTaskX(Func<T, T> f)
=> (value, cancellationToken)
=> ValueTask.FromResult(f(value));
private static AwaitSelectorWithCancellation<T> ResultToValueTaskX<T>(Func<T, T> f)
=> new((value, _) => ValueTask.FromResult(f(value)));
}

public sealed record AwaitSelector<T>(Func<T, ValueTask<T>> Get);

public sealed record AwaitSelectorWithCancellation<T>(Func<T, CancellationToken, ValueTask<T>> Get);
1 change: 1 addition & 0 deletions Funcky.Async.Test/AsyncSequence/ConcatTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Funcky.Async.Test.TestUtilities;

Expand Down
1 change: 1 addition & 0 deletions Funcky.Async.Test/AsyncSequence/CycleRangeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Funcky.Async.Test.TestUtilities;
using Funcky.Test.TestUtils;
Expand Down
1 change: 1 addition & 0 deletions Funcky.Async.Test/AsyncSequence/CycleTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Funcky.Test.TestUtils;

Expand Down
1 change: 1 addition & 0 deletions Funcky.Async.Test/AsyncSequence/RepeatRangeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Funcky.Async.Test.TestUtilities;
using Funcky.Test.TestUtils;
Expand Down
1 change: 1 addition & 0 deletions Funcky.Async.Test/AsyncSequence/ReturnTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Funcky.Async.Test.TestUtilities;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,150 +1,136 @@
// ReSharper disable PossibleMultipleEnumeration
using FsCheck;
using FsCheck.Xunit;
using FsCheck.Fluent;
using Funcky.Test.Internal;

namespace Funcky.Async.Test.Extensions.AsyncEnumerableExtensions;

public sealed class AverageOrNoneTest
{
public AverageOrNoneTest()
{
Arb.Register<AsyncGenerator<int?>>();
Arb.Register<AsyncGenerator<int>>();
Arb.Register<AsyncGenerator<long?>>();
Arb.Register<AsyncGenerator<long>>();
Arb.Register<AsyncGenerator<float?>>();
Arb.Register<AsyncGenerator<float>>();
Arb.Register<AsyncGenerator<double?>>();
Arb.Register<AsyncGenerator<double>>();
Arb.Register<AsyncGenerator<decimal?>>();
Arb.Register<AsyncGenerator<decimal>>();
}

// Int32/int Tests
[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageAsyncForInt32(IAsyncEnumerable<int> sequence)
=> CompareAverageAndHandleEmptyInt32SequenceAsync(sequence).Result.ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence)
=> (Option.FromNullable(sequence.AverageAsync().Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync().Result).ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence, Func<int?, int?> selector)
=> (Option.FromNullable(sequence.AverageAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();

[Property]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence, Func<int?, ValueTask<int?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence, AwaitSelector<int?> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

[Property]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence, Func<int?, CancellationToken, ValueTask<int?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt32(IAsyncEnumerable<int?> sequence, AwaitSelectorWithCancellation<int?> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

// Int64/long Tests
[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageAsyncForInt64(IAsyncEnumerable<long> sequence)
=> CompareAverageAndHandleEmptyInt64SequenceAsync(sequence).Result.ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<long?> sequence)
=> (Option.FromNullable(sequence.AverageAsync().Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync().Result).ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<long?> sequence, Func<long?, long?> selector)
=> (Option.FromNullable(sequence.AverageAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();

[Property]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<long?> sequence, Func<long?, ValueTask<long?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<long?> sequence, AwaitSelector<long?> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

[Property]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<int?> sequence, Func<int?, CancellationToken, ValueTask<int?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForInt64(IAsyncEnumerable<int?> sequence, AwaitSelectorWithCancellation<int?> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

// Single/float Tests
[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageAsyncForSingle(IAsyncEnumerable<float> sequence)
=> CompareAverageAndHandleEmptySingleSequenceAsync(sequence).Result.ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence)
=> (Option.FromNullable(sequence.AverageAsync().Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync().Result).ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence, Func<float?, float?> selector)
=> (Option.FromNullable(sequence.AverageAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();

[Property]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence, Func<float?, ValueTask<float?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence, AwaitSelector<float?> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

[Property]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence, Func<float?, CancellationToken, ValueTask<float?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForSingle(IAsyncEnumerable<float?> sequence, AwaitSelectorWithCancellation<float?> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

// Double/double Tests
[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageAsyncForDouble(IAsyncEnumerable<double> sequence)
=> CompareAverageAndHandleEmptyDoubleSequenceAsync(sequence).Result.ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence)
=> (Option.FromNullable(sequence.AverageAsync().Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync().Result).ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence, Func<double?, double?> selector)
=> (Option.FromNullable(sequence.AverageAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();

[Property]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence, Func<double?, ValueTask<double?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence, AwaitSelector<double?> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

[Property]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence, Func<double?, CancellationToken, ValueTask<double?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDouble(IAsyncEnumerable<double?> sequence, AwaitSelectorWithCancellation<double?> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

// Decimal/decimal Tests
[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageAsyncForDecimal(IAsyncEnumerable<decimal> sequence)
=> CompareAverageAndHandleEmptyDecimalSequenceAsync(sequence).Result.ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence)
=> (Option.FromNullable(sequence.AverageAsync().Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync().Result).ToProperty();

[Property]
[FunckyAsyncProperty]
public Property AverageOrNoneAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence, Func<decimal?, decimal?> selector)
=> (Option.FromNullable(sequence.AverageAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();

[Property]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence, Func<decimal?, ValueTask<decimal?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageOrNoneAwaitAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence, AwaitSelector<decimal?> selector)
=> (Option.FromNullable(sequence.AverageAwaitAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

[Property]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence, Func<decimal?, CancellationToken, ValueTask<decimal?>> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector)).Result).ToProperty();
[FunckyAsyncProperty]
public Property AverageAwaitWithCancellationAsyncWithSelectorGivesTheSameResultAsAverageForNullableAsyncForDecimal(IAsyncEnumerable<decimal?> sequence, AwaitSelectorWithCancellation<decimal?> selector)
=> (Option.FromNullable(sequence.AverageAwaitWithCancellationAsync(selector.Get).Result)
== sequence.Select(Option.FromNullable).AverageOrNoneAwaitWithCancellationAsync(SelectorTransformation.TransformNullableSelector(selector.Get)).Result).ToProperty();

private static async Task<bool> CompareAverageAndHandleEmptyInt32SequenceAsync(IAsyncEnumerable<int> sequence)
=> await sequence.AnyAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FsCheck;
using FsCheck.Xunit;
using FsCheck.Fluent;
using Funcky.FsCheck;
using static Funcky.Async.Test.Extensions.AsyncEnumerableExtensions.TestData;

Expand Down Expand Up @@ -33,9 +33,7 @@ public async Task ElementAtOrNoneReturnsSomeWithinTheRangeAndNoneOutside()

public sealed class IndexIndex
{
public IndexIndex() => FunckyGenerators.Register();

[Property(Verbose = true)]
[FunckyProperty(Verbose = true)]
public Property BehavesIdenticalToSynchronousCounterpart(List<int> source, Index index)
=> (source.ElementAtOrNone(index) == source.ToAsyncEnumerable().ElementAtOrNoneAsync(index).Result)
.ToProperty();
Expand Down
Loading
Loading