-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1567e36
commit 1bec866
Showing
10 changed files
with
131 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
35 changes: 0 additions & 35 deletions
35
examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/Container.cs
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/GenAttribute.cs
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/ParameterizedGenAttribute.cs
This file was deleted.
Oops, something went wrong.
72 changes: 34 additions & 38 deletions
72
examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 1 addition & 2 deletions
3
examples/Hedgehog.Xunit.Examples.FSharp/Hedgehog.Xunit.Examples.FSharp.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters