Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special processing for printing ResizeArray<_> in Property.forAll #323 #324

Merged
7 commits merged into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Hedgehog/Property.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,28 @@ module Property =
let bind (k : 'a -> Property<'b>) (m : Property<'a>) : Property<'b> =
bindGen (toGen << k) (toGen m) |> ofGen

let private printValue (value) : string =
// sprintf "%A" is not prepared for printing ResizeArray<_> (C# List<T>) so we prepare the value instead
let prepareForPrinting (value: obj) : obj =
#if FABLE_COMPILER
value
#else
let t = value.GetType()
// have to use TypeInfo due to targeting netstandard 1.6
let t = System.Reflection.IntrospectionExtensions.GetTypeInfo(t)
let isList = t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<ResizeArray<_>>
if isList
then value :?> System.Collections.IEnumerable |> Seq.cast<obj> |> List.ofSeq :> obj
else value
#endif

value |> prepareForPrinting |> sprintf "%A"
This conversation was marked as resolved.
Show resolved Hide resolved

let forAll (k : 'a -> Property<'b>) (gen : Gen<'a>) : Property<'b> =
let handle (e : exn) =
Gen.constant (Journal.singletonMessage (string e), Failure) |> ofGen
let prepend (x : 'a) =
counterexample (fun () -> sprintf "%A" x)
counterexample (fun () -> printValue x)
|> bind (fun _ -> try k x with e -> handle e)
|> toGen

Expand Down
1 change: 1 addition & 0 deletions tests/Hedgehog.Tests/Hedgehog.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="SeedTests.fs" />
<Compile Include="ShrinkTests.fs" />
<Compile Include="MinimalTests.fs" />
<Compile Include="PropertyTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/Hedgehog.Tests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let allTests = testList "All tests" [
SeedTests.seedTests
ShrinkTests.shrinkTests
MinimalTests.minimalTests
PropertyTests.propertyTests
]

[<EntryPoint>]
Expand Down
17 changes: 17 additions & 0 deletions tests/Hedgehog.Tests/PropertyTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Hedgehog.Tests.PropertyTests

open Hedgehog
open Expecto
open TestDsl

let propertyTests = testList "Property tests" [
fableIgnore "generated C# list of five elements is not abbreviated in the failure report" <| fun _ ->
let report =
property {
let! xs = Range.singleton 0 |> Gen.int |> Gen.list (Range.singleton 5) |> Gen.map ResizeArray
return false
}
|> Property.renderWith (PropertyConfig.withShrinks 0<shrinks> PropertyConfig.defaultConfig)
Expect.isNotMatch report "\.\.\." "Abbreviation (...) found"

]