Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmaturtle committed Jun 30, 2023
1 parent 1567e36 commit 1bec866
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ public async Task Async_Task_property(
int i)
{
await FooAsync();
#pragma warning disable CS1718
Assert.True(i == i);
#pragma warning restore CS1718
}

[Property]
public Task Task_property(
int i)
{
#pragma warning disable CS1718
Assert.True(i == i);
#pragma warning restore CS1718
return Task.CompletedTask;
}

Expand Down
76 changes: 76 additions & 0 deletions examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Hedgehog.Xunit.Examples.CSharp;

using Hedgehog;
using Hedgehog.Linq;
using Hedgehog.Xunit;
using Gen = Linq.Gen;
using Range = Linq.Range;

public class PositiveAndNegativeWithAutoGenConfigContainer
{
public record PositiveInt(int Value);
public record NegativeInt(int Value);

public class Generators
{
public static Gen<PositiveInt> GenPositiveInt =>
from i in Gen.Int32(Range.Constant(1, int.MaxValue))
select new PositiveInt(i);

public static Gen<NegativeInt> GenNegativeInt =>
from i in Gen.Int32(Range.Constant(int.MinValue, -1))
select new NegativeInt(i);

public static AutoGenConfig _ => GenX.defaults
.WithGenerator(GenPositiveInt)
.WithGenerator(GenNegativeInt);
}

[Property(typeof(Generators))]
public bool ResultOfAddingPositiveAndNegativeLessThanPositive(
PositiveInt positive,
NegativeInt negative) =>
positive.Value + negative.Value < positive.Value;
}

public class PositiveAndNegativeWithGenAttribute
{
public class Negative : GenAttribute<int>
{
public override Gen<int> Generator => Gen.Int32(Range.Constant(int.MinValue, -1));
}

public class Positive : GenAttribute<int>
{
public override Gen<int> Generator => Gen.Int32(Range.Constant(1, int.MaxValue));
}

[Property]
public bool ResultOfAddingPositiveAndNegativeLessThanPositive(
[Positive] int positive,
[Negative] int negative) =>
positive + negative < positive;
}

public class PositiveAndNegativeWithParameterizedGenAttribute
{
public class Int32Range : GenAttribute<int>
{
private readonly int _min;
private readonly int _max;

public Int32Range(int min, int max)
{
_min = min;
_max = max;
}

public override Gen<int> Generator => Gen.Int32(Range.Constant(_min, _max));
}

[Property]
public bool ResultOfAddingPositiveAndNegativeLessThanPositive(
[Int32Range(1, int.MaxValue)] int positive,
[Int32Range(int.MinValue, -1)] int negative) =>
positive + negative < positive;
}

This file was deleted.

This file was deleted.

This file was deleted.

72 changes: 34 additions & 38 deletions examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
module GenAttributeVsContainer

open System
open Hedgehog
open Hedgehog.Xunit

// Properties containing multiple parameter of the same type with different
// generator requirements.
let positiveInt() = Range.constant 1 Int32.MaxValue |> Gen.int32
let negativeInt() = Range.constant Int32.MinValue 1 |> Gen.int32

let positiveInt() = Range.constant 0 System.Int32.MaxValue |> Gen.int32
let negativeInt() = Range.constant System.Int32.MinValue 0 |> Gen.int32
module ``With AutoGenConfig Container`` =
type PositiveInt = { Value: int }
type NegativeInt = { Value: int }

// Using property attribute we need to create container types so that
// the parameters of the property can be of different types.
type PositiveInt = {value : int}
type NegativeInt = {value : int}
type AutoGenConfigContainer =
static member __ =
GenX.defaults
|> AutoGenConfig.addGenerator (positiveInt() |> Gen.map(fun x -> { PositiveInt.Value = x }))
|> AutoGenConfig.addGenerator (negativeInt() |> Gen.map(fun x -> { NegativeInt.Value = x }))

type AutoGenConfigContainer =
static member __ =
GenX.defaults
|> AutoGenConfig.addGenerator (positiveInt() |> Gen.map(fun x -> {PositiveInt.value=x}))
|> AutoGenConfig.addGenerator (negativeInt() |> Gen.map(fun x -> {NegativeInt.value=x}))
[<Property(typeof<AutoGenConfigContainer>)>]
let ``Positive + Negative < Positive`` (positive: PositiveInt) (negative: NegativeInt) =
positive.Value + negative.Value < positive.Value

[<Property(typeof<AutoGenConfigContainer>)>]
let ``Positive + Negative <= Positive`` (positive:PositiveInt) (negative:NegativeInt) =
positive.value + negative.value <= positive.value
module ``With GenAttribute`` =
type PosInt() =
inherit GenAttribute<int>()
override _.Generator = positiveInt()

// Using attributes to configure what generator the property should use
type Posint() =
inherit GenAttribute<int>()
override _.Generator = positiveInt()

type NegInt() =
inherit GenAttribute<int>()
type NegInt() =
inherit GenAttribute<int>()
override _.Generator = negativeInt()

[<Property>]
let ``Positive + Negative <= Positive attribute`` ([<Posint>] positive) ([<NegInt>] negative) =
positive + negative <= positive

// Using a parameterised attribute to configure the generators
// Using attributes to configure what generator the property should use
type IntRange(minimum:int32, maximum:int32) =
inherit GenAttribute<int>()
override _.Generator = Range.constant minimum maximum |> Gen.int32

[<Property>]
let ``Positive + Negative <= Positive attribute parameterised``
([<IntRange(0, System.Int32.MaxValue)>] positive)
([<IntRange(System.Int32.MinValue, 0)>] negative) =
positive + negative <= positive
[<Property>]
let ``Positive + Negative < Positive`` ([<PosInt>] positive) ([<NegInt>] negative) =
positive + negative < positive

module ``With Parameterized GenAttribute`` =
type IntRange(min: int32, max: int32) =
inherit GenAttribute<int>()
override _.Generator = Range.constant min max |> Gen.int32

[<Property>]
let ``Positive + Negative < Positive, parameterized``
([<IntRange(0, Int32.MaxValue)>] positive)
([<IntRange(Int32.MinValue, 0)>] negative) =
positive + negative < positive
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Hedgehog.Xunit/InternalLogic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ let withShrinks = function
| Some x -> PropertyConfig.withShrinks x
| None -> PropertyConfig.withoutShrinks


let report (testMethod:MethodInfo) testClass testClassInstance =
let getAttributeGenerator (parameterInfo: ParameterInfo) =
let attributes = parameterInfo.GetCustomAttributes()
Expand Down
2 changes: 2 additions & 0 deletions tests/Hedgehog.Xunit.Tests.CSharp/Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public async Task Async_property_which_returns_task_can_run(
int i)
{
await FooAsync();
#pragma warning disable CS1718
Assert.True(i == i);
#pragma warning restore CS1718
}

[Property]
Expand Down
32 changes: 14 additions & 18 deletions tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Int6() =
inherit GenAttribute<int>()
override _.Generator = Gen.constant 6

type IntCRange(max:int, min:int)=
type IntConstantRange(max: int, min: int)=
inherit GenAttribute<int>()
override _.Generator = Range.constant max min |> Gen.int32

Expand Down Expand Up @@ -393,33 +393,28 @@ module ``Asynchronous tests`` =
[<Property(Skip = skipReason)>]
let ``TaskResult with Error shrinks, skipped`` (i: int) =
task {
do! Task.Delay 100
do! FooAsync()
if i > 10 then
return Error ()
else
return Ok ()
}

[<Fact>]
let ``TaskResult with Error shrinks`` () =
assertShrunk (nameof ``TaskResult with Error shrinks, skipped``) "[11]"

[<Property(Skip = skipReason)>]
let ``Non Unit TaskResult with Error shrinks, skipped`` (i: int) =
let ``Non-unit TaskResult with Error shrinks, skipped`` (i: int) =
task {
do! Task.Delay 100
do! FooAsync()
if i > 10 then
return Error "Test fails"
else
return Ok 1
}

[<Fact>]
let ``Non Unit TaskResult with Error shrinks`` () =
assertShrunk (nameof ``Non Unit TaskResult with Error shrinks, skipped``) "[11]"



let ``Non-unit TaskResult with Error shrinks`` () =
assertShrunk (nameof ``Non-unit TaskResult with Error shrinks, skipped``) "[11]"

module ``IDisposable test module`` =
let mutable runs = 0
Expand Down Expand Up @@ -752,7 +747,7 @@ module ``returning a property runs it`` =
let actual = Assert.Throws<Exception>(fun () -> InternalLogic.tryRaise report)
actual.Message.Contains("51") |> Assert.True

module ``Attribute Parameter Type Tests`` =
module ``GenAttribute Tests`` =

[<Property>]
let ``can define parameter as 5`` ([<Int5>] i) =
Expand All @@ -763,19 +758,20 @@ module ``Attribute Parameter Type Tests`` =
Assert.StrictEqual(5, i)

[<Property>]
let ``can have different generators for the same type of parameter`` ([<Int5>] five) ([<Int6>] six) =
let ``can have different generators for the same parameter type`` ([<Int5>] five) ([<Int6>] six) =
five = 5 && six = 6

[<Property>]
let ``can restrict on range`` ([<IntCRange(min = 0, max = 5)>] i) =
let ``can restrict on range`` ([<IntConstantRange(min = 0, max = 5)>] i) =
i >= 0 && i <= 5

[<Properties(typeof<Int13>, 200<tests>)>]
module ``Attribute Parameter Type Tests with Properties`` =
[<Property(typeof<Int13>)>]
[<Properties(typeof<Int13>)>]
module ``GenAttribute with Properties Tests`` =

[<Property>]
let ``overrides Properties' autoGenConfig`` ([<Int5>] i) =
Assert.StrictEqual(5, i)

[<Property(typeof<Int13>)>]
let ``overrides Property's autoGenConfig`` ([<Int5>] i) =
let ``overrides Properties' and Property's autoGenConfig`` ([<Int5>] i) =
Assert.StrictEqual(5, i)

0 comments on commit 1bec866

Please sign in to comment.