Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmaturtle committed Jul 1, 2023
1 parent 3cb81fb commit f51725e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
42 changes: 18 additions & 24 deletions src/Hedgehog.Xunit/InternalLogic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,12 @@ let rec yieldAndCheckReturnValue (x: obj) =
| :? Task<unit> as t -> Async.AwaitTask t |> yieldAndCheckReturnValue
| :? Task<bool> as t -> Async.AwaitTask t |> yieldAndCheckReturnValue
| _ when x <> null && x.GetType().IsGenericType && x.GetType().GetGenericTypeDefinition().IsSubclassOf typeof<Task> ->
let genType = x.GetType().GetGenericTypeDefinition()
if not genType.ContainsGenericParameters then
let t = x :?> Task
Async.AwaitTask t |> yieldAndCheckReturnValue
else
typeof<Async>
.GetMethods()
.First(fun x -> x.Name = "AwaitTask" && x.IsGenericMethod)
.MakeGenericMethod(x.GetType().GetGenericArguments())
.Invoke(null, [|x|])
|> yieldAndCheckReturnValue
typeof<Async>
.GetMethods()
.First(fun x -> x.Name = "AwaitTask" && x.IsGenericMethod)
.MakeGenericMethod(x.GetType().GetGenericArguments().First())
.Invoke(null, [|x|])
|> yieldAndCheckReturnValue
| :? Task as t -> Async.AwaitTask t |> yieldAndCheckReturnValue
| :? Async<unit> as a -> Async.RunSynchronously(a, cancellationToken = CancellationToken.None) |> yieldAndCheckReturnValue
| _ when x <> null && x.GetType().IsGenericType && x.GetType().GetGenericTypeDefinition() = typedefof<Async<_>> ->
Expand Down Expand Up @@ -162,19 +157,18 @@ let withShrinks = function

let report (testMethod:MethodInfo) testClass testClassInstance =
let getAttributeGenerator (parameterInfo: ParameterInfo) =
let attributes = parameterInfo.GetCustomAttributes()
if Seq.isEmpty attributes then
None
else
attributes
|> Seq.tryPick(fun x ->
let attType = x.GetType().BaseType
if attType.IsGenericType && attType.GetGenericTypeDefinition().IsAssignableFrom(typedefof<GenAttribute<_>>) then
let method = attType.GetMethods() |> Array.pick(fun x -> if x.Name = "Box" then Some x else None)
method.Invoke(x, null) :?> Gen<obj> |> Some
else
None
)
parameterInfo.GetCustomAttributes()
|> Seq.tryPick(fun attr ->
let attrType = attr.GetType().BaseType
if attrType.IsGenericType && attrType.GetGenericTypeDefinition().IsAssignableFrom(typedefof<GenAttribute<_>>) then
attrType
.GetMethods()
.First(fun x -> x.Name = "Box")
.Invoke(attr, null)
:?> Gen<obj> |> Some
else
None
)
let config, tests, shrinks, recheck, size = parseAttributes testMethod testClass
let gens =
testMethod.GetParameters()
Expand Down
8 changes: 7 additions & 1 deletion tests/Hedgehog.Xunit.Tests.FSharp/PropertyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ module ``Asynchronous tests`` =
let ``Returning Task with exception fails, skipped`` (i: int) : Task =
if i > 10 then
Exception() |> Task.FromException
else Task.Delay 100
else FooAsync()
[<Fact>]
let ``Returning Task with exception fails`` () =
assertShrunk (nameof ``Returning Task with exception fails, skipped``) "[11]"
Expand Down Expand Up @@ -765,6 +765,12 @@ module ``GenAttribute Tests`` =
let ``can restrict on range`` ([<IntConstantRange(min = 0, max = 5)>] i) =
i >= 0 && i <= 5

type OtherAttribute() = inherit Attribute()

[<Property>]
let ``Doesn't error with OtherAttribute`` ([<OtherAttribute>][<Int5>] i) =
i = 5

[<Properties(typeof<Int13>)>]
module ``GenAttribute with Properties Tests`` =

Expand Down

0 comments on commit f51725e

Please sign in to comment.