From 0b1197cb9ff58a305e669a6fb50fc71b028c826f Mon Sep 17 00:00:00 2001 From: AN Date: Fri, 30 Jun 2023 15:55:24 -0500 Subject: [PATCH] polish --- .../DocumentationSamples.cs | 4 +- .../GenAttributeVsContainer.cs | 76 +++++++++++++++++++ .../GenAttributeVsContainer/Container.cs | 35 --------- .../GenAttributeVsContainer/GenAttribute.cs | 25 ------ .../ParameterizedGenAttribute.cs | 29 ------- .../GenAttributeVsContainer.fs | 72 +++++++++--------- .../Hedgehog.Xunit.Examples.FSharp.fsproj | 3 +- src/Hedgehog.Xunit/InternalLogic.fs | 1 - tests/Hedgehog.Xunit.Tests.CSharp/Async.cs | 2 +- .../PropertyTests.fs | 32 ++++---- 10 files changed, 128 insertions(+), 151 deletions(-) create mode 100644 examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs delete mode 100644 examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/Container.cs delete mode 100644 examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/GenAttribute.cs delete mode 100644 examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/ParameterizedGenAttribute.cs diff --git a/examples/Hedgehog.Xunit.Examples.CSharp/DocumentationSamples.cs b/examples/Hedgehog.Xunit.Examples.CSharp/DocumentationSamples.cs index 3e580ad..278d104 100644 --- a/examples/Hedgehog.Xunit.Examples.CSharp/DocumentationSamples.cs +++ b/examples/Hedgehog.Xunit.Examples.CSharp/DocumentationSamples.cs @@ -97,14 +97,14 @@ public async Task Async_Task_property( int i) { await FooAsync(); - Assert.True(i == i); + Assert.StrictEqual(i, i); } [Property] public Task Task_property( int i) { - Assert.True(i == i); + Assert.StrictEqual(i, i); return Task.CompletedTask; } diff --git a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs b/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs new file mode 100644 index 0000000..3053192 --- /dev/null +++ b/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer.cs @@ -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 GenPositiveInt => + from i in Gen.Int32(Range.Constant(1, int.MaxValue)) + select new PositiveInt(i); + + public static Gen 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 + { + public override Gen Generator => Gen.Int32(Range.Constant(int.MinValue, -1)); + } + + public class Positive : GenAttribute + { + public override Gen 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 + { + private readonly int _min; + private readonly int _max; + + public Int32Range(int min, int max) + { + _min = min; + _max = max; + } + + public override Gen 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; +} diff --git a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/Container.cs b/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/Container.cs deleted file mode 100644 index 58442a6..0000000 --- a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/Container.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace Hedgehog.Xunit.Examples.CSharp.GenAttributeVsContainer; - -using Hedgehog; -using Hedgehog.Linq; -using Hedgehog.Xunit; -using Gen = Linq.Gen; -using Range = Linq.Range; - -public record PositiveInt(int Value); -public record NegativeInt(int Value); - -public class Generators -{ - public static Gen GenPositiveInt => - from i in Gen.Int32(Range.Constant(1, int.MaxValue)) - select new PositiveInt(i); - - public static Gen GenNegativeInt => - from i in Gen.Int32(Range.Constant(int.MinValue, -1)) - select new NegativeInt(i); - - public static AutoGenConfig _ => GenX.defaults - .WithGenerator(GenPositiveInt) - .WithGenerator(GenNegativeInt); -} - -public class Container -{ - [Property(typeof(Generators))] - public bool ResultOfAddingPositiveAndNegativeLessThanPositive( - PositiveInt positive, - NegativeInt negative) => - positive.Value + negative.Value < positive.Value; - -} diff --git a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/GenAttribute.cs b/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/GenAttribute.cs deleted file mode 100644 index 89506f7..0000000 --- a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/GenAttribute.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Hedgehog.Xunit.Examples.CSharp.GenAttributeVsContainer; - -using Hedgehog; -using Hedgehog.Xunit; -using Gen = Linq.Gen; -using Range = Linq.Range; - -public class Negative : GenAttribute -{ - public override Gen Generator => Gen.Int32(Range.Constant(int.MinValue, -1)); -} - -public class Positive : GenAttribute -{ - public override Gen Generator => Gen.Int32(Range.Constant(1, int.MaxValue)); -} - -public class PositiveAndNegativeUtilizingIntegerRangeAttribute -{ - [Property] - public bool ResultOfAddingPositiveAndNegativeLessThanPositive( - [Positive] int positive, - [Negative] int negative) => - positive + negative < positive; -} diff --git a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/ParameterizedGenAttribute.cs b/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/ParameterizedGenAttribute.cs deleted file mode 100644 index b3f142d..0000000 --- a/examples/Hedgehog.Xunit.Examples.CSharp/GenAttributeVsContainer/ParameterizedGenAttribute.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Hedgehog.Xunit.Examples.CSharp.GenAttributeVsContainer; - -using Hedgehog; -using Hedgehog.Xunit; -using Gen = Linq.Gen; -using Range = Linq.Range; - -public class Int32Range : GenAttribute -{ - private readonly int _min; - private readonly int _max; - - public Int32Range(int min, int max) - { - _min = min; - _max = max; - } - - public override Gen Generator => Gen.Int32(Range.Constant(_min, _max)); -} - -public class PositiveAndNegativeWithAttributes -{ - [Property] - public bool ResultOfAddingPositiveAndNegativeLessThanPositive( - [Int32Range(1, int.MaxValue)] int positive, - [Int32Range(int.MinValue, -1)] int negative) => - positive + negative < positive; -} diff --git a/examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs b/examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs index f98064e..5f708ca 100644 --- a/examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs +++ b/examples/Hedgehog.Xunit.Examples.FSharp/GenAttributeVsContainer.fs @@ -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})) + [)>] + let ``Positive + Negative < Positive`` (positive: PositiveInt) (negative: NegativeInt) = + positive.Value + negative.Value < positive.Value -[)>] -let ``Positive + Negative <= Positive`` (positive:PositiveInt) (negative:NegativeInt) = - positive.value + negative.value <= positive.value +module ``With GenAttribute`` = + type PosInt() = + inherit GenAttribute() + override _.Generator = positiveInt() -// Using attributes to configure what generator the property should use -type Posint() = - inherit GenAttribute() - override _.Generator = positiveInt() - -type NegInt() = - inherit GenAttribute() + type NegInt() = + inherit GenAttribute() override _.Generator = negativeInt() -[] -let ``Positive + Negative <= Positive attribute`` ([] positive) ([] 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() - override _.Generator = Range.constant minimum maximum |> Gen.int32 - -[] -let ``Positive + Negative <= Positive attribute parameterised`` - ([] positive) - ([] negative) = - positive + negative <= positive + [] + let ``Positive + Negative < Positive`` ([] positive) ([] negative) = + positive + negative < positive + +module ``With Parameterized GenAttribute`` = + type IntRange(min: int32, max: int32) = + inherit GenAttribute() + override _.Generator = Range.constant min max |> Gen.int32 + + [] + let ``Positive + Negative < Positive, parameterized`` + ([] positive) + ([] negative) = + positive + negative < positive diff --git a/examples/Hedgehog.Xunit.Examples.FSharp/Hedgehog.Xunit.Examples.FSharp.fsproj b/examples/Hedgehog.Xunit.Examples.FSharp/Hedgehog.Xunit.Examples.FSharp.fsproj index 2dedd9c..1bbf1fe 100644 --- a/examples/Hedgehog.Xunit.Examples.FSharp/Hedgehog.Xunit.Examples.FSharp.fsproj +++ b/examples/Hedgehog.Xunit.Examples.FSharp/Hedgehog.Xunit.Examples.FSharp.fsproj @@ -1,10 +1,9 @@ - + net6.0 false - false diff --git a/src/Hedgehog.Xunit/InternalLogic.fs b/src/Hedgehog.Xunit/InternalLogic.fs index aa383ba..58f2609 100644 --- a/src/Hedgehog.Xunit/InternalLogic.fs +++ b/src/Hedgehog.Xunit/InternalLogic.fs @@ -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() diff --git a/tests/Hedgehog.Xunit.Tests.CSharp/Async.cs b/tests/Hedgehog.Xunit.Tests.CSharp/Async.cs index 8d23964..42a021b 100644 --- a/tests/Hedgehog.Xunit.Tests.CSharp/Async.cs +++ b/tests/Hedgehog.Xunit.Tests.CSharp/Async.cs @@ -9,7 +9,7 @@ public async Task Async_property_which_returns_task_can_run( int i) { await FooAsync(); - Assert.True(i == i); + Assert.StrictEqual(i, i); } [Property] diff --git a/tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs b/tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs index bad59c2..4491bf6 100644 --- a/tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs +++ b/tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs @@ -20,7 +20,7 @@ type Int6() = inherit GenAttribute() override _.Generator = Gen.constant 6 -type IntCRange(max:int, min:int)= +type IntConstantRange(max: int, min: int)= inherit GenAttribute() override _.Generator = Range.constant max min |> Gen.int32 @@ -393,33 +393,28 @@ module ``Asynchronous tests`` = [] let ``TaskResult with Error shrinks, skipped`` (i: int) = task { - do! Task.Delay 100 + do! FooAsync() if i > 10 then return Error () else return Ok () } - [] let ``TaskResult with Error shrinks`` () = assertShrunk (nameof ``TaskResult with Error shrinks, skipped``) "[11]" [] - 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 } - [] - 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 @@ -752,7 +747,7 @@ module ``returning a property runs it`` = let actual = Assert.Throws(fun () -> InternalLogic.tryRaise report) actual.Message.Contains("51") |> Assert.True -module ``Attribute Parameter Type Tests`` = +module ``GenAttribute Tests`` = [] let ``can define parameter as 5`` ([] i) = @@ -763,19 +758,20 @@ module ``Attribute Parameter Type Tests`` = Assert.StrictEqual(5, i) [] - let ``can have different generators for the same type of parameter`` ([] five) ([] six) = + let ``can have different generators for the same parameter type`` ([] five) ([] six) = five = 5 && six = 6 [] - let ``can restrict on range`` ([] i) = + let ``can restrict on range`` ([] i) = i >= 0 && i <= 5 -[, 200)>] -module ``Attribute Parameter Type Tests with Properties`` = - [)>] +[)>] +module ``GenAttribute with Properties Tests`` = + + [] let ``overrides Properties' autoGenConfig`` ([] i) = Assert.StrictEqual(5, i) [)>] - let ``overrides Property's autoGenConfig`` ([] i) = + let ``overrides Properties' and Property's autoGenConfig`` ([] i) = Assert.StrictEqual(5, i)