From bd81548c5b56d34661020db7936a3ded3a92ba1e Mon Sep 17 00:00:00 2001 From: AN Date: Sun, 31 Jul 2022 14:16:47 -0500 Subject: [PATCH] `recheck runs once` passes resolves https://github.com/dharmaturtle/fsharp-hedgehog-xunit/issues/7 --- src/Hedgehog.Xunit/Exceptions.fs | 7 +++++++ src/Hedgehog.Xunit/Hedgehog.Xunit.fsproj | 1 + src/Hedgehog.Xunit/InternalLogic.fs | 6 +++--- tests/Hedgehog.Xunit.Tests/PropertyTests.fs | 6 ++++-- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 src/Hedgehog.Xunit/Exceptions.fs diff --git a/src/Hedgehog.Xunit/Exceptions.fs b/src/Hedgehog.Xunit/Exceptions.fs new file mode 100644 index 0000000..28c3cee --- /dev/null +++ b/src/Hedgehog.Xunit/Exceptions.fs @@ -0,0 +1,7 @@ +namespace Hedgehog.Xunit + +// This exists to make it clear to users that the exception is in the return of their test. +// Raising System.Exception isn't descriptive enough. +// Using Xunit.Assert.True could be confusing since it may resemble a user's assertion. +type internal TestReturnedFalseException() = + inherit System.Exception("Test returned `false`.") diff --git a/src/Hedgehog.Xunit/Hedgehog.Xunit.fsproj b/src/Hedgehog.Xunit/Hedgehog.Xunit.fsproj index 4d4037f..497f5cc 100644 --- a/src/Hedgehog.Xunit/Hedgehog.Xunit.fsproj +++ b/src/Hedgehog.Xunit/Hedgehog.Xunit.fsproj @@ -33,6 +33,7 @@ Docs at https://github.com/dharmaturtle/fsharp-hedgehog-xunit + diff --git a/src/Hedgehog.Xunit/InternalLogic.fs b/src/Hedgehog.Xunit/InternalLogic.fs index a13a370..65e3529 100644 --- a/src/Hedgehog.Xunit/InternalLogic.fs +++ b/src/Hedgehog.Xunit/InternalLogic.fs @@ -90,7 +90,7 @@ open System.Linq let rec toProperty (x: obj) = match x with - | :? bool as b -> Property.ofBool b + | :? bool as b -> if not b then TestReturnedFalseException() |> raise | :? Task as t -> Async.AwaitTask t |> toProperty | _ when x <> null && x.GetType().IsGenericType && x.GetType().GetGenericTypeDefinition().IsSubclassOf typeof -> typeof @@ -115,7 +115,7 @@ let rec toProperty (x: obj) = .MakeGenericMethod(x.GetType().GetGenericArguments()) .Invoke(null, [|x|]) |> toProperty - | _ -> Property.success () + | _ -> () let dispose (o:obj) = match o with @@ -166,7 +166,7 @@ let report (testMethod:MethodInfo) testClass testClassInstance = PropertyConfig.defaultConfig |> withTests tests |> withShrinks shrinks - Property.forAll invoke gens + PropertyBuilder.property.BindReturn(gens, invoke) |> match recheck with | Some recheckData -> Property.reportRecheckWith recheckData config | None -> Property.reportWith config diff --git a/tests/Hedgehog.Xunit.Tests/PropertyTests.fs b/tests/Hedgehog.Xunit.Tests/PropertyTests.fs index cc2eb6a..4625abb 100644 --- a/tests/Hedgehog.Xunit.Tests/PropertyTests.fs +++ b/tests/Hedgehog.Xunit.Tests/PropertyTests.fs @@ -628,7 +628,9 @@ module ``tryRaise tests`` = let report = InternalLogic.report (nameof ``always fails, skipped`` |> getMethod) typeof.DeclaringType null let actual = Assert.Throws(fun () -> InternalLogic.tryRaise report) let expectedMessage = """*** Failed! Falsifiable (after 1 test): -[] +[]""" + actual.Message.Contains(expectedMessage) |> Assert.True + let expectedMessage = """ This failure can be reproduced by running: > Property.recheck "0_""" - actual.Message.StartsWith(expectedMessage) |> Assert.True + actual.Message.Contains(expectedMessage) |> Assert.True